From 7f3ee2e405de0fb7667488a3b3f1b63a9d812dab Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 1 Mar 2016 11:12:59 +0100 Subject: [PATCH] Disable attach of repackaged artifact This commit allows to generate the packaged artifact only locally by adding a new `attach` property. If `attach` is set to `false` explicitly, only the main artifact is installed/deployed. Closes gh-5258 --- .../boot/loader/tools/Repackager.java | 13 ++++- .../it/jar-attach-disabled/invoker.properties | 1 + .../src/it/jar-attach-disabled/pom.xml | 58 +++++++++++++++++++ .../main/java/org/test/SampleApplication.java | 24 ++++++++ .../src/it/jar-attach-disabled/verify.groovy | 20 +++++++ .../boot/maven/RepackageMojo.java | 39 +++++++++---- .../examples/repackage-disable-attach.apt.vm | 49 ++++++++++++++++ .../src/site/apt/index.apt | 2 + .../src/site/site.xml | 1 + 9 files changed, 194 insertions(+), 13 deletions(-) create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/invoker.properties create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/pom.xml create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/src/main/java/org/test/SampleApplication.java create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-disable-attach.apt.vm diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index 0928254727..b1a6b527eb 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -37,6 +37,7 @@ import org.springframework.lang.UsesJava8; * * @author Phillip Webb * @author Andy Wilkinson + * @author Stephane Nicoll */ public class Repackager { @@ -140,8 +141,7 @@ public class Repackager { destination = destination.getAbsoluteFile(); File workingSource = this.source; if (this.source.equals(destination)) { - workingSource = new File(this.source.getParentFile(), - this.source.getName() + ".original"); + workingSource = getBackupFile(); workingSource.delete(); renameFile(this.source, workingSource); } @@ -162,6 +162,15 @@ public class Repackager { } } + /** + * Return the {@link File} to use to backup the original source. + * @return the file to use to backup the original source + */ + public File getBackupFile() { + return new File(this.source.getParentFile(), + this.source.getName() + ".original"); + } + private boolean alreadyRepackaged() throws IOException { JarFile jarFile = new JarFile(this.source); try { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/invoker.properties b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/invoker.properties new file mode 100644 index 0000000000..c0c3f7cc07 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/invoker.properties @@ -0,0 +1 @@ +invoker.goals=clean install \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/pom.xml new file mode 100644 index 0000000000..5677aff7f2 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + org.springframework.boot.maven.it + jar-attach-disabled + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + repackage + + + false + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + some.random.Main + + + Foo + + + + + + + + + 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-attach-disabled/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..e8784d4593 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/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-attach-disabled/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy new file mode 100644 index 0000000000..f7fde1c94c --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-attach-disabled/verify.groovy @@ -0,0 +1,20 @@ +import java.io.*; +import org.springframework.boot.maven.* + +import static org.junit.Assert.assertTrue; + +Verify.verifyJar( + new File( basedir, "target/jar-attach-disabled-0.0.1.BUILD-SNAPSHOT.jar" ), "some.random.Main" +); +File main = new File( basedir, "target/jar-attach-disabled-0.0.1.BUILD-SNAPSHOT.jar") +File backup = new File( basedir, "target/jar-attach-disabled-0.0.1.BUILD-SNAPSHOT.jar.original") +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) +return file.text.contains ("Installing "+backup) + + + 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 83bce72bff..c0f58f3bcd 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 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. @@ -95,8 +95,10 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { /** * Classifier to add to the artifact generated. If given, the artifact will be - * attached. If this is not given, it will merely be written to the output directory - * according to the finalName. Attaching the artifact allows to deploy it alongside to + * 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. @@ -105,6 +107,13 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { @Parameter private String classifier; + /** + * Attach the repackaged archive to be installed and deployed. + * @since 1.4 + */ + @Parameter(defaultValue = "true") + private boolean attach = true; + /** * The name of the main class. If not specified the first compiled class found that * contains a 'main' method will be used. @@ -210,15 +219,23 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { catch (IOException ex) { throw new MojoExecutionException(ex.getMessage(), ex); } - if (this.classifier != null) { - getLog().info("Attaching archive: " + target + ", with classifier: " - + this.classifier); - this.projectHelper.attachArtifact(this.project, this.project.getPackaging(), - this.classifier, target); + + if (this.attach) { + if (this.classifier != null) { + getLog().info("Attaching archive: " + target + ", with classifier: " + + this.classifier); + this.projectHelper.attachArtifact(this.project, this.project.getPackaging(), + this.classifier, target); + } + else if (!source.equals(target)) { + this.project.getArtifact().setFile(target); + getLog().info("Replacing main artifact " + source + " to " + target); + } } - else if (!source.equals(target)) { - this.project.getArtifact().setFile(target); - getLog().info("Replacing main artifact " + source + " to " + target); + else if (source.equals(target)) { + File backup = repackager.getBackupFile(); + this.project.getArtifact().setFile(backup); + getLog().info("Updating main artifact " + source + " to " + backup); } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-disable-attach.apt.vm b/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-disable-attach.apt.vm new file mode 100644 index 0000000000..0d39255532 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/repackage-disable-attach.apt.vm @@ -0,0 +1,49 @@ + ----- + Local repackaged artifact + ----- + Stephane Nicoll + ----- + 2016-03-01 + ----- + + By default, the <<>> goal will replace the original artifact with the + executable one. If you need to only deploy the original jar and yet be able to + run your app with the regular file name, configure the plugin as follows: + +--- + + ... + + ... + + ... + + ${project.groupId} + ${project.artifactId} + ${project.version} + + + + repackage + + + false + + + + ... + + ... + + ... + + ... + +--- + + This configuration will generate two artifacts: the original one and the executable counter + part produced by the repackage goal. Only the original one will be installed/deployed. + + + + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/index.apt b/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/index.apt index d7a7e99595..f52a3654bb 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/index.apt +++ b/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/index.apt @@ -42,6 +42,8 @@ Spring Boot Maven Plugin * {{{./examples/repackage-classifier.html}Custom repackage classifier}} + * {{{./examples/repackage-disable-attach.html}Local repackaged artifact}} + * {{{./examples/exclude-dependency.html}Exclude a dependency}} * {{{./examples/run-debug.html}Debug the application}} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/site/site.xml b/spring-boot-tools/spring-boot-maven-plugin/src/site/site.xml index 76499d51a7..0d27faaed2 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/site/site.xml +++ b/spring-boot-tools/spring-boot-maven-plugin/src/site/site.xml @@ -8,6 +8,7 @@ +