From 3c8e012411ecc453f9dc4e035b92c39c89885896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Thu, 10 Aug 2017 14:37:51 +0200 Subject: [PATCH] Allow repackage maven goal to take a source classifier Previously, the "classifier" attribute was only used to determine the target classifier of the repackaged archive, always using the main artifact as the source. This commit changes the semantic of the attribute so that an existing archive matching the "classifier" attribute can be used as source, replacing the archive the same way the goal replaces the main archive if no classifier is found. If no artifact with the specified classifier exists, the repackaged archive is still processed based on the main archive and attached to the lifecycle using the value of the classifier attribute. See gh-11061 --- .../src/it/jar-repackage-classifier/pom.xml | 66 +++++++++++++++++++ .../main/java/org/test/SampleApplication.java | 24 +++++++ .../it/jar-repackage-classifier/verify.groovy | 26 ++++++++ .../boot/maven/RepackageMojo.java | 31 ++++++++- 4 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/src/main/java/org/test/SampleApplication.java create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml new file mode 100644 index 0000000000..e1e829462e --- /dev/null +++ b/spring-boot-project/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-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/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-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-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy new file mode 100644 index 0000000000..8d5e781d77 --- /dev/null +++ b/spring-boot-project/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-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..9ea06837b3 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 { @@ -99,7 +100,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(), @@ -225,6 +227,21 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { updateArtifact(source, target, repackager.getBackupFile()); } + private File getSourceFile() { + Artifact sourceArtifact = this.project.getArtifact(); + + if (this.classifier != null) { + for (Artifact attachedArtifact : this.project.getAttachedArtifacts()) { + if (this.classifier.equals(attachedArtifact.getClassifier())) { + sourceArtifact = attachedArtifact; + break; + } + } + } + + return sourceArtifact.getFile(); + } + private File getTargetFile() { String classifier = (this.classifier != null ? this.classifier.trim() : ""); if (!classifier.isEmpty() && !classifier.startsWith("-")) { @@ -233,10 +250,18 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { 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(