From ed02d02d19abe91f4145e4fdb92648d19efb052c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 15 May 2018 14:47:18 +0200 Subject: [PATCH] Polish "Allow repackage maven goal to take a source classifier" Closes gh-11061 --- .../src/it/jar-attach-disabled/verify.groovy | 2 +- .../it/jar-classifier-main/invoker.properties | 1 + .../src/it/jar-classifier-main/pom.xml | 57 ++++++++++++++ .../java/org/test/SampleApplication.java} | 16 ++-- .../src/it/jar-classifier-main/verify.groovy | 20 +++++ .../jar-classifier-source/invoker.properties | 1 + .../pom.xml | 2 +- .../main/java/org/test/SampleApplication.java | 0 .../it/jar-classifier-source/verify.groovy | 33 +++++++++ .../boot/maven/RepackageMojo.java | 74 ++++++++++--------- .../apt/examples/repackage-classifier.apt.vm | 59 ++++++++++++++- 11 files changed, 216 insertions(+), 49 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/invoker.properties create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/pom.xml rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/{jar-repackage-classifier/verify.groovy => jar-classifier-main/src/main/java/org/test/SampleApplication.java} (58%) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/verify.groovy create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/invoker.properties rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/{jar-repackage-classifier => jar-classifier-source}/pom.xml (97%) rename spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/{jar-repackage-classifier => jar-classifier-source}/src/main/java/org/test/SampleApplication.java (100%) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/verify.groovy 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-repackage-classifier/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/src/main/java/org/test/SampleApplication.java similarity index 58% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/verify.groovy rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-main/src/main/java/org/test/SampleApplication.java index 8d5e781d77..8a5b669129 100644 --- 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-classifier-main/src/main/java/org/test/SampleApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * 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. @@ -14,13 +14,11 @@ * limitations under the License. */ -import java.io.*; -import org.springframework.boot.maven.*; +package org.test; -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) +public class SampleApplication { + + public static void main(String[] args) { } -}.verify(); + +} 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-repackage-classifier/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/pom.xml similarity index 97% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/pom.xml rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/pom.xml index e1e829462e..ad8143b793 100644 --- 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-classifier-source/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot.maven.it - jar-repackage-classifier + jar-classifier-source 0.0.1.BUILD-SNAPSHOT UTF-8 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-classifier-source/src/main/java/org/test/SampleApplication.java similarity index 100% rename from spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-repackage-classifier/src/main/java/org/test/SampleApplication.java rename to spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-classifier-source/src/main/java/org/test/SampleApplication.java 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 9ea06837b3..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 @@ -98,12 +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 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 the maven documentation for more details. * @since 1.0 @@ -210,9 +211,9 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { } private void repackage() throws MojoExecutionException { - File source = getSourceFile(); + 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, @@ -227,19 +228,26 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { updateArtifact(source, target, repackager.getBackupFile()); } - private File getSourceFile() { - Artifact sourceArtifact = this.project.getArtifact(); + /** + * 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()); + } - if (this.classifier != null) { + private Artifact getArtifact(String classifier) { + if (classifier != null) { for (Artifact attachedArtifact : this.project.getAttachedArtifacts()) { - if (this.classifier.equals(attachedArtifact.getClassifier())) { - sourceArtifact = attachedArtifact; - break; + if (classifier.equals(attachedArtifact.getClassifier())) { + return attachedArtifact; } } } - - return sourceArtifact.getFile(); + return null; } private File getTargetFile() { @@ -250,18 +258,10 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { if (!this.outputDirectory.exists()) { this.outputDirectory.mkdirs(); } - return new File(this.outputDirectory, this.finalName + getClassifier() + "." + return new File(this.outputDirectory, this.finalName + classifier + "." + 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( @@ -327,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).