Add layout=NONE to packaging tools

pull/132/head
Dave Syer 11 years ago
parent 06c16ae452
commit c0bcb5e8e9

@ -38,7 +38,7 @@ import org.springframework.boot.loader.tools.Layouts
public class SpringBootPluginExtension { public class SpringBootPluginExtension {
static enum LayoutType { static enum LayoutType {
JAR(new Layouts.Jar()), WAR(new Layouts.War()), ZIP(new Layouts.Expanded()), DIR(new Layouts.Expanded()); JAR(new Layouts.Jar()), WAR(new Layouts.War()), ZIP(new Layouts.Expanded()), DIR(new Layouts.Expanded()), NONE(new Layouts.None());
Layout layout; Layout layout;
private LayoutType(Layout layout) { private LayoutType(Layout layout) {
this.layout = layout; this.layout = layout;

@ -25,6 +25,7 @@ import java.util.Map;
* Common {@link Layout}s. * Common {@link Layout}s.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer
*/ */
public class Layouts { public class Layouts {
@ -82,6 +83,17 @@ public class Layouts {
} }
/**
* Executable expanded archive layout.
*/
public static class None extends Jar {
@Override
public String getLauncherClassName() {
return null;
}
}
/** /**
* Executable WAR layout. * Executable WAR layout.
*/ */

@ -172,12 +172,18 @@ public class Repackager {
startClass = MainClassFinder.findMainClass(source, startClass = MainClassFinder.findMainClass(source,
this.layout.getClassesLocation()); this.layout.getClassesLocation());
} }
String launcherClassName = this.layout.getLauncherClassName();
if (launcherClassName != null) {
manifest.getMainAttributes()
.putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName);
if (startClass == null) { if (startClass == null) {
throw new IllegalStateException("Unable to find main class"); throw new IllegalStateException("Unable to find main class");
} }
manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE,
this.layout.getLauncherClassName());
manifest.getMainAttributes().putValue(START_CLASS_ATTRIBUTE, startClass); manifest.getMainAttributes().putValue(START_CLASS_ATTRIBUTE, startClass);
}
else if (startClass != null) {
manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, startClass);
}
String bootVersion = getClass().getPackage().getImplementationVersion(); String bootVersion = getClass().getPackage().getImplementationVersion();
manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion); manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion);

@ -138,6 +138,30 @@ public class RepackagerTests {
new Repackager(this.testJarFile.getFile()).repackage(NO_LIBRARIES); new Repackager(this.testJarFile.getFile()).repackage(NO_LIBRARIES);
} }
@Test
public void noMainClassAndLayoutIsNone() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
repackager.setLayout(new Layouts.None());
repackager.repackage(file, NO_LIBRARIES);
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"),
equalTo("a.b.C"));
}
@Test
public void noMainClassAndLayoutIsNoneWithNoMain() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
repackager.setLayout(new Layouts.None());
repackager.repackage(file, NO_LIBRARIES);
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"),
equalTo(null));
}
@Test @Test
public void sameSourceAndDestinationWithBackup() throws Exception { public void sameSourceAndDestinationWithBackup() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class); this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);

@ -36,7 +36,9 @@ import org.springframework.boot.loader.tools.Repackager;
/** /**
* MOJO that can can be used to repackage existing JAR and WAR archives so that they can * 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}. * be executed from the command line using {@literal java -jar}. With
* <code>layout=NONE</code> can also be used simply to package a JAR with nested
* dependencies (and no main class, so not executable).
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer * @author Dave Syer
@ -84,7 +86,7 @@ public class RepackageMojo extends AbstractMojo {
private String mainClass; private String mainClass;
/** /**
* The layout to use (JAR, WAR, ZIP, DIR) in case it cannot be inferred. * The layout to use (JAR, WAR, ZIP, DIR, NONE) in case it cannot be inferred.
*/ */
@Parameter @Parameter
private LayoutType layout; private LayoutType layout;
@ -126,7 +128,7 @@ public class RepackageMojo extends AbstractMojo {
public static enum LayoutType { public static enum LayoutType {
JAR(new Layouts.Jar()), WAR(new Layouts.War()), ZIP(new Layouts.Expanded()), DIR( JAR(new Layouts.Jar()), WAR(new Layouts.War()), ZIP(new Layouts.Expanded()), DIR(
new Layouts.Expanded()); new Layouts.Expanded()), NONE(new Layouts.None());
private Layout layout; private Layout layout;
public Layout layout() { public Layout layout() {

Loading…
Cancel
Save