Skip to content

Commit ed02d02

Browse files
committed
Polish "Allow repackage maven goal to take a source classifier"
Closes gh-11061
1 parent 3c8e012 commit ed02d02

File tree

11 files changed

+216
-49
lines changed

11 files changed

+216
-49
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ assertTrue 'backup file should exist', backup.exists()
1313

1414
def file = new File(basedir, "build.log")
1515
assertTrue 'main artifact should have been updated',
16-
file.text.contains("Updating main artifact " + main + " to " + backup)
16+
file.text.contains("Updating artifact " + main + " to " + backup)
1717
return file.text.contains ("Installing "+backup)
1818

1919

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals=clean install
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>jar-classifier-main</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>@java.version@</maven.compiler.source>
11+
<maven.compiler.target>@java.version@</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>@project.groupId@</groupId>
17+
<artifactId>@project.artifactId@</artifactId>
18+
<version>@project.version@</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>repackage</goal>
23+
</goals>
24+
<configuration>
25+
<classifier>test</classifier>
26+
</configuration>
27+
</execution>
28+
</executions>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-jar-plugin</artifactId>
33+
<version>@maven-jar-plugin.version@</version>
34+
<configuration>
35+
<archive>
36+
<manifestEntries>
37+
<Not-Used>Foo</Not-Used>
38+
</manifestEntries>
39+
</archive>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-context</artifactId>
48+
<version>@spring.version@</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>javax.servlet</groupId>
52+
<artifactId>javax.servlet-api</artifactId>
53+
<version>@servlet-api.version@</version>
54+
<scope>provided</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,13 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import java.io.*;
18-
import org.springframework.boot.maven.*;
17+
package org.test;
1918

20-
File f = new File(basedir, "target/jar-repackage-classifier-0.0.1.BUILD-SNAPSHOT-test.jar");
21-
new Verify.JarArchiveVerification(f, Verify.SAMPLE_APP) {
22-
@Override
23-
protected void verifyZipEntries(Verify.ArchiveVerifier verifier) throws Exception {
24-
super.verifyZipEntries(verifier)
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
2522
}
26-
}.verify();
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
import org.springframework.boot.maven.*;
3+
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertFalse;
6+
7+
File repackaged = new File(basedir, "target/jar-classifier-main-0.0.1.BUILD-SNAPSHOT-test.jar")
8+
new Verify.JarArchiveVerification(repackaged, Verify.SAMPLE_APP).verify();
9+
10+
File main = new File( basedir, "target/jar-classifier-main-0.0.1.BUILD-SNAPSHOT.jar")
11+
assertTrue 'main artifact should exist', main.exists()
12+
13+
File backup = new File( basedir, "target/jar-classifier-main-0.0.1.BUILD-SNAPSHOT.jar.original")
14+
assertFalse 'backup artifact should not exist', backup.exists()
15+
16+
def file = new File(basedir, "build.log")
17+
assertTrue 'repackaged artifact should have been attached', file.text.contains("Attaching archive " + repackaged)
18+
assertTrue 'main artifact should have been installed', file.text.contains ("Installing "+main)
19+
assertTrue 'repackaged artifact should have been installed', file.text.contains ("Installing "+repackaged)
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals=clean install
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.springframework.boot.maven.it</groupId>
6-
<artifactId>jar-repackage-classifier</artifactId>
6+
<artifactId>jar-classifier-source</artifactId>
77
<version>0.0.1.BUILD-SNAPSHOT</version>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.io.*;
18+
import org.springframework.boot.maven.*
19+
20+
import static org.junit.Assert.assertFalse
21+
import static org.junit.Assert.assertTrue;
22+
23+
File repackaged = new File(basedir, "target/jar-classifier-source-0.0.1.BUILD-SNAPSHOT-test.jar");
24+
new Verify.JarArchiveVerification(repackaged, Verify.SAMPLE_APP).verify();
25+
26+
File backup = new File( basedir, "target/jar-classifier-source-0.0.1.BUILD-SNAPSHOT-test.jar.original")
27+
assertTrue 'backup artifact should exist', backup.exists()
28+
29+
def file = new File(basedir, "build.log")
30+
assertTrue 'repackaged artifact should have been replaced', file.text.contains("Replacing artifact with classifier test " + repackaged)
31+
assertFalse 'backup artifact should not have been installed', file.text.contains ("Installing "+backup)
32+
assertTrue 'repackaged artifact should have been installed', file.text.contains ("Installing "+repackaged)
33+

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
9898
private boolean skip;
9999

100100
/**
101-
* Classifier to add to the artifact generated. If given, the artifact will be
102-
* attached with that classifier and the main artifact will be deployed as the main
103-
* artifact. If an artifact with the classifier already exists, it will be used as source.
104-
* If a classifier is not given (default), it will replace the main artifact and
105-
* only the repackaged artifact will be deployed. Attaching the artifact allows to
106-
* deploy it alongside to the original one, see <a href=
101+
* Classifier to add to the repackaged archive. If not given, the main artifact will
102+
* be replaced by the repackaged archive. If given, the classifier will also be used
103+
* to determine the source archive to repackage: if an artifact with that classifier
104+
* already exists, it will be used as source and replaced. If no such artifact exists,
105+
* the main artifact will be used as source and the repackaged archive will be
106+
* attached as a supplemental artifact with that classifier. Attaching the artifact
107+
* allows to deploy it alongside to the original one, see <a href=
107108
* "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
108109
* > the maven documentation for more details</a>.
109110
* @since 1.0
@@ -210,9 +211,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
210211
}
211212

212213
private void repackage() throws MojoExecutionException {
213-
File source = getSourceFile();
214+
Artifact source = getSourceArtifact();
214215
File target = getTargetFile();
215-
Repackager repackager = getRepackager(source);
216+
Repackager repackager = getRepackager(source.getFile());
216217
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
217218
getFilters(getAdditionalFilters()));
218219
Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack,
@@ -227,19 +228,26 @@ private void repackage() throws MojoExecutionException {
227228
updateArtifact(source, target, repackager.getBackupFile());
228229
}
229230

230-
private File getSourceFile() {
231-
Artifact sourceArtifact = this.project.getArtifact();
231+
/**
232+
* Return the source {@link Artifact} to repackage. If a classifier is specified
233+
* and an artifact with that classifier exists, it is used. Otherwise, the main
234+
* artifact is used.
235+
* @return the source artifact to repackage
236+
*/
237+
private Artifact getSourceArtifact() {
238+
Artifact sourceArtifact = getArtifact(this.classifier);
239+
return (sourceArtifact != null ? sourceArtifact : this.project.getArtifact());
240+
}
232241

233-
if (this.classifier != null) {
242+
private Artifact getArtifact(String classifier) {
243+
if (classifier != null) {
234244
for (Artifact attachedArtifact : this.project.getAttachedArtifacts()) {
235-
if (this.classifier.equals(attachedArtifact.getClassifier())) {
236-
sourceArtifact = attachedArtifact;
237-
break;
245+
if (classifier.equals(attachedArtifact.getClassifier())) {
246+
return attachedArtifact;
238247
}
239248
}
240249
}
241-
242-
return sourceArtifact.getFile();
250+
return null;
243251
}
244252

245253
private File getTargetFile() {
@@ -250,18 +258,10 @@ private File getTargetFile() {
250258
if (!this.outputDirectory.exists()) {
251259
this.outputDirectory.mkdirs();
252260
}
253-
return new File(this.outputDirectory, this.finalName + getClassifier() + "."
261+
return new File(this.outputDirectory, this.finalName + classifier + "."
254262
+ this.project.getArtifact().getArtifactHandler().getExtension());
255263
}
256264

257-
private String getClassifier() {
258-
String classifier = (this.classifier == null ? "" : this.classifier.trim());
259-
if (classifier.length() > 0 && !classifier.startsWith("-")) {
260-
classifier = "-" + classifier;
261-
}
262-
return classifier;
263-
}
264-
265265
private Repackager getRepackager(File source) {
266266
Repackager repackager = new Repackager(source, this.layoutFactory);
267267
repackager.addMainClassTimeoutWarningListener(
@@ -327,26 +327,28 @@ private void putIfMissing(Properties properties, String key,
327327
}
328328
}
329329

330-
private void updateArtifact(File source, File repackaged, File original) {
330+
private void updateArtifact(Artifact source, File target, File original) {
331331
if (this.attach) {
332-
attachArtifact(source, repackaged);
332+
attachArtifact(source, target);
333333
}
334-
else if (source.equals(repackaged)) {
334+
else if (source.getFile().equals(target)) {
335+
getLog().info("Updating artifact " + source.getFile() + " to " + original);
335336
this.project.getArtifact().setFile(original);
336-
getLog().info("Updating main artifact " + source + " to " + original);
337337
}
338338
}
339339

340-
private void attachArtifact(File source, File repackaged) {
341-
if (this.classifier != null) {
342-
getLog().info("Attaching archive: " + repackaged + ", with classifier: "
340+
private void attachArtifact(Artifact source, File target) {
341+
if (this.classifier != null && !source.getFile().equals(target)) {
342+
getLog().info("Attaching archive " + target + " with classifier "
343343
+ this.classifier);
344344
this.projectHelper.attachArtifact(this.project, this.project.getPackaging(),
345-
this.classifier, repackaged);
345+
this.classifier, target);
346346
}
347-
else if (!source.equals(repackaged)) {
348-
this.project.getArtifact().setFile(repackaged);
349-
getLog().info("Replacing main artifact " + source + " to " + repackaged);
347+
else {
348+
String artifactId = this.classifier != null
349+
? "artifact with classifier " + this.classifier : "main artifact";
350+
getLog().info(String.format("Replacing %s %s", artifactId, source.getFile()));
351+
source.setFile(target);
350352
}
351353
}
352354

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-classifier.apt.vm

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,64 @@
4747
</project>
4848
---
4949

50-
This configuration will generate two artifacts: the original one and the repackaged counter
51-
part produced by the repackage goal. Both will be installed/deployed transparently.
50+
This configuration will generate two artifacts: the original one and the repackaged
51+
counter part produced by the repackage goal. Both will be installed/deployed
52+
transparently.
5253

54+
You can also use the same configuration if you want to repackage a secondary artifact
55+
the same way the main artifact is replaced. The following configuration installs/deploys
56+
a single <<<task>>> classified artifact with the repackaged app:
57+
58+
---
59+
<project>
60+
...
61+
<build>
62+
...
63+
<plugins>
64+
...
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-jar-plugin</artifactId>
68+
<version>@maven-jar-plugin.version@</version>
69+
<executions>
70+
<execution>
71+
<goals>
72+
<goal>jar</goal>
73+
</goals>
74+
<phase>package</phase>
75+
<configuration>
76+
<classifier>task</classifier>
77+
</configuration>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
<plugin>
82+
<groupId>${project.groupId}</groupId>
83+
<artifactId>${project.artifactId}</artifactId>
84+
<version>${project.version}</version>
85+
<executions>
86+
<execution>
87+
<goals>
88+
<goal>repackage</goal>
89+
</goals>
90+
<configuration>
91+
<classifier>task</classifier>
92+
</configuration>
93+
</execution>
94+
</executions>
95+
...
96+
</plugin>
97+
...
98+
</plugins>
99+
...
100+
</build>
101+
...
102+
</project>
103+
---
104+
105+
As both the <<<maven-jar-plugin>>> and the <<<spring-boot-maven-plugin>>> runs at the
106+
same phase, it is important that the jar plugin is defined first (so that it runs before
107+
the repackage goal).
53108

54109

55110

0 commit comments

Comments
 (0)