Polish "Allow repackage maven goal to take a source classifier"

Closes gh-11061
pull/13171/merge
Stephane Nicoll 7 years ago
parent 3c8e012411
commit ed02d02d19

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

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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-classifier-main</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>test</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@maven-jar-plugin.version@</version>
<configuration>
<archive>
<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>

@ -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();
}

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

@ -3,7 +3,7 @@
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-repackage-classifier</artifactId>
<artifactId>jar-classifier-source</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

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

@ -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 <a href=
* Classifier to add to the repackaged archive. If not given, the main artifact will
* be replaced by the repackaged archive. If given, the classifier will also be used
* to determine the source archive to repackage: if an artifact with that classifier
* already exists, it will be used as source and replaced. If no such artifact exists,
* the main artifact will be used as source and the repackaged archive will be
* attached as a supplemental artifact with that classifier. 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>.
* @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<Artifact> 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);
}
}

@ -47,9 +47,64 @@
</project>
---
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 <<<task>>> classified artifact with the repackaged app:
---
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@maven-jar-plugin.version@</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<classifier>task</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>task</classifier>
</configuration>
</execution>
</executions>
...
</plugin>
...
</plugins>
...
</build>
...
</project>
---
As both the <<<maven-jar-plugin>>> and the <<<spring-boot-maven-plugin>>> runs at the
same phase, it is important that the jar plugin is defined first (so that it runs before
the repackage goal).

Loading…
Cancel
Save