From fd62eab23a8cbc3d7651df970afbaa24c9fa4774 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 17 Jul 2013 01:12:21 -0700 Subject: [PATCH] Polish maven plugin --- .../maven/packaging/ArchiveHelper.java | 2 ++ .../packaging/ExecutableArchiveMojo.java | 28 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ArchiveHelper.java b/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ArchiveHelper.java index c34b066095..839a6b3af6 100644 --- a/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ArchiveHelper.java +++ b/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ArchiveHelper.java @@ -19,6 +19,8 @@ package org.springframework.maven.packaging; import org.apache.maven.artifact.Artifact; /** + * Strategy interface used by {@link ExecutableArchiveMojo} when creating archives. + * * @author Dave Syer */ public interface ArchiveHelper { diff --git a/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ExecutableArchiveMojo.java b/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ExecutableArchiveMojo.java index 688c956b1d..53941c8b7c 100644 --- a/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ExecutableArchiveMojo.java +++ b/spring-package-maven-plugin/src/main/java/org/springframework/maven/packaging/ExecutableArchiveMojo.java @@ -21,8 +21,11 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.maven.archiver.MavenArchiveConfiguration; import org.apache.maven.archiver.MavenArchiver; @@ -54,7 +57,8 @@ import org.sonatype.aether.resolution.ArtifactResult; import org.sonatype.aether.util.artifact.DefaultArtifact; /** - * Abstract base of MOJOs that package executable archives. + * MOJO that can can be used to repackage existing JAR and WAR archives so that they can + * be executed from the command line using {@literal java -jar}. * * @author Phillip Webb */ @@ -65,6 +69,14 @@ public class ExecutableArchiveMojo extends AbstractMojo { private static final String START_CLASS_ATTRIBUTE = "Start-Class"; + private static final Map ARCHIVE_HELPERS; + static { + Map helpers = new HashMap(); + helpers.put("jar", new ExecutableJarHelper()); + helpers.put("war", new ExecutableWarHelper()); + ARCHIVE_HELPERS = Collections.unmodifiableMap(helpers); + } + /** * Archiver used to create a JAR file. */ @@ -149,10 +161,6 @@ public class ExecutableArchiveMojo extends AbstractMojo { @Parameter(defaultValue = "${repositorySystemSession}", readonly = true) private RepositorySystemSession repositorySystemSession; - private ExecutableJarHelper jarHelper = new ExecutableJarHelper(); - - private ExecutableWarHelper warHelper = new ExecutableWarHelper(); - @Override public void execute() throws MojoExecutionException, MojoFailureException { File archiveFile = createArchive(); @@ -169,13 +177,11 @@ public class ExecutableArchiveMojo extends AbstractMojo { } private ArchiveHelper getArchiveHelper() throws MojoExecutionException { - if (getType().equals("jar")) { - return this.jarHelper; - } - if (getType().equals("war")) { - return this.warHelper; + ArchiveHelper helper = ARCHIVE_HELPERS.get(getType()); + if (helper == null) { + throw new MojoExecutionException("Unsupported packaging type: " + getType()); } - throw new MojoExecutionException("Unsupported packaging type: " + getType()); + return helper; } private File createArchive() throws MojoExecutionException {