1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.resources.remote;
20
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.nio.charset.Charset;
26 import java.util.Comparator;
27 import java.util.List;
28
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Writer;
32 import org.apache.maven.plugins.annotations.LifecyclePhase;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.codehaus.plexus.util.DirectoryScanner;
36 import org.codehaus.plexus.util.FileUtils;
37 import org.codehaus.plexus.util.StringUtils;
38
39
40
41
42
43 @Mojo(name = "bundle", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
44 public class BundleRemoteResourcesMojo extends AbstractMojo {
45 public static final String RESOURCES_MANIFEST = "META-INF/maven/remote-resources.xml";
46
47 private static final String[] DEFAULT_INCLUDES = new String[] {
48 "**/*.txt", "**/*.vm",
49 };
50
51
52
53
54 @Parameter(defaultValue = "${basedir}/src/main/resources")
55 private File resourcesDirectory;
56
57
58
59
60 @Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
61 private File outputDirectory;
62
63
64
65
66
67
68
69
70 @Parameter
71 private String[] includes;
72
73
74
75
76
77
78 @Parameter
79 private String[] excludes;
80
81
82
83
84
85
86 @Parameter(defaultValue = "${project.build.sourceEncoding}")
87 private String sourceEncoding;
88
89
90
91
92
93
94
95 @Parameter(property = "bundle.requiredProjectProperties")
96 private List<String> requiredProjectProperties;
97
98 @Override
99 public void execute() throws MojoExecutionException {
100 if (!resourcesDirectory.exists()) {
101 getLog().info("skip non existing resourceDirectory " + resourcesDirectory.getAbsolutePath());
102 return;
103 }
104
105 if (sourceEncoding == null || sourceEncoding.isEmpty()) {
106 getLog().warn("sourceEncoding has not been set; using platform encoding " + Charset.defaultCharset()
107 + "; i.e. build is platform dependent!");
108 sourceEncoding = Charset.defaultCharset().name();
109 }
110
111
112
113
114 RemoteResourcesBundle remoteResourcesBundle = new RemoteResourcesBundle();
115 remoteResourcesBundle.setSourceEncoding(sourceEncoding);
116 remoteResourcesBundle.setRequiredProjectProperties(requiredProjectProperties);
117
118 DirectoryScanner scanner = new DirectoryScanner();
119 scanner.setFilenameComparator(Comparator.naturalOrder());
120
121 scanner.setBasedir(resourcesDirectory);
122 if (includes != null && includes.length != 0) {
123 scanner.setIncludes(includes);
124 } else {
125 scanner.setIncludes(DEFAULT_INCLUDES);
126 }
127
128 if (excludes != null && excludes.length != 0) {
129 scanner.setExcludes(excludes);
130 }
131
132 scanner.addDefaultExcludes();
133 scanner.scan();
134
135 String[] includedFiles = scanner.getIncludedFiles();
136
137 for (String resource : includedFiles) {
138 remoteResourcesBundle.addRemoteResource(StringUtils.replace(resource, '\\', '/'));
139 }
140
141 int n = remoteResourcesBundle.getRemoteResources().size();
142 getLog().info("Writing " + RESOURCES_MANIFEST + " descriptor with " + n + " entr" + ((n > 1) ? "ies" : "y"));
143
144 RemoteResourcesBundleXpp3Writer w = new RemoteResourcesBundleXpp3Writer();
145
146 File f = new File(outputDirectory, RESOURCES_MANIFEST);
147
148 FileUtils.mkdir(f.getParentFile().getAbsolutePath());
149
150 try (Writer writer = new FileWriter(f)) {
151 w.write(writer, remoteResourcesBundle);
152 } catch (IOException e) {
153 throw new MojoExecutionException("Error creating remote resources manifest.", e);
154 }
155 }
156 }