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
pull/5300/head
Stephane Nicoll 9 years ago
parent 2ecb33f7b4
commit 7f3ee2e405

@ -37,6 +37,7 @@ import org.springframework.lang.UsesJava8;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
*/ */
public class Repackager { public class Repackager {
@ -140,8 +141,7 @@ public class Repackager {
destination = destination.getAbsoluteFile(); destination = destination.getAbsoluteFile();
File workingSource = this.source; File workingSource = this.source;
if (this.source.equals(destination)) { if (this.source.equals(destination)) {
workingSource = new File(this.source.getParentFile(), workingSource = getBackupFile();
this.source.getName() + ".original");
workingSource.delete(); workingSource.delete();
renameFile(this.source, workingSource); 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 { private boolean alreadyRepackaged() throws IOException {
JarFile jarFile = new JarFile(this.source); JarFile jarFile = new JarFile(this.source);
try { try {

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-attach-disabled</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>some.random.Main</mainClass>
</manifest>
<manifestEntries>
<Not-Used>Foo</Not-Used>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring.version@</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>@servlet-api.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

@ -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) {
}
}

@ -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)

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * 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 * attached with that classifier and the main artifact will be deployed as the
* according to the finalName. Attaching the artifact allows to deploy it alongside to * 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 <a href= * the original one, see <a href=
* "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html" * "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
* > the maven documentation for more details</a>. * > the maven documentation for more details</a>.
@ -105,6 +107,13 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
@Parameter @Parameter
private String classifier; 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 * The name of the main class. If not specified the first compiled class found that
* contains a 'main' method will be used. * contains a 'main' method will be used.
@ -210,6 +219,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
catch (IOException ex) { catch (IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex); throw new MojoExecutionException(ex.getMessage(), ex);
} }
if (this.attach) {
if (this.classifier != null) { if (this.classifier != null) {
getLog().info("Attaching archive: " + target + ", with classifier: " getLog().info("Attaching archive: " + target + ", with classifier: "
+ this.classifier); + this.classifier);
@ -221,6 +232,12 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
getLog().info("Replacing main artifact " + source + " to " + 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);
}
}
private ArtifactsFilter[] getAdditionalFilters() { private ArtifactsFilter[] getAdditionalFilters() {
if (this.excludeDevtools) { if (this.excludeDevtools) {

@ -0,0 +1,49 @@
-----
Local repackaged artifact
-----
Stephane Nicoll
-----
2016-03-01
-----
By default, the <<<repackage>>> 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>
...
<build>
...
<plugins>
...
<plugin>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
...
</plugin>
...
</plugins>
...
</build>
...
</project>
---
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.

@ -42,6 +42,8 @@ Spring Boot Maven Plugin
* {{{./examples/repackage-classifier.html}Custom repackage classifier}} * {{{./examples/repackage-classifier.html}Custom repackage classifier}}
* {{{./examples/repackage-disable-attach.html}Local repackaged artifact}}
* {{{./examples/exclude-dependency.html}Exclude a dependency}} * {{{./examples/exclude-dependency.html}Exclude a dependency}}
* {{{./examples/run-debug.html}Debug the application}} * {{{./examples/run-debug.html}Debug the application}}

@ -8,6 +8,7 @@
</menu> </menu>
<menu name="Examples"> <menu name="Examples">
<item name="Custom repackage classifier" href="examples/repackage-classifier.html"/> <item name="Custom repackage classifier" href="examples/repackage-classifier.html"/>
<item name="Local repackaged artifact" href="examples/repackage-disable-attach.html"/>
<item name="Exclude a dependency" href="examples/exclude-dependency.html"/> <item name="Exclude a dependency" href="examples/exclude-dependency.html"/>
<item name="Debug the application" href="examples/run-debug.html"/> <item name="Debug the application" href="examples/run-debug.html"/>
<item name="Random port for integration tests" href="examples/it-random-port.html"/> <item name="Random port for integration tests" href="examples/it-random-port.html"/>

Loading…
Cancel
Save