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
This commit is contained in:
Stephane Nicoll 2016-03-01 11:12:59 +01:00
parent 2ecb33f7b4
commit 7f3ee2e405
9 changed files with 194 additions and 13 deletions

View File

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

View File

@ -0,0 +1 @@
invoker.goals=clean install

View File

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

View File

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

View File

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

View File

@ -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 <a href=
* "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
* > the maven documentation for more details</a>.
@ -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);
}
}

View File

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

View File

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

View File

@ -8,6 +8,7 @@
</menu>
<menu name="Examples">
<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="Debug the application" href="examples/run-debug.html"/>
<item name="Random port for integration tests" href="examples/it-random-port.html"/>