|
21 | 21 |
|
22 | 22 | import java.io.File; |
23 | 23 | import java.io.IOException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.util.Collections; |
24 | 27 | import java.util.List; |
| 28 | +import java.util.Set; |
25 | 29 |
|
| 30 | +import org.apache.commons.lang3.StringUtils; |
26 | 31 | import org.apache.maven.plugin.MojoExecutionException; |
27 | 32 | import org.apache.maven.plugin.MojoFailureException; |
28 | 33 | import org.apache.maven.plugins.annotations.Component; |
|
32 | 37 | import org.codehaus.plexus.archiver.ArchiverException; |
33 | 38 | import org.codehaus.plexus.archiver.zip.ZipArchiver; |
34 | 39 |
|
| 40 | +import io.wcm.devops.conga.generator.util.FileUtil; |
35 | 41 | import io.wcm.devops.conga.plugins.aem.maven.model.ModelParser; |
36 | 42 |
|
37 | 43 | /** |
@@ -72,16 +78,60 @@ public void execute() throws MojoExecutionException, MojoFailureException { |
72 | 78 | private void buildDispatcherConfig(File nodeDir) throws MojoFailureException { |
73 | 79 | File targetFile = new File(getTargetDir(), nodeDir.getName() + ".dispatcher-config.zip"); |
74 | 80 |
|
75 | | - zipArchiver.setDestFile(targetFile); |
76 | | - String[] includes = new String[] { "**/*" }; |
77 | | - String[] excludes = new String[] { ModelParser.MODEL_FILE }; |
78 | | - zipArchiver.addDirectory(nodeDir, includes, excludes); |
79 | 81 | try { |
| 82 | + String basePath = toZipDirectoryPath(nodeDir); |
| 83 | + addZipDirectory(basePath, nodeDir, Collections.singleton(ModelParser.MODEL_FILE)); |
| 84 | + zipArchiver.setDestFile(targetFile); |
| 85 | + |
80 | 86 | zipArchiver.createArchive(); |
81 | 87 | } |
82 | 88 | catch (ArchiverException | IOException ex) { |
83 | 89 | throw new MojoFailureException("Unable to build file " + targetFile.getPath() + ": " + ex.getMessage(), ex); |
84 | 90 | } |
85 | 91 | } |
86 | 92 |
|
| 93 | + /** |
| 94 | + * Recursive through all directory and add file to zipArchiver. |
| 95 | + * This method has special support for symlinks which are required for dispatcher configuration. |
| 96 | + * @param basePath Base path |
| 97 | + * @param directory Directory to include |
| 98 | + * @param excludeFiles Exclude filenames |
| 99 | + * @throws IOException I/O exception |
| 100 | + */ |
| 101 | + private void addZipDirectory(String basePath, File directory, Set<String> excludeFiles) throws IOException { |
| 102 | + String directoryPath = toZipDirectoryPath(directory); |
| 103 | + if (StringUtils.startsWith(directoryPath, basePath)) { |
| 104 | + String relativeDirectoryPath = StringUtils.substring(directoryPath, basePath.length()); |
| 105 | + File[] files = directory.listFiles(); |
| 106 | + if (files != null) { |
| 107 | + for (File file : files) { |
| 108 | + if (excludeFiles.contains(file.getName())) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + if (file.isDirectory()) { |
| 112 | + addZipDirectory(basePath, file, Collections.emptySet()); |
| 113 | + } |
| 114 | + else if (Files.isSymbolicLink(file.toPath())) { |
| 115 | + Path linkPath = file.toPath(); |
| 116 | + Path targetPath = linkPath.toRealPath(); |
| 117 | + Path symlinkPath = file.getParentFile().toPath().relativize(targetPath); |
| 118 | + zipArchiver.addSymlink(relativeDirectoryPath + file.getName(), sanitizePathSeparators(symlinkPath.toString())); |
| 119 | + } |
| 120 | + else { |
| 121 | + zipArchiver.addFile(file, relativeDirectoryPath + file.getName()); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private String toZipDirectoryPath(File directory) { |
| 129 | + String canoncialPath = FileUtil.getCanonicalPath(directory); |
| 130 | + return sanitizePathSeparators(canoncialPath) + "/"; |
| 131 | + } |
| 132 | + |
| 133 | + private String sanitizePathSeparators(String path) { |
| 134 | + return StringUtils.replace(path, "\\", "/"); |
| 135 | + } |
| 136 | + |
87 | 137 | } |
0 commit comments