Skip to content

Commit d84cb44

Browse files
committed
Merge pull request #11061 from axtavt:gh-7659
* pr/11061: Polish "Allow repackage maven goal to take a source classifier" Allow repackage maven goal to take a source classifier
2 parents 08a12e7 + ed02d02 commit d84cb44

File tree

11 files changed

+329
-21
lines changed

11 files changed

+329
-21
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
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2017 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+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
22+
}
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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-source</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>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-jar-plugin</artifactId>
18+
<version>@maven-jar-plugin.version@</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>jar</goal>
23+
</goals>
24+
<phase>package</phase>
25+
<configuration>
26+
<classifier>test</classifier>
27+
<archive>
28+
<manifestEntries>
29+
<Not-Used>Foo</Not-Used>
30+
</manifestEntries>
31+
</archive>
32+
</configuration>
33+
</execution>
34+
</executions>
35+
</plugin>
36+
<plugin>
37+
<groupId>@project.groupId@</groupId>
38+
<artifactId>@project.artifactId@</artifactId>
39+
<version>@project.version@</version>
40+
<executions>
41+
<execution>
42+
<goals>
43+
<goal>repackage</goal>
44+
</goals>
45+
<configuration>
46+
<classifier>test</classifier>
47+
</configuration>
48+
</execution>
49+
</executions>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
<dependencies>
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-context</artifactId>
57+
<version>@spring.version@</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>javax.servlet</groupId>
61+
<artifactId>javax.servlet-api</artifactId>
62+
<version>@servlet-api.version@</version>
63+
<scope>provided</scope>
64+
</dependency>
65+
</dependencies>
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
22+
}
23+
24+
}
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: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Phillip Webb
5858
* @author Dave Syer
5959
* @author Stephane Nicoll
60+
* @author Björn Lindström
6061
*/
6162
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
6263
public class RepackageMojo extends AbstractDependencyFilterMojo {
@@ -97,11 +98,13 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
9798
private boolean skip;
9899

99100
/**
100-
* Classifier to add to the artifact generated. If given, the artifact will be
101-
* attached with that classifier and the main artifact will be deployed as the main
102-
* artifact. If this is not given (default), it will replace the main artifact and
103-
* only the repackaged artifact will be deployed. Attaching the artifact allows to
104-
* 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=
105108
* "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
106109
* > the maven documentation for more details</a>.
107110
* @since 1.0
@@ -208,9 +211,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
208211
}
209212

210213
private void repackage() throws MojoExecutionException {
211-
File source = this.project.getArtifact().getFile();
214+
Artifact source = getSourceArtifact();
212215
File target = getTargetFile();
213-
Repackager repackager = getRepackager(source);
216+
Repackager repackager = getRepackager(source.getFile());
214217
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
215218
getFilters(getAdditionalFilters()));
216219
Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack,
@@ -225,6 +228,28 @@ private void repackage() throws MojoExecutionException {
225228
updateArtifact(source, target, repackager.getBackupFile());
226229
}
227230

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+
}
241+
242+
private Artifact getArtifact(String classifier) {
243+
if (classifier != null) {
244+
for (Artifact attachedArtifact : this.project.getAttachedArtifacts()) {
245+
if (classifier.equals(attachedArtifact.getClassifier())) {
246+
return attachedArtifact;
247+
}
248+
}
249+
}
250+
return null;
251+
}
252+
228253
private File getTargetFile() {
229254
String classifier = (this.classifier != null ? this.classifier.trim() : "");
230255
if (!classifier.isEmpty() && !classifier.startsWith("-")) {
@@ -302,26 +327,28 @@ private void putIfMissing(Properties properties, String key,
302327
}
303328
}
304329

305-
private void updateArtifact(File source, File repackaged, File original) {
330+
private void updateArtifact(Artifact source, File target, File original) {
306331
if (this.attach) {
307-
attachArtifact(source, repackaged);
332+
attachArtifact(source, target);
308333
}
309-
else if (source.equals(repackaged)) {
334+
else if (source.getFile().equals(target)) {
335+
getLog().info("Updating artifact " + source.getFile() + " to " + original);
310336
this.project.getArtifact().setFile(original);
311-
getLog().info("Updating main artifact " + source + " to " + original);
312337
}
313338
}
314339

315-
private void attachArtifact(File source, File repackaged) {
316-
if (this.classifier != null) {
317-
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 "
318343
+ this.classifier);
319344
this.projectHelper.attachArtifact(this.project, this.project.getPackaging(),
320-
this.classifier, repackaged);
345+
this.classifier, target);
321346
}
322-
else if (!source.equals(repackaged)) {
323-
this.project.getArtifact().setFile(repackaged);
324-
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);
325352
}
326353
}
327354

0 commit comments

Comments
 (0)