1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.acr;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.nio.file.Path;
27 import java.util.List;
28
29 import org.apache.commons.io.input.XmlStreamReader;
30 import org.apache.maven.archiver.MavenArchiveConfiguration;
31 import org.apache.maven.archiver.MavenArchiver;
32 import org.apache.maven.artifact.DependencyResolutionRequiredException;
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.plugin.AbstractMojo;
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.plugins.annotations.ResolutionScope;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.shared.filtering.FilterWrapper;
42 import org.apache.maven.shared.filtering.MavenFileFilter;
43 import org.apache.maven.shared.filtering.MavenFilteringException;
44 import org.apache.maven.shared.filtering.MavenResourcesExecution;
45 import org.codehaus.plexus.archiver.ArchiverException;
46 import org.codehaus.plexus.archiver.jar.JarArchiver;
47 import org.codehaus.plexus.archiver.jar.ManifestException;
48 import org.codehaus.plexus.util.FileUtils;
49
50
51
52
53
54
55
56 @Mojo(
57 name = "acr",
58 requiresDependencyResolution = ResolutionScope.RUNTIME,
59 threadSafe = true,
60 defaultPhase = LifecyclePhase.PACKAGE)
61 public class AcrMojo extends AbstractMojo {
62
63 private static final String APP_CLIENT_XML = "META-INF/application-client.xml";
64
65
66 private static final String[] DEFAULT_INCLUDES = {"**/**"};
67
68 private static final String[] DEFAULT_EXCLUDES = {APP_CLIENT_XML};
69
70
71
72
73 @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true)
74 private File basedir;
75
76
77
78
79
80
81 @Parameter(property = "maven.acr.outputDirectory", defaultValue = "${project.build.outputDirectory}")
82 private File outputDirectory;
83
84
85
86
87 @Parameter(defaultValue = "${project.build.finalName}")
88 private String jarName;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 @Parameter
104 private List<String> excludes;
105
106
107
108
109 @Parameter(defaultValue = "${project}", readonly = true, required = true)
110 private MavenProject project;
111
112
113
114
115
116 @Parameter
117 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
118
119
120
121
122
123
124 @Parameter(property = "maven.acr.escapeBackslashesInFilePath", defaultValue = "false")
125 private boolean escapeBackslashesInFilePath;
126
127
128
129
130
131
132 @Parameter(property = "maven.acr.escapeString")
133 private String escapeString;
134
135
136
137
138
139 @Parameter(property = "maven.acr.filterDeploymentDescriptor", defaultValue = "false")
140 private boolean filterDeploymentDescriptor;
141
142
143
144
145 @Parameter
146 private List<String> filters;
147
148 @Parameter(defaultValue = "${session}", readonly = true, required = true)
149 private MavenSession session;
150
151
152
153
154
155
156
157
158 @Parameter(defaultValue = "${project.build.outputTimestamp}")
159 private String outputTimestamp;
160
161
162
163
164 private final JarArchiver jarArchiver;
165
166 private final MavenFileFilter mavenFileFilter;
167
168 @Inject
169 public AcrMojo(JarArchiver jarArchiver, @Named("default") MavenFileFilter mavenFileFilter) {
170 this.jarArchiver = jarArchiver;
171 this.mavenFileFilter = mavenFileFilter;
172 }
173
174
175 public void execute() throws MojoExecutionException {
176 if (getLog().isInfoEnabled()) {
177 getLog().info("Building JavaEE Application client: " + jarName);
178 }
179
180 File jarFile = getAppClientJarFile(basedir, jarName);
181
182 MavenArchiver archiver = new MavenArchiver();
183
184 archiver.setArchiver(jarArchiver);
185
186 archiver.setCreatedBy("Maven ACR Plugin", "org.apache.maven.plugins", "maven-acr-plugin");
187
188 archiver.setOutputFile(jarFile);
189
190
191 archiver.configureReproducibleBuild(outputTimestamp);
192
193 try {
194 String[] mainJarExcludes = DEFAULT_EXCLUDES;
195
196 if (excludes != null && !excludes.isEmpty()) {
197 excludes.add(APP_CLIENT_XML);
198 mainJarExcludes = excludes.toArray(new String[0]);
199 }
200
201 if (outputDirectory.exists()) {
202 archiver.getArchiver().addDirectory(outputDirectory, DEFAULT_INCLUDES, mainJarExcludes);
203 } else {
204
205 getLog().info(
206 "JAR will only contain the META-INF/application-client.xml as no content was marked for inclusion");
207
208 }
209
210 File deploymentDescriptor = new File(outputDirectory, APP_CLIENT_XML);
211
212 if (deploymentDescriptor.exists()) {
213 if (filterDeploymentDescriptor) {
214 getLog().debug("Filtering deployment descriptor.");
215 MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution();
216 mavenResourcesExecution.setEscapeString(escapeString);
217 List<FilterWrapper> filterWrappers = mavenFileFilter.getDefaultFilterWrappers(
218 project, filters, escapeBackslashesInFilePath, this.session, mavenResourcesExecution);
219
220
221 File unfilteredDeploymentDescriptor = new File(outputDirectory, APP_CLIENT_XML + ".unfiltered");
222 FileUtils.copyFile(deploymentDescriptor, unfilteredDeploymentDescriptor);
223 mavenFileFilter.copyFile(
224 unfilteredDeploymentDescriptor,
225 deploymentDescriptor,
226 true,
227 filterWrappers,
228 getEncoding(unfilteredDeploymentDescriptor.toPath()));
229
230 FileUtils.forceDelete(unfilteredDeploymentDescriptor);
231 }
232 archiver.getArchiver().addFile(deploymentDescriptor, APP_CLIENT_XML);
233 }
234
235
236 archiver.createArchive(session, project, archive);
237
238 } catch (ArchiverException e) {
239 throw new MojoExecutionException(
240 "There was a problem creating the JavaEE Application Client archive: " + e.getMessage(), e);
241 } catch (ManifestException e) {
242 throw new MojoExecutionException(
243 "There was a problem reading / creating the manifest for the JavaEE Application Client archive: "
244 + e.getMessage(),
245 e);
246 } catch (IOException e) {
247 throw new MojoExecutionException(
248 "There was a I/O problem creating the JavaEE Application Client archive: " + e.getMessage(), e);
249 } catch (DependencyResolutionRequiredException e) {
250 throw new MojoExecutionException(
251 "There was a problem resolving dependencies while creating the JavaEE Application Client archive: "
252 + e.getMessage(),
253 e);
254 } catch (MavenFilteringException e) {
255 throw new MojoExecutionException(
256 "There was a problem filtering the deployment descriptor: " + e.getMessage(), e);
257 }
258
259 project.getArtifact().setFile(jarFile);
260
261
262 }
263
264
265
266
267
268
269
270
271 private static File getAppClientJarFile(File basedir, String finalName) {
272 return new File(basedir, finalName + ".jar");
273 }
274
275
276
277
278
279
280
281
282 private String getEncoding(Path xmlFile) throws IOException {
283 try (XmlStreamReader xmlReader =
284 XmlStreamReader.builder().setPath(xmlFile).get()) {
285 return xmlReader.getEncoding();
286 }
287 }
288 }