Skip to content

Commit e397466

Browse files
bjornlindstromaxtavt
authored andcommitted
Change the maven plugin to allow use a source with classifier. Fixes spring-projectsgh-7659.
1 parent a4ddbcf commit e397466

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"
@@ -205,7 +208,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
205208
}
206209

207210
private void repackage() throws MojoExecutionException {
208-
File source = this.project.getArtifact().getFile();
211+
File source = getSourceFile();
209212
File target = getTargetFile();
210213
Repackager repackager = getRepackager(source);
211214
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
@@ -222,18 +225,35 @@ private void repackage() throws MojoExecutionException {
222225
updateArtifact(source, target, repackager.getBackupFile());
223226
}
224227

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

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

0 commit comments

Comments
 (0)