-
Notifications
You must be signed in to change notification settings - Fork 22
/
MavenArchiver.java
828 lines (711 loc) · 35.8 KB
/
MavenArchiver.java
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
* 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.archiver;
import javax.lang.model.SourceVersion;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.jar.Attributes;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.Manifest;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.PrefixedPropertiesValueSource;
import org.codehaus.plexus.interpolation.RecursionInterceptor;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.interpolation.ValueSource;
import static org.apache.maven.archiver.ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM;
import static org.apache.maven.archiver.ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY;
import static org.apache.maven.archiver.ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_SIMPLE;
/**
* <p>MavenArchiver class.</p>
*
* @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
* @author kama
* @version $Id: $Id
*/
public class MavenArchiver {
private static final String CREATED_BY = "Maven Archiver";
/**
* The simply layout.
*/
public static final String SIMPLE_LAYOUT =
"${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}";
/**
* Repository layout.
*/
public static final String REPOSITORY_LAYOUT =
"${artifact.groupIdPath}/${artifact.artifactId}/" + "${artifact.baseVersion}/${artifact.artifactId}-"
+ "${artifact.version}${dashClassifier?}.${artifact.extension}";
/**
* simple layout non unique.
*/
public static final String SIMPLE_LAYOUT_NONUNIQUE =
"${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}";
/**
* Repository layout non unique.
*/
public static final String REPOSITORY_LAYOUT_NONUNIQUE =
"${artifact.groupIdPath}/${artifact.artifactId}/" + "${artifact.baseVersion}/${artifact.artifactId}-"
+ "${artifact.baseVersion}${dashClassifier?}.${artifact.extension}";
private static final Instant DATE_MIN = Instant.parse("1980-01-01T00:00:02Z");
private static final Instant DATE_MAX = Instant.parse("2099-12-31T23:59:59Z");
private static final List<String> ARTIFACT_EXPRESSION_PREFIXES;
static {
List<String> artifactExpressionPrefixes = new ArrayList<>();
artifactExpressionPrefixes.add("artifact.");
ARTIFACT_EXPRESSION_PREFIXES = artifactExpressionPrefixes;
}
static boolean isValidModuleName(String name) {
return SourceVersion.isName(name);
}
private JarArchiver archiver;
private File archiveFile;
private String createdBy;
private boolean buildJdkSpecDefaultEntry = true;
/**
* <p>getManifest.</p>
*
* @param session the Maven Session
* @param project the Maven Project
* @param config the MavenArchiveConfiguration
* @return the {@link org.codehaus.plexus.archiver.jar.Manifest}
* @throws org.codehaus.plexus.archiver.jar.ManifestException in case of a failure
* @throws org.apache.maven.artifact.DependencyResolutionRequiredException resolution failure
*/
public Manifest getManifest(MavenSession session, MavenProject project, MavenArchiveConfiguration config)
throws ManifestException, DependencyResolutionRequiredException {
boolean hasManifestEntries = !config.isManifestEntriesEmpty();
Map<String, String> entries = hasManifestEntries ? config.getManifestEntries() : Collections.emptyMap();
Manifest manifest = getManifest(session, project, config.getManifest(), entries);
// any custom manifest entries in the archive configuration manifest?
if (hasManifestEntries) {
for (Map.Entry<String, String> entry : entries.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Manifest.Attribute attr = manifest.getMainSection().getAttribute(key);
if (key.equals(Attributes.Name.CLASS_PATH.toString()) && attr != null) {
// Merge the user-supplied Class-Path value with the programmatically
// created Class-Path. Note that the user-supplied value goes first
// so that resources there will override any in the standard Class-Path.
attr.setValue(value + " " + attr.getValue());
} else {
addManifestAttribute(manifest, key, value);
}
}
}
// any custom manifest sections in the archive configuration manifest?
if (!config.isManifestSectionsEmpty()) {
for (ManifestSection section : config.getManifestSections()) {
Manifest.Section theSection = new Manifest.Section();
theSection.setName(section.getName());
if (!section.isManifestEntriesEmpty()) {
Map<String, String> sectionEntries = section.getManifestEntries();
for (Map.Entry<String, String> entry : sectionEntries.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Manifest.Attribute attr = new Manifest.Attribute(key, value);
theSection.addConfiguredAttribute(attr);
}
}
manifest.addConfiguredSection(theSection);
}
}
return manifest;
}
/**
* Return a pre-configured manifest.
*
* @param project {@link org.apache.maven.project.MavenProject}
* @param config {@link org.apache.maven.archiver.ManifestConfiguration}
* @return {@link org.codehaus.plexus.archiver.jar.Manifest}
* @throws org.codehaus.plexus.archiver.jar.ManifestException Manifest exception.
* @throws org.apache.maven.artifact.DependencyResolutionRequiredException Dependency resolution exception.
*/
// TODO Add user attributes list and user groups list
public Manifest getManifest(MavenProject project, ManifestConfiguration config)
throws ManifestException, DependencyResolutionRequiredException {
return getManifest(null, project, config, Collections.emptyMap());
}
/**
* <p>getManifest.</p>
*
* @param mavenSession {@link org.apache.maven.execution.MavenSession}
* @param project {@link org.apache.maven.project.MavenProject}
* @param config {@link org.apache.maven.archiver.ManifestConfiguration}
* @return {@link org.codehaus.plexus.archiver.jar.Manifest}
* @throws org.codehaus.plexus.archiver.jar.ManifestException the manifest exception
* @throws org.apache.maven.artifact.DependencyResolutionRequiredException the dependency resolution required
* exception
*/
public Manifest getManifest(MavenSession mavenSession, MavenProject project, ManifestConfiguration config)
throws ManifestException, DependencyResolutionRequiredException {
return getManifest(mavenSession, project, config, Collections.emptyMap());
}
private void addManifestAttribute(Manifest manifest, Map<String, String> map, String key, String value)
throws ManifestException {
if (map.containsKey(key)) {
return; // The map value will be added later
}
addManifestAttribute(manifest, key, value);
}
private void addManifestAttribute(Manifest manifest, String key, String value) throws ManifestException {
if (!(value == null || value.isEmpty())) {
Manifest.Attribute attr = new Manifest.Attribute(key, value);
manifest.addConfiguredAttribute(attr);
} else {
// if the value is empty, create an entry with an empty string
// to prevent null print in the manifest file
Manifest.Attribute attr = new Manifest.Attribute(key, "");
manifest.addConfiguredAttribute(attr);
}
}
/**
* <p>getManifest.</p>
*
* @param session {@link org.apache.maven.execution.MavenSession}
* @param project {@link org.apache.maven.project.MavenProject}
* @param config {@link org.apache.maven.archiver.ManifestConfiguration}
* @param entries The entries.
* @return {@link org.codehaus.plexus.archiver.jar.Manifest}
* @throws org.codehaus.plexus.archiver.jar.ManifestException the manifest exception
* @throws org.apache.maven.artifact.DependencyResolutionRequiredException the dependency resolution required
* exception
*/
protected Manifest getManifest(
MavenSession session, MavenProject project, ManifestConfiguration config, Map<String, String> entries)
throws ManifestException, DependencyResolutionRequiredException {
// TODO: Should we replace "map" with a copy? Note, that we modify it!
Manifest m = new Manifest();
if (config.isAddDefaultEntries()) {
handleDefaultEntries(m, entries);
}
if (config.isAddBuildEnvironmentEntries()) {
handleBuildEnvironmentEntries(session, m, entries);
}
if (config.isAddClasspath()) {
StringBuilder classpath = new StringBuilder();
List<String> artifacts = project.getRuntimeClasspathElements();
String classpathPrefix = config.getClasspathPrefix();
String layoutType = config.getClasspathLayoutType();
String layout = config.getCustomClasspathLayout();
Interpolator interpolator = new StringSearchInterpolator();
for (String artifactFile : artifacts) {
File f = new File(artifactFile);
if (f.getAbsoluteFile().isFile()) {
Artifact artifact = findArtifactWithFile(project.getArtifacts(), f);
if (classpath.length() > 0) {
classpath.append(" ");
}
classpath.append(classpathPrefix);
// NOTE: If the artifact or layout type (from config) is null, give up and use the file name by
// itself.
if (artifact == null || layoutType == null) {
classpath.append(f.getName());
} else {
List<ValueSource> valueSources = new ArrayList<>();
handleExtraExpression(artifact, valueSources);
for (ValueSource vs : valueSources) {
interpolator.addValueSource(vs);
}
RecursionInterceptor recursionInterceptor =
new PrefixAwareRecursionInterceptor(ARTIFACT_EXPRESSION_PREFIXES);
try {
switch (layoutType) {
case CLASSPATH_LAYOUT_TYPE_SIMPLE:
if (config.isUseUniqueVersions()) {
classpath.append(interpolator.interpolate(SIMPLE_LAYOUT, recursionInterceptor));
} else {
classpath.append(interpolator.interpolate(
SIMPLE_LAYOUT_NONUNIQUE, recursionInterceptor));
}
break;
case CLASSPATH_LAYOUT_TYPE_REPOSITORY:
// we use layout /$groupId[0]/../${groupId[n]/$artifactId/$version/{fileName}
// here we must find the Artifact in the project Artifacts
// to create the maven layout
if (config.isUseUniqueVersions()) {
classpath.append(
interpolator.interpolate(REPOSITORY_LAYOUT, recursionInterceptor));
} else {
classpath.append(interpolator.interpolate(
REPOSITORY_LAYOUT_NONUNIQUE, recursionInterceptor));
}
break;
case CLASSPATH_LAYOUT_TYPE_CUSTOM:
if (layout == null) {
throw new ManifestException(CLASSPATH_LAYOUT_TYPE_CUSTOM
+ " layout type was declared, but custom layout expression was not"
+ " specified. Check your <archive><manifest><customLayout/>"
+ " element.");
}
classpath.append(interpolator.interpolate(layout, recursionInterceptor));
break;
default:
throw new ManifestException("Unknown classpath layout type: '" + layoutType
+ "'. Check your <archive><manifest><layoutType/> element.");
}
} catch (InterpolationException e) {
ManifestException error = new ManifestException(
"Error interpolating artifact path for classpath entry: " + e.getMessage());
error.initCause(e);
throw error;
} finally {
for (ValueSource vs : valueSources) {
interpolator.removeValuesSource(vs);
}
}
}
}
}
if (classpath.length() > 0) {
// Class-Path is special and should be added to manifest even if
// it is specified in the manifestEntries section
addManifestAttribute(m, "Class-Path", classpath.toString());
}
}
if (config.isAddDefaultSpecificationEntries()) {
handleSpecificationEntries(project, entries, m);
}
if (config.isAddDefaultImplementationEntries()) {
handleImplementationEntries(project, entries, m);
}
String mainClass = config.getMainClass();
if (mainClass != null && !"".equals(mainClass)) {
addManifestAttribute(m, entries, "Main-Class", mainClass);
}
if (config.isAddExtensions()) {
handleExtensions(project, entries, m);
}
addCustomEntries(m, entries, config);
return m;
}
private void handleExtraExpression(Artifact artifact, List<ValueSource> valueSources) {
valueSources.add(new PrefixedObjectValueSource(ARTIFACT_EXPRESSION_PREFIXES, artifact, true));
valueSources.add(
new PrefixedObjectValueSource(ARTIFACT_EXPRESSION_PREFIXES, artifact.getArtifactHandler(), true));
Properties extraExpressions = new Properties();
// FIXME: This query method SHOULD NOT affect the internal
// state of the artifact version, but it does.
if (!artifact.isSnapshot()) {
extraExpressions.setProperty("baseVersion", artifact.getVersion());
}
extraExpressions.setProperty("groupIdPath", artifact.getGroupId().replace('.', '/'));
String classifier = artifact.getClassifier();
if (classifier != null && !classifier.isEmpty()) {
extraExpressions.setProperty("dashClassifier", "-" + classifier);
extraExpressions.setProperty("dashClassifier?", "-" + classifier);
} else {
extraExpressions.setProperty("dashClassifier", "");
extraExpressions.setProperty("dashClassifier?", "");
}
valueSources.add(new PrefixedPropertiesValueSource(ARTIFACT_EXPRESSION_PREFIXES, extraExpressions, true));
}
private void handleExtensions(MavenProject project, Map<String, String> entries, Manifest m)
throws ManifestException {
// TODO: this is only for applets - should we distinguish them as a packaging?
StringBuilder extensionsList = new StringBuilder();
Set<Artifact> artifacts = project.getArtifacts();
for (Artifact artifact : artifacts) {
if (!Artifact.SCOPE_TEST.equals(artifact.getScope())) {
if ("jar".equals(artifact.getType())) {
if (extensionsList.length() > 0) {
extensionsList.append(" ");
}
extensionsList.append(artifact.getArtifactId());
}
}
}
if (extensionsList.length() > 0) {
addManifestAttribute(m, entries, "Extension-List", extensionsList.toString());
}
for (Artifact artifact : artifacts) {
// TODO: the correct solution here would be to have an extension type, and to read
// the real extension values either from the artifact's manifest or some part of the POM
if ("jar".equals(artifact.getType())) {
String artifactId = artifact.getArtifactId().replace('.', '_');
String ename = artifactId + "-Extension-Name";
addManifestAttribute(m, entries, ename, artifact.getArtifactId());
String iname = artifactId + "-Implementation-Version";
addManifestAttribute(m, entries, iname, artifact.getVersion());
if (artifact.getRepository() != null) {
iname = artifactId + "-Implementation-URL";
String url = artifact.getRepository().getUrl() + "/" + artifact;
addManifestAttribute(m, entries, iname, url);
}
}
}
}
private void handleImplementationEntries(MavenProject project, Map<String, String> entries, Manifest m)
throws ManifestException {
addManifestAttribute(m, entries, "Implementation-Title", project.getName());
addManifestAttribute(m, entries, "Implementation-Version", project.getVersion());
if (project.getOrganization() != null) {
addManifestAttribute(
m,
entries,
"Implementation-Vendor",
project.getOrganization().getName());
}
}
private void handleSpecificationEntries(MavenProject project, Map<String, String> entries, Manifest m)
throws ManifestException {
addManifestAttribute(m, entries, "Specification-Title", project.getName());
try {
ArtifactVersion version = project.getArtifact().getSelectedVersion();
String specVersion = String.format("%s.%s", version.getMajorVersion(), version.getMinorVersion());
addManifestAttribute(m, entries, "Specification-Version", specVersion);
} catch (OverConstrainedVersionException e) {
throw new ManifestException("Failed to get selected artifact version to calculate"
+ " the specification version: " + e.getMessage());
}
if (project.getOrganization() != null) {
addManifestAttribute(
m,
entries,
"Specification-Vendor",
project.getOrganization().getName());
}
}
private void addCustomEntries(Manifest m, Map<String, String> entries, ManifestConfiguration config)
throws ManifestException {
/*
* TODO: rethink this, it wasn't working Artifact projectArtifact = project.getArtifact(); if (
* projectArtifact.isSnapshot() ) { Manifest.Attribute buildNumberAttr = new Manifest.Attribute( "Build-Number",
* "" + project.getSnapshotDeploymentBuildNumber() ); m.addConfiguredAttribute( buildNumberAttr ); }
*/
if (config.getPackageName() != null) {
addManifestAttribute(m, entries, "Package", config.getPackageName());
}
}
/**
* <p>Getter for the field <code>archiver</code>.</p>
*
* @return {@link org.codehaus.plexus.archiver.jar.JarArchiver}
*/
public JarArchiver getArchiver() {
return archiver;
}
/**
* <p>Setter for the field <code>archiver</code>.</p>
*
* @param archiver {@link org.codehaus.plexus.archiver.jar.JarArchiver}
*/
public void setArchiver(JarArchiver archiver) {
this.archiver = archiver;
}
/**
* <p>setOutputFile.</p>
*
* @param outputFile Set output file.
*/
public void setOutputFile(File outputFile) {
archiveFile = outputFile;
}
/**
* <p>createArchive.</p>
*
* @param session {@link org.apache.maven.execution.MavenSession}
* @param project {@link org.apache.maven.project.MavenProject}
* @param archiveConfiguration {@link org.apache.maven.archiver.MavenArchiveConfiguration}
* @throws org.codehaus.plexus.archiver.ArchiverException Archiver Exception.
* @throws org.codehaus.plexus.archiver.jar.ManifestException Manifest Exception.
* @throws java.io.IOException IO Exception.
* @throws org.apache.maven.artifact.DependencyResolutionRequiredException Dependency resolution exception.
*/
public void createArchive(
MavenSession session, MavenProject project, MavenArchiveConfiguration archiveConfiguration)
throws ManifestException, IOException, DependencyResolutionRequiredException {
// we have to clone the project instance so we can write out the pom with the deployment version,
// without impacting the main project instance...
MavenProject workingProject = project.clone();
boolean forced = archiveConfiguration.isForced();
if (archiveConfiguration.isAddMavenDescriptor()) {
// ----------------------------------------------------------------------
// We want to add the metadata for the project to the JAR in two forms:
//
// The first form is that of the POM itself. Applications that wish to
// access the POM for an artifact using maven tools they can.
//
// The second form is that of a properties file containing the basic
// top-level POM elements so that applications that wish to access
// POM information without the use of maven tools can do so.
// ----------------------------------------------------------------------
if (workingProject.getArtifact().isSnapshot()) {
workingProject.setVersion(workingProject.getArtifact().getVersion());
}
String groupId = workingProject.getGroupId();
String artifactId = workingProject.getArtifactId();
archiver.addFile(project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
// ----------------------------------------------------------------------
// Create pom.properties file
// ----------------------------------------------------------------------
File customPomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
File dir = new File(workingProject.getBuild().getDirectory(), "maven-archiver");
File pomPropertiesFile = new File(dir, "pom.properties");
new PomPropertiesUtil()
.createPomProperties(workingProject, archiver, customPomPropertiesFile, pomPropertiesFile, forced);
}
// ----------------------------------------------------------------------
// Create the manifest
// ----------------------------------------------------------------------
archiver.setMinimalDefaultManifest(true);
File manifestFile = archiveConfiguration.getManifestFile();
if (manifestFile != null) {
archiver.setManifest(manifestFile);
}
Manifest manifest = getManifest(session, workingProject, archiveConfiguration);
// Configure the jar
archiver.addConfiguredManifest(manifest);
archiver.setCompress(archiveConfiguration.isCompress());
archiver.setRecompressAddedZips(archiveConfiguration.isRecompressAddedZips());
archiver.setIndex(archiveConfiguration.isIndex());
archiver.setDestFile(archiveFile);
// make the archiver index the jars on the classpath, if we are adding that to the manifest
if (archiveConfiguration.getManifest().isAddClasspath()) {
List<String> artifacts = project.getRuntimeClasspathElements();
for (String artifact : artifacts) {
File f = new File(artifact);
archiver.addConfiguredIndexJars(f);
}
}
archiver.setForced(forced);
if (!archiveConfiguration.isForced() && archiver.isSupportingForced()) {
// TODO Should issue a warning here, but how do we get a logger?
// TODO getLog().warn(
// "Forced build is disabled, but disabling the forced mode isn't supported by the archiver." );
}
String automaticModuleName = manifest.getMainSection().getAttributeValue("Automatic-Module-Name");
if (automaticModuleName != null) {
if (!isValidModuleName(automaticModuleName)) {
throw new ManifestException("Invalid automatic module name: '" + automaticModuleName + "'");
}
}
// create archive
archiver.createArchive();
}
private void handleDefaultEntries(Manifest m, Map<String, String> entries) throws ManifestException {
String createdBy = this.createdBy;
if (createdBy == null) {
createdBy = createdBy(CREATED_BY, "org.apache.maven", "maven-archiver");
}
addManifestAttribute(m, entries, "Created-By", createdBy);
if (buildJdkSpecDefaultEntry) {
addManifestAttribute(m, entries, "Build-Jdk-Spec", System.getProperty("java.specification.version"));
}
}
private void handleBuildEnvironmentEntries(MavenSession session, Manifest m, Map<String, String> entries)
throws ManifestException {
addManifestAttribute(
m,
entries,
"Build-Tool",
session != null ? session.getSystemProperties().getProperty("maven.build.version") : "Apache Maven");
addManifestAttribute(
m,
entries,
"Build-Jdk",
String.format("%s (%s)", System.getProperty("java.version"), System.getProperty("java.vendor")));
addManifestAttribute(
m,
entries,
"Build-Os",
String.format(
"%s (%s; %s)",
System.getProperty("os.name"),
System.getProperty("os.version"),
System.getProperty("os.arch")));
}
private Artifact findArtifactWithFile(Set<Artifact> artifacts, File file) {
for (Artifact artifact : artifacts) {
// normally not null but we can check
if (artifact.getFile() != null) {
if (artifact.getFile().equals(file)) {
return artifact;
}
}
}
return null;
}
private static String getCreatedByVersion(String groupId, String artifactId) {
final Properties properties = loadOptionalProperties(MavenArchiver.class.getResourceAsStream(
"/META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties"));
return properties.getProperty("version");
}
private static Properties loadOptionalProperties(final InputStream inputStream) {
Properties properties = new Properties();
if (inputStream != null) {
try (InputStream in = inputStream) {
properties.load(in);
} catch (IllegalArgumentException | IOException ex) {
// ignore and return empty properties
}
}
return properties;
}
/**
* Define a value for "Created By" entry.
*
* @param description description of the plugin, like "Maven Source Plugin"
* @param groupId groupId where to get version in pom.properties
* @param artifactId artifactId where to get version in pom.properties
* @since 3.5.0
*/
public void setCreatedBy(String description, String groupId, String artifactId) {
createdBy = createdBy(description, groupId, artifactId);
}
private String createdBy(String description, String groupId, String artifactId) {
String createdBy = description;
String version = getCreatedByVersion(groupId, artifactId);
if (version != null) {
createdBy += " " + version;
}
return createdBy;
}
/**
* Add "Build-Jdk-Spec" entry as part of default manifest entries (true by default).
* For plugins whose output is not impacted by JDK release (like maven-source-plugin), adding
* Jdk spec adds unnecessary requirement on JDK version used at build to get reproducible result.
*
* @param buildJdkSpecDefaultEntry the value for "Build-Jdk-Spec" entry
* @since 3.5.0
*/
public void setBuildJdkSpecDefaultEntry(boolean buildJdkSpecDefaultEntry) {
this.buildJdkSpecDefaultEntry = buildJdkSpecDefaultEntry;
}
/**
* Parse output timestamp configured for Reproducible Builds' archive entries, either formatted as ISO 8601
* <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
* <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>.
*
* @param outputTimestamp the value of <code>${project.build.outputTimestamp}</code> (may be <code>null</code>)
* @return the parsed timestamp, may be <code>null</code> if <code>null</code> input or input contains only 1
* character
* @since 3.5.0
* @throws IllegalArgumentException if the outputTimestamp is neither ISO 8601 nor an integer, or it's not within
* the valid range 1980-01-01T00:00:02Z to 2099-12-31T23:59:59Z
* @deprecated Use {@link #parseBuildOutputTimestamp(String)} instead.
*/
@Deprecated
public Date parseOutputTimestamp(String outputTimestamp) {
return parseBuildOutputTimestamp(outputTimestamp).map(Date::from).orElse(null);
}
/**
* Configure Reproducible Builds archive creation if a timestamp is provided.
*
* @param outputTimestamp the value of {@code ${project.build.outputTimestamp}} (may be {@code null})
* @return the parsed timestamp as {@link java.util.Date}
* @since 3.5.0
* @see #parseOutputTimestamp
* @deprecated Use {@link #configureReproducibleBuild(String)} instead.
*/
@Deprecated
public Date configureReproducible(String outputTimestamp) {
configureReproducibleBuild(outputTimestamp);
return parseOutputTimestamp(outputTimestamp);
}
/**
* Parse output timestamp configured for Reproducible Builds' archive entries.
*
* <p>Either as {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME} or as a number representing seconds
* since the epoch (like <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
*
* @param outputTimestamp the value of {@code ${project.build.outputTimestamp}} (may be {@code null})
* @return the parsed timestamp as an {@code Optional<Instant>}, {@code empty} if input is {@code null} or input
* contains only 1 character (not a number)
* @since 3.6.0
* @throws IllegalArgumentException if the outputTimestamp is neither ISO 8601 nor an integer, or it's not within
* the valid range 1980-01-01T00:00:02Z to 2099-12-31T23:59:59Z
*/
public static Optional<Instant> parseBuildOutputTimestamp(String outputTimestamp) {
// Fail-fast on nulls
if (outputTimestamp == null) {
return Optional.empty();
}
// Number representing seconds since the epoch
if (isNumeric(outputTimestamp)) {
return Optional.of(Instant.ofEpochSecond(Long.parseLong(outputTimestamp)));
}
// no timestamp configured (1 character configuration is useful to override a full value during pom
// inheritance)
if (outputTimestamp.length() < 2) {
return Optional.empty();
}
try {
// Parse the date in UTC such as '2011-12-03T10:15:30Z' or with an offset '2019-10-05T20:37:42+06:00'.
final Instant date = OffsetDateTime.parse(outputTimestamp)
.withOffsetSameInstant(ZoneOffset.UTC)
.truncatedTo(ChronoUnit.SECONDS)
.toInstant();
if (date.isBefore(DATE_MIN) || date.isAfter(DATE_MAX)) {
throw new IllegalArgumentException(
"'" + date + "' is not within the valid range " + DATE_MIN + " to " + DATE_MAX);
}
return Optional.of(date);
} catch (DateTimeParseException pe) {
throw new IllegalArgumentException(
"Invalid project.build.outputTimestamp value '" + outputTimestamp + "'", pe);
}
}
private static boolean isNumeric(String str) {
if (str.isEmpty()) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
/**
* Configure Reproducible Builds archive creation if a timestamp is provided.
*
* @param outputTimestamp the value of {@code project.build.outputTimestamp} (may be {@code null})
* @since 3.6.0
* @see #parseBuildOutputTimestamp(String)
*/
public void configureReproducibleBuild(String outputTimestamp) {
parseBuildOutputTimestamp(outputTimestamp).map(FileTime::from).ifPresent(modifiedTime -> getArchiver()
.configureReproducibleBuild(modifiedTime));
}
}