-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathAbstractJarMojo.java
More file actions
366 lines (320 loc) · 13.2 KB
/
AbstractJarMojo.java
File metadata and controls
366 lines (320 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.jar;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.jar.Attributes;
import java.util.stream.Stream;
import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Parameter;
import org.apache.maven.api.services.ProjectManager;
import org.apache.maven.shared.archiver.MavenArchiveConfiguration;
import org.apache.maven.shared.archiver.MavenArchiver;
import org.apache.maven.shared.archiver.MavenArchiverException;
import org.apache.maven.shared.model.fileset.FileSet;
import org.apache.maven.shared.model.fileset.util.FileSetManager;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.jar.JarArchiver;
/**
* Base class for creating a <abbr>JAR</abbr> file from project classes.
*
* @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
*/
public abstract class AbstractJarMojo implements org.apache.maven.api.plugin.Mojo {
private static final String[] DEFAULT_EXCLUDES = new String[] {"**/package.html"};
private static final String[] DEFAULT_INCLUDES = new String[] {"**/**"};
private static final String MODULE_DESCRIPTOR_FILE_NAME = "module-info.class";
/**
* List of files to include. Specified as fileset patterns which are relative to the input directory whose contents
* is being packaged into the JAR.
*/
@Parameter
private String[] includes;
/**
* List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents
* is being packaged into the JAR.
*/
@Parameter
private String[] excludes;
/**
* Directory containing the generated JAR.
*/
@Parameter(defaultValue = "${project.build.directory}", required = true)
private Path outputDirectory;
/**
* Name of the generated JAR.
*/
@Parameter(defaultValue = "${project.build.finalName}", readonly = true)
private String finalName;
/**
* The JAR archiver.
*/
@Inject
private Map<String, Archiver> archivers;
/**
* The Maven project.
*/
@Inject
private Project project;
/**
* The session.
*/
@Inject
private Session session;
/**
* The archive configuration to use. See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
* Archiver Reference</a>.
*/
@Parameter
private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
@Inject
private ProjectManager projectManager;
/**
* Require the jar plugin to build a new JAR even if none of the contents appear to have changed.
* By default, this plugin looks to see if the output JAR exists and inputs have not changed.
* If these conditions are true, the plugin skips creation of the JAR file.
* This does not work when other plugins, like the maven-shade-plugin, are configured to post-process the JAR.
* This plugin can not detect the post-processing, and so leaves the post-processed JAR file in place.
* This can lead to failures when those plugins do not expect to find their own output as an input.
* Set this parameter to {@code true} to avoid these problems by forcing this plugin to recreate the JAR every time.
*
* <p>Starting with <b>3.0.0</b> the property has been renamed from {@code jar.forceCreation}
* to {@code maven.jar.forceCreation}.</p>
*/
@Parameter(property = "maven.jar.forceCreation", defaultValue = "false")
private boolean forceCreation;
/**
* Skip creating empty archives.
*/
@Parameter(defaultValue = "false")
private boolean skipIfEmpty;
/**
* Timestamp for reproducible output archive entries.
* This is either formatted as ISO 8601 extended offset date-time
* (e.g. in UTC such as '2011-12-03T10:15:30Z' or with an offset '2019-10-05T20:37:42+06:00'),
* or as an integer representing seconds since the epoch
* (like <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
*
* @since 3.2.0
*/
@Parameter(defaultValue = "${project.build.outputTimestamp}")
private String outputTimestamp;
/**
* Whether to detect multi-release JAR files.
* If the JAR contains the {@code META-INF/versions} directory it will be detected as a multi-release JAR file
* ("MRJAR"), adding the {@code Multi-Release: true} attribute to the main section of the JAR {@code MANIFEST.MF}.
*
* @since 3.4.0
*/
@Parameter(property = "maven.jar.detectMultiReleaseJar", defaultValue = "true")
private boolean detectMultiReleaseJar;
/**
* The <abbr>MOJO</abbr> logger.
*/
@Inject
private Log log;
/**
* Creates a new <abbr>MOJO</abbr>.
*/
protected AbstractJarMojo() {}
/**
* Specifies whether to attach the jar to the project.
*
* @since 4.0.0-beta-2
*/
@Parameter(property = "maven.jar.attach", defaultValue = "true")
protected boolean attach;
/**
* {@return the specific output directory to serve as the root for the archive}
*/
protected abstract Path getClassesDirectory();
/**
* Return the {@linkplain #project Maven project}.
*
* @return the Maven project
*/
protected final Project getProject() {
return project;
}
/**
* {@return the <abbr>MOJO</abbr> logger}
*/
protected final Log getLog() {
return log;
}
/**
* {@return the classifier of the JAR file to produce}
* This is usually null or empty for the main artifact, or {@code "tests"} for the JAR file of test code.
*/
protected abstract String getClassifier();
/**
* {@return the type of the JAR file to produce}
* This is usually {@code "jar"} for the main artifact, or {@code "test-jar"} for the JAR file of test code.
*/
protected abstract String getType();
/**
* Returns the JAR file to generate, based on an optional classifier.
*
* @param basedir the output directory
* @param resultFinalName the name of the JAR file
* @param classifier an optional classifier
* @return the file to generate
*/
protected Path getJarFile(Path basedir, String resultFinalName, String classifier) {
Objects.requireNonNull(basedir, "basedir is not allowed to be null");
Objects.requireNonNull(resultFinalName, "finalName is not allowed to be null");
String fileName = resultFinalName + (hasClassifier(classifier) ? '-' + classifier : "") + ".jar";
return basedir.resolve(fileName);
}
/**
* Generates the JAR.
*
* @return the path to the created archive file
* @throws MojoException in case of an error
*/
public Path createArchive() throws MojoException {
Path jarFile = getJarFile(outputDirectory, finalName, getClassifier());
FileSetManager fileSetManager = new FileSetManager();
FileSet jarContentFileSet = new FileSet();
jarContentFileSet.setDirectory(getClassesDirectory().toAbsolutePath().toString());
jarContentFileSet.setIncludes(Arrays.asList(getIncludes()));
jarContentFileSet.setExcludes(Arrays.asList(getExcludes()));
String[] includedFiles = fileSetManager.getIncludedFiles(jarContentFileSet);
if (detectMultiReleaseJar
&& Arrays.stream(includedFiles)
.anyMatch(
p -> p.startsWith("META-INF" + File.separatorChar + "versions" + File.separatorChar))) {
getLog().debug("Adding 'Multi-Release: true' manifest entry.");
archive.addManifestEntry(Attributes.Name.MULTI_RELEASE.toString(), "true");
}
// May give false positives if the files is named as module descriptor
// but is not in the root of the archive or in the versioned area
// (and hence not actually a module descriptor).
// That is fine since the modular Jar archiver will gracefully
// handle such case.
// And also such case is unlikely to happen as file ending
// with "module-info.class" is unlikely to be included in Jar file
// unless it is a module descriptor.
boolean containsModuleDescriptor =
Arrays.stream(includedFiles).anyMatch(p -> p.endsWith(MODULE_DESCRIPTOR_FILE_NAME));
String archiverName = containsModuleDescriptor ? "mjar" : "jar";
MavenArchiver archiver = new MavenArchiver();
archiver.setCreatedBy("Maven JAR Plugin", "org.apache.maven.plugins", "maven-jar-plugin");
archiver.setArchiver((JarArchiver) archivers.get(archiverName));
archiver.setOutputFile(jarFile.toFile());
// configure for Reproducible Builds based on outputTimestamp value
archiver.configureReproducibleBuild(outputTimestamp);
archive.setForced(forceCreation);
try {
Path contentDirectory = getClassesDirectory();
if (!Files.exists(contentDirectory)) {
if (!forceCreation) {
getLog().warn("JAR will be empty - no content was marked for inclusion!");
}
} else {
archiver.getArchiver().addDirectory(contentDirectory.toFile(), getIncludes(), getExcludes());
}
archiver.createArchive(session, project, archive);
return jarFile;
} catch (Exception e) {
// TODO: improve error handling
throw new MojoException("Error assembling JAR", e);
}
}
/**
* Generates the JAR.
*
* @throws MojoException in case of an error
*/
@Override
public void execute() throws MojoException {
if (skipIfEmpty && isEmpty(getClassesDirectory())) {
getLog().info(String.format("Skipping packaging of the %s.", getType()));
} else {
Path jarFile = createArchive();
ProducedArtifact artifact;
String classifier = getClassifier();
if (attach) {
if (hasClassifier(classifier)) {
artifact = session.createProducedArtifact(
project.getGroupId(),
project.getArtifactId(),
project.getVersion(),
classifier,
null,
getType());
} else {
if (projectHasAlreadySetAnArtifact()) {
throw new MojoException("You have to use a classifier "
+ "to attach supplemental artifacts to the project instead of replacing them.");
}
artifact = project.getMainArtifact().get();
}
projectManager.attachArtifact(project, artifact, jarFile);
} else {
getLog().debug("Skipping attachment of the " + getType() + " artifact to the project.");
}
}
}
private static boolean isEmpty(Path directory) {
if (!Files.isDirectory(directory)) {
return true;
}
try (Stream<Path> children = Files.list(directory)) {
return children.findAny().isEmpty();
} catch (IOException e) {
throw new MavenArchiverException("Unable to access directory", e);
}
}
private boolean projectHasAlreadySetAnArtifact() {
Path path = projectManager.getPath(project).orElse(null);
return path != null && Files.isRegularFile(path);
}
/**
* Return {@code true} if the classifier is not {@code null} and contains something other than white spaces.
*
* @param classifier the classifier to verify
* @return {@code true} if the classifier is set
*/
private static boolean hasClassifier(String classifier) {
return classifier != null && !classifier.isBlank();
}
private String[] getIncludes() {
if (includes != null && includes.length > 0) {
return includes;
}
return DEFAULT_INCLUDES;
}
private String[] getExcludes() {
if (excludes != null && excludes.length > 0) {
return excludes;
}
return DEFAULT_EXCLUDES;
}
}