diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy
index f7fde1c94c..676d04c4b6 100644
--- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy
@@ -13,7 +13,7 @@ assertTrue 'backup file should exist', backup.exists()
def file = new File(basedir, "build.log")
assertTrue 'main artifact should have been updated',
- file.text.contains("Updating main artifact " + main + " to " + backup)
+ file.text.contains("Updating artifact " + main + " to " + backup)
return file.text.contains ("Installing "+backup)
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/invoker.properties b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/invoker.properties
new file mode 100644
index 0000000000..c0c3f7cc07
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/invoker.properties
@@ -0,0 +1 @@
+invoker.goals=clean install
\ No newline at end of file
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/pom.xml
new file mode 100644
index 0000000000..6dd361c02b
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/pom.xml
@@ -0,0 +1,57 @@
+
+
+ 4.0.0
+ org.springframework.boot.maven.it
+ jar-classifier-main
+ 0.0.1.BUILD-SNAPSHOT
+
+ UTF-8
+ @java.version@
+ @java.version@
+
+
+
+
+ @project.groupId@
+ @project.artifactId@
+ @project.version@
+
+
+
+ repackage
+
+
+ test
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+ @maven-jar-plugin.version@
+
+
+
+ Foo
+
+
+
+
+
+
+
+
+ org.springframework
+ spring-context
+ @spring.version@
+
+
+ javax.servlet
+ javax.servlet-api
+ @servlet-api.version@
+ provided
+
+
+
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/src/main/java/org/test/SampleApplication.java
new file mode 100644
index 0000000000..8a5b669129
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/src/main/java/org/test/SampleApplication.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2012-2017 the original author or authors.
+ *
+ * Licensed 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.test;
+
+public class SampleApplication {
+
+ public static void main(String[] args) {
+ }
+
+}
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/verify.groovy
new file mode 100644
index 0000000000..c42623c3eb
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/verify.groovy
@@ -0,0 +1,20 @@
+import java.io.*;
+import org.springframework.boot.maven.*;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+File repackaged = new File(basedir, "target/jar-classifier-main-0.0.1.BUILD-SNAPSHOT-test.jar")
+new Verify.JarArchiveVerification(repackaged, Verify.SAMPLE_APP).verify();
+
+File main = new File( basedir, "target/jar-classifier-main-0.0.1.BUILD-SNAPSHOT.jar")
+assertTrue 'main artifact should exist', main.exists()
+
+File backup = new File( basedir, "target/jar-classifier-main-0.0.1.BUILD-SNAPSHOT.jar.original")
+assertFalse 'backup artifact should not exist', backup.exists()
+
+def file = new File(basedir, "build.log")
+assertTrue 'repackaged artifact should have been attached', file.text.contains("Attaching archive " + repackaged)
+assertTrue 'main artifact should have been installed', file.text.contains ("Installing "+main)
+assertTrue 'repackaged artifact should have been installed', file.text.contains ("Installing "+repackaged)
+
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/invoker.properties b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/invoker.properties
new file mode 100644
index 0000000000..c0c3f7cc07
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/invoker.properties
@@ -0,0 +1 @@
+invoker.goals=clean install
\ No newline at end of file
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/pom.xml
new file mode 100644
index 0000000000..ad8143b793
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/pom.xml
@@ -0,0 +1,66 @@
+
+
+ 4.0.0
+ org.springframework.boot.maven.it
+ jar-classifier-source
+ 0.0.1.BUILD-SNAPSHOT
+
+ UTF-8
+ @java.version@
+ @java.version@
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+ @maven-jar-plugin.version@
+
+
+
+ jar
+
+ package
+
+ test
+
+
+ Foo
+
+
+
+
+
+
+
+ @project.groupId@
+ @project.artifactId@
+ @project.version@
+
+
+
+ repackage
+
+
+ test
+
+
+
+
+
+
+
+
+ org.springframework
+ spring-context
+ @spring.version@
+
+
+ javax.servlet
+ javax.servlet-api
+ @servlet-api.version@
+ provided
+
+
+
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/src/main/java/org/test/SampleApplication.java
new file mode 100644
index 0000000000..e8784d4593
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/src/main/java/org/test/SampleApplication.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2012-2014 the original author or authors.
+ *
+ * Licensed 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.test;
+
+public class SampleApplication {
+
+ public static void main(String[] args) {
+ }
+
+}
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/verify.groovy
new file mode 100644
index 0000000000..7ee8a990cc
--- /dev/null
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/verify.groovy
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2012-2014 the original author or authors.
+ *
+ * Licensed 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.
+ */
+
+import java.io.*;
+import org.springframework.boot.maven.*
+
+import static org.junit.Assert.assertFalse
+import static org.junit.Assert.assertTrue;
+
+File repackaged = new File(basedir, "target/jar-classifier-source-0.0.1.BUILD-SNAPSHOT-test.jar");
+new Verify.JarArchiveVerification(repackaged, Verify.SAMPLE_APP).verify();
+
+File backup = new File( basedir, "target/jar-classifier-source-0.0.1.BUILD-SNAPSHOT-test.jar.original")
+assertTrue 'backup artifact should exist', backup.exists()
+
+def file = new File(basedir, "build.log")
+assertTrue 'repackaged artifact should have been replaced', file.text.contains("Replacing artifact with classifier test " + repackaged)
+assertFalse 'backup artifact should not have been installed', file.text.contains ("Installing "+backup)
+assertTrue 'repackaged artifact should have been installed', file.text.contains ("Installing "+repackaged)
+
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java
index d0a2289d98..f43fe800ad 100644
--- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java
@@ -57,6 +57,7 @@ import org.springframework.boot.loader.tools.Repackager.MainClassTimeoutWarningL
* @author Phillip Webb
* @author Dave Syer
* @author Stephane Nicoll
+ * @author Björn Lindström
*/
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class RepackageMojo extends AbstractDependencyFilterMojo {
@@ -97,11 +98,13 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
private boolean skip;
/**
- * Classifier to add to the artifact generated. If given, the artifact will be
- * attached with that classifier and the main artifact will be deployed as the main
- * artifact. If this is not given (default), it will replace the main artifact and
- * only the repackaged artifact will be deployed. Attaching the artifact allows to
- * deploy it alongside to the original one, see the maven documentation for more details.
* @since 1.0
@@ -208,9 +211,9 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
}
private void repackage() throws MojoExecutionException {
- File source = this.project.getArtifact().getFile();
+ Artifact source = getSourceArtifact();
File target = getTargetFile();
- Repackager repackager = getRepackager(source);
+ Repackager repackager = getRepackager(source.getFile());
Set artifacts = filterDependencies(this.project.getArtifacts(),
getFilters(getAdditionalFilters()));
Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack,
@@ -225,6 +228,28 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
updateArtifact(source, target, repackager.getBackupFile());
}
+ /**
+ * Return the source {@link Artifact} to repackage. If a classifier is specified
+ * and an artifact with that classifier exists, it is used. Otherwise, the main
+ * artifact is used.
+ * @return the source artifact to repackage
+ */
+ private Artifact getSourceArtifact() {
+ Artifact sourceArtifact = getArtifact(this.classifier);
+ return (sourceArtifact != null ? sourceArtifact : this.project.getArtifact());
+ }
+
+ private Artifact getArtifact(String classifier) {
+ if (classifier != null) {
+ for (Artifact attachedArtifact : this.project.getAttachedArtifacts()) {
+ if (classifier.equals(attachedArtifact.getClassifier())) {
+ return attachedArtifact;
+ }
+ }
+ }
+ return null;
+ }
+
private File getTargetFile() {
String classifier = (this.classifier != null ? this.classifier.trim() : "");
if (!classifier.isEmpty() && !classifier.startsWith("-")) {
@@ -302,26 +327,28 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
}
}
- private void updateArtifact(File source, File repackaged, File original) {
+ private void updateArtifact(Artifact source, File target, File original) {
if (this.attach) {
- attachArtifact(source, repackaged);
+ attachArtifact(source, target);
}
- else if (source.equals(repackaged)) {
+ else if (source.getFile().equals(target)) {
+ getLog().info("Updating artifact " + source.getFile() + " to " + original);
this.project.getArtifact().setFile(original);
- getLog().info("Updating main artifact " + source + " to " + original);
}
}
- private void attachArtifact(File source, File repackaged) {
- if (this.classifier != null) {
- getLog().info("Attaching archive: " + repackaged + ", with classifier: "
+ private void attachArtifact(Artifact source, File target) {
+ if (this.classifier != null && !source.getFile().equals(target)) {
+ getLog().info("Attaching archive " + target + " with classifier "
+ this.classifier);
this.projectHelper.attachArtifact(this.project, this.project.getPackaging(),
- this.classifier, repackaged);
+ this.classifier, target);
}
- else if (!source.equals(repackaged)) {
- this.project.getArtifact().setFile(repackaged);
- getLog().info("Replacing main artifact " + source + " to " + repackaged);
+ else {
+ String artifactId = this.classifier != null
+ ? "artifact with classifier " + this.classifier : "main artifact";
+ getLog().info(String.format("Replacing %s %s", artifactId, source.getFile()));
+ source.setFile(target);
}
}
diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-classifier.apt.vm b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-classifier.apt.vm
index d990679282..3d2fee3457 100644
--- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-classifier.apt.vm
+++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-classifier.apt.vm
@@ -47,9 +47,64 @@
---
- This configuration will generate two artifacts: the original one and the repackaged counter
- part produced by the repackage goal. Both will be installed/deployed transparently.
+ This configuration will generate two artifacts: the original one and the repackaged
+ counter part produced by the repackage goal. Both will be installed/deployed
+ transparently.
+ You can also use the same configuration if you want to repackage a secondary artifact
+ the same way the main artifact is replaced. The following configuration installs/deploys
+ a single <<>> classified artifact with the repackaged app:
+
+---
+
+ ...
+
+ ...
+
+ ...
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+ @maven-jar-plugin.version@
+
+
+
+ jar
+
+ package
+
+ task
+
+
+
+
+
+ ${project.groupId}
+ ${project.artifactId}
+ ${project.version}
+
+
+
+ repackage
+
+
+ task
+
+
+
+ ...
+
+ ...
+
+ ...
+
+ ...
+
+---
+
+ As both the <<>> and the <<>> runs at the
+ same phase, it is important that the jar plugin is defined first (so that it runs before
+ the repackage goal).