Skip to content

Commit

Permalink
[bs-232] Use "jar" packaging instead of custom "executable-jar"
Browse files Browse the repository at this point in the history
[Fixes #53376257]
  • Loading branch information
dsyer committed Jul 16, 2013
1 parent 4ce6b64 commit 6c3fd2b
Show file tree
Hide file tree
Showing 29 changed files with 232 additions and 292 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ target
.springBeans
dependency-reduced-pom.xml
build.log
_site/
2 changes: 1 addition & 1 deletion spring-package-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<configuration>
<goalPrefix>configurator</goalPrefix>
<goalPrefix>spring</goalPrefix>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
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.zero.maven.it</groupId>
<artifactId>executable-jar</artifactId>
<artifactId>jar</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>executable-jar</packaging>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import java.io.*;
import org.springframework.maven.packaging.*;

Verify.verifyJar(
new File( basedir, "target/executable-jar-0.0.1.BUILD-SNAPSHOT.jar" )
new File( basedir, "target/jar-0.0.1.BUILD-SNAPSHOT.jar" )
);

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@
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.zero.maven.it</groupId>
<artifactId>executable-war</artifactId>
<artifactId>war</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>executable-war</packaging>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import java.io.*;
import org.springframework.maven.packaging.*;

Verify.verifyWar(
new File( basedir, "target/executable-war-0.0.1.BUILD-SNAPSHOT.war" )
new File( basedir, "target/war-0.0.1.BUILD-SNAPSHOT.war" )
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2012-2013 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.springframework.maven.packaging;

import org.apache.maven.artifact.Artifact;

/**
* @author Dave Syer
*/
public interface ArchiveHelper {

/**
* Returns the destination of an {@link Artifact}.
* @param artifact the artifact
* @return the destination or {@code null} to exclude
*/
String getArtifactDestination(Artifact artifact);

/**
* Returns the launcher class that will be used.
*/
String getLauncherClass();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.maven.packaging;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
Expand All @@ -30,14 +32,18 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.zip.ZipEntry;
import org.codehaus.plexus.archiver.zip.ZipFile;
import org.codehaus.plexus.archiver.zip.ZipResource;
import org.codehaus.plexus.util.IOUtil;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.repository.RemoteRepository;
Expand All @@ -52,11 +58,8 @@
*
* @author Phillip Webb
*/
public abstract class AbstractExecutableArchiveMojo extends AbstractMojo {

private static final String[] DEFAULT_EXCLUDES = new String[] { "**/package.html" };

private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
@Mojo(name = "package", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class ExecutableArchiveMojo extends AbstractMojo {

private static final String MAIN_CLASS_ATTRIBUTE = "Main-Class";

Expand Down Expand Up @@ -113,26 +116,12 @@ public abstract class AbstractExecutableArchiveMojo extends AbstractMojo {

/**
* 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. If this is not given, it will merely be written to the output directory
* according to the finalName.
*/
@Parameter
private String classifier;

/**
* List of files to include. Specified as fileset patterns which are relative to the
* input directory whose contents is being packaged into the archive.
*/
@Parameter
private String[] includes;

/**
* List of files to exclude. Specified as fileset patterns which are relative to the
* input directory whose contents is being packaged into the archive.
*/
@Parameter
private String[] excludes;

/**
* Directory containing the classes and resource files that should be packaged into
* the archive.
Expand All @@ -151,7 +140,7 @@ public abstract class AbstractExecutableArchiveMojo extends AbstractMojo {
/**
* Whether creating the archive should be forced.
*/
@Parameter(property = "archive.forceCreation", defaultValue = "false")
@Parameter(property = "archive.forceCreation", defaultValue = "true")
private boolean forceCreation;

/**
Expand All @@ -160,27 +149,9 @@ public abstract class AbstractExecutableArchiveMojo extends AbstractMojo {
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
private RepositorySystemSession repositorySystemSession;

/**
* Returns the type as defined in plexus components.xml
*/
protected abstract String getType();

/**
* Returns the file extension for the archive (e.g. 'jar').
*/
protected abstract String getExtension();
private ExecutableJarHelper jarHelper = new ExecutableJarHelper();

/**
* Returns the destination of an {@link Artifact}.
* @param artifact the artifact
* @return the destination or {@code null} to exclude
*/
protected abstract String getArtifactDestination(Artifact artifact);

/**
* Returns the launcher class that will be used.
*/
protected abstract String getLauncherClass();
private ExecutableWarHelper warHelper = new ExecutableWarHelper();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand All @@ -189,11 +160,24 @@ public void execute() throws MojoExecutionException, MojoFailureException {
this.project.getArtifact().setFile(archiveFile);
}
else {
getLog().info(
"Attaching archive: " + archiveFile + ", with classifier: "
+ this.classifier);
this.projectHelper.attachArtifact(this.project, getType(), this.classifier,
archiveFile);
}
}

private ArchiveHelper getArchiveHelper() throws MojoExecutionException {
if (getType().equals("jar")) {
return this.jarHelper;
}
if (getType().equals("war")) {
return this.warHelper;
}
throw new MojoExecutionException("Unsupported packaging type: " + getType());
}

private File createArchive() throws MojoExecutionException {
File archiveFile = getTargetFile();
MavenArchiver archiver = new MavenArchiver();
Expand All @@ -204,7 +188,8 @@ private File createArchive() throws MojoExecutionException {
archiver.getArchiver().setRecompressAddedZips(false);

try {
addContent(archiver);
getLog().info("Modifying archive: " + archiveFile);
copyContent(archiver, this.project.getArtifact().getFile());
addLibs(archiver);
ZipFile zipFile = addLauncherClasses(archiver);
try {
Expand All @@ -220,6 +205,37 @@ private File createArchive() throws MojoExecutionException {
}
}

private String getType() {
return this.project.getPackaging();
}

private String getExtension() {
return this.project.getPackaging();
}

private void copyContent(MavenArchiver archiver, File file) throws IOException {

FileInputStream input = new FileInputStream(file);
File original = new File(this.outputDirectory, "original.jar");
FileOutputStream output = new FileOutputStream(original);
IOUtil.copy(input, output, 2048);
input.close();
output.close();

ZipFile zipFile = new ZipFile(original);
Enumeration<? extends ZipEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// TODO: maybe merge manifest instead of skipping it?
if (!entry.isDirectory()
&& !entry.getName().toUpperCase().equals("/META-INF/MANIFEST.MF")) {
ZipResource zipResource = new ZipResource(zipFile, entry);
archiver.getArchiver().addResource(zipResource, entry.getName(), -1);
}
}

}

private File getTargetFile() {
String classifier = (this.classifier == null ? "" : this.classifier.trim());
if (classifier.length() > 0 && !classifier.startsWith("-")) {
Expand All @@ -242,40 +258,18 @@ private void customizeArchiveConfiguration() throws MojoExecutionException {
throw new MojoExecutionException("Unable to find a suitable main class, "
+ "please add a 'mainClass' property");
}
this.archive.getManifestEntries().put(MAIN_CLASS_ATTRIBUTE, getLauncherClass());
this.archive.getManifestEntries().put(MAIN_CLASS_ATTRIBUTE,
getArchiveHelper().getLauncherClass());
this.archive.getManifestEntries().put(START_CLASS_ATTRIBUTE, mainClass);
}

protected void addContent(MavenArchiver archiver) {
if (this.classesDirectrory.exists()) {
archiver.getArchiver().addDirectory(this.classesDirectrory,
getClassesDirectoryPrefix(), getIncludes(), getExcludes());
}
}

protected String getClassesDirectoryPrefix() {
return "";
}

protected final String[] getIncludes() {
if (this.includes != null && this.includes.length > 0) {
return this.includes;
}
return DEFAULT_INCLUDES;
}

protected final String[] getExcludes() {
if (this.excludes != null && this.excludes.length > 0) {
return this.excludes;
}
return DEFAULT_EXCLUDES;
}

private void addLibs(MavenArchiver archiver) {
private void addLibs(MavenArchiver archiver) throws MojoExecutionException {
getLog().info("Adding dependencies");
for (Artifact artifact : this.project.getArtifacts()) {
if (artifact.getFile() != null) {
String dir = getArtifactDestination(artifact);
String dir = getArchiveHelper().getArtifactDestination(artifact);
if (dir != null) {
getLog().debug("Adding dependency: " + artifact);
archiver.getArchiver().addFile(artifact.getFile(),
dir + artifact.getFile().getName());
}
Expand All @@ -285,6 +279,7 @@ private void addLibs(MavenArchiver archiver) {

private ZipFile addLauncherClasses(MavenArchiver archiver)
throws MojoExecutionException {
getLog().info("Adding launcher classes");
try {
List<RemoteRepository> repositories = new ArrayList<RemoteRepository>();
repositories.addAll(this.project.getRemotePluginRepositories());
Expand Down
Loading

0 comments on commit 6c3fd2b

Please sign in to comment.