diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml new file mode 100644 index 000000000000..e1e829462e40 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + org.springframework.boot.maven.it + jar-repackage-classifier + 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-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/src/main/java/org/test/SampleApplication.java new file mode 100644 index 000000000000..e8784d4593d1 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/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-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy new file mode 100644 index 000000000000..8d5e781d774b --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy @@ -0,0 +1,26 @@ +/* + * 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.*; + +File f = new File(basedir, "target/jar-repackage-classifier-0.0.1.BUILD-SNAPSHOT-test.jar"); +new Verify.JarArchiveVerification(f, Verify.SAMPLE_APP) { + @Override + protected void verifyZipEntries(Verify.ArchiveVerifier verifier) throws Exception { + super.verifyZipEntries(verifier) + } +}.verify(); diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java index 9a51fbefa655..a39c2725a03f 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java @@ -23,6 +23,7 @@ import java.util.Properties; import java.util.Set; +import org.apache.commons.io.FilenameUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; @@ -54,6 +55,7 @@ * @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 { @@ -96,7 +98,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { /** * 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 + * artifact. If an artifact with the classifier already exists, it will be used as source. + * If a classifier 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 artifacts = filterDependencies(this.project.getArtifacts(), @@ -223,18 +226,35 @@ private void repackage() throws MojoExecutionException { updateArtifact(source, target, repackager.getBackupFile()); } - private File getTargetFile() { - String classifier = (this.classifier == null ? "" : this.classifier.trim()); - if (classifier.length() > 0 && !classifier.startsWith("-")) { - classifier = "-" + classifier; + private File getSourceFile() { + File source = this.project.getArtifact().getFile(); + if (this.classifier != null) { + File sourceWithClassifier = new File(source.getParent(), + FilenameUtils.getBaseName(source.getName()) + getClassifier() + "." + + FilenameUtils.getExtension(source.getName())); + if (sourceWithClassifier.exists()) { + source = sourceWithClassifier; + } } + return source; + } + + private File getTargetFile() { if (!this.outputDirectory.exists()) { this.outputDirectory.mkdirs(); } - return new File(this.outputDirectory, this.finalName + classifier + "." + return new File(this.outputDirectory, this.finalName + getClassifier() + "." + this.project.getArtifact().getArtifactHandler().getExtension()); } + private String getClassifier() { + String classifier = (this.classifier == null ? "" : this.classifier.trim()); + if (classifier.length() > 0 && !classifier.startsWith("-")) { + classifier = "-" + classifier; + } + return classifier; + } + private Repackager getRepackager(File source) { Repackager repackager = new Repackager(source, this.layoutFactory); repackager.addMainClassTimeoutWarningListener(