Skip to content

Commit e38bb31

Browse files
Change the maven plugin to allow use a source with classifier. Fixes spring-projectsgh-7659.
1 parent 5351ceb commit e38bb31

File tree

4 files changed

+143
-7
lines changed

4 files changed

+143
-7
lines changed
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-repackage-classifier</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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
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)
25+
}
26+
}.verify();

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Properties;
2424
import java.util.Set;
2525

26+
import org.apache.commons.io.FilenameUtils;
2627
import org.apache.maven.artifact.Artifact;
2728
import org.apache.maven.model.Dependency;
2829
import org.apache.maven.plugin.MojoExecutionException;
@@ -54,6 +55,7 @@
5455
* @author Phillip Webb
5556
* @author Dave Syer
5657
* @author Stephane Nicoll
58+
* @author Björn Lindström
5759
*/
5860
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
5961
public class RepackageMojo extends AbstractDependencyFilterMojo {
@@ -96,7 +98,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
9698
/**
9799
* Classifier to add to the artifact generated. If given, the artifact will be
98100
* attached with that classifier and the main artifact will be deployed as the main
99-
* artifact. If this is not given (default), it will replace the main artifact and
101+
* artifact. If an artifact with the classifier already exists, it will be used as source.
102+
* If a classifier is not given (default), it will replace the main artifact and
100103
* only the repackaged artifact will be deployed. Attaching the artifact allows to
101104
* deploy it alongside to the original one, see <a href=
102105
* "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
@@ -206,7 +209,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
206209
}
207210

208211
private void repackage() throws MojoExecutionException {
209-
File source = this.project.getArtifact().getFile();
212+
File source = getSourceFile();
210213
File target = getTargetFile();
211214
Repackager repackager = getRepackager(source);
212215
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
@@ -223,18 +226,35 @@ private void repackage() throws MojoExecutionException {
223226
updateArtifact(source, target, repackager.getBackupFile());
224227
}
225228

226-
private File getTargetFile() {
227-
String classifier = (this.classifier == null ? "" : this.classifier.trim());
228-
if (classifier.length() > 0 && !classifier.startsWith("-")) {
229-
classifier = "-" + classifier;
229+
private File getSourceFile() {
230+
File source = this.project.getArtifact().getFile();
231+
if (this.classifier != null) {
232+
File sourceWithClassifier = new File(source.getParent(),
233+
FilenameUtils.getBaseName(source.getName()) + getClassifier() + "."
234+
+ FilenameUtils.getExtension(source.getName()));
235+
if (sourceWithClassifier.exists()) {
236+
source = sourceWithClassifier;
237+
}
230238
}
239+
return source;
240+
}
241+
242+
private File getTargetFile() {
231243
if (!this.outputDirectory.exists()) {
232244
this.outputDirectory.mkdirs();
233245
}
234-
return new File(this.outputDirectory, this.finalName + classifier + "."
246+
return new File(this.outputDirectory, this.finalName + getClassifier() + "."
235247
+ this.project.getArtifact().getArtifactHandler().getExtension());
236248
}
237249

250+
private String getClassifier() {
251+
String classifier = (this.classifier == null ? "" : this.classifier.trim());
252+
if (classifier.length() > 0 && !classifier.startsWith("-")) {
253+
classifier = "-" + classifier;
254+
}
255+
return classifier;
256+
}
257+
238258
private Repackager getRepackager(File source) {
239259
Repackager repackager = new Repackager(source, this.layoutFactory);
240260
repackager.addMainClassTimeoutWarningListener(

0 commit comments

Comments
 (0)