-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Repackager: pluggable layouts #2629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8c5011a
cec323a
59fcc14
49af74e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
|
||
package org.springframework.boot.loader.tools; | ||
|
||
import org.springframework.boot.loader.tools.layout.BaseLayout; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
@@ -33,6 +35,49 @@ | |
*/ | ||
public class Layouts { | ||
|
||
static private Class<? extends Layout> defaultLayout = Layouts.Jar.class; | ||
|
||
static private Map<String, Class<?>> map = new HashMap<String, Class<?>>() {{ | ||
for (Class<?> cls : Layouts.class.getClasses()) { | ||
if (Layout.class.isAssignableFrom(cls)) { | ||
put(toSimpleLayoutName(cls), (Class) cls); | ||
} | ||
} | ||
}}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/** | ||
* Resolves and instantiates named layout. | ||
* If name is undefined, default layout is returned (JAR). | ||
* If given name refers to one of the layout aliases (JAR, ZIP, etc), it is returned. | ||
* Otherwise, assume layout name is an actual class name, and try to load it. | ||
* @param name | ||
* @return | ||
* @throws RuntimeException | ||
*/ | ||
static public Layout resolve(String name) throws RuntimeException { | ||
try { | ||
if (name == null) { return defaultLayout.newInstance(); } | ||
|
||
Class<?> cls = null; | ||
if (cls == null) { cls = map.get(name); } | ||
if (cls == null) { cls = Class.forName(name); } | ||
|
||
return (Layout) cls.newInstance(); | ||
} | ||
catch (Exception e) { | ||
throw new RuntimeException(String.format( | ||
"Cannot resolve layout `%s`. " + | ||
"Provide a fully qualified name of the class implementing Layout interface, " + | ||
"or one of the following: %s", | ||
name, map.keySet() | ||
), e); | ||
} | ||
} | ||
|
||
static private String toSimpleLayoutName(Class<?> cls) { | ||
return cls.getSimpleName().toUpperCase(); | ||
} | ||
|
||
/** | ||
* Return the a layout for the given source file. | ||
* @param file the source file | ||
|
@@ -90,9 +135,12 @@ public static class Expanded extends Jar { | |
public String getLauncherClassName() { | ||
return "org.springframework.boot.loader.PropertiesLauncher"; | ||
} | ||
|
||
} | ||
|
||
public static class Zip extends Expanded {} | ||
|
||
public static class Dir extends Expanded {} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need the classes above to support all legacy layout names built in |
||
/** | ||
* No layout. | ||
*/ | ||
|
@@ -110,6 +158,11 @@ public boolean isExecutable() { | |
|
||
} | ||
|
||
/** | ||
* This is what NONE should be. | ||
*/ | ||
public static class Null extends BaseLayout {} | ||
|
||
/** | ||
* Executable WAR layout. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.springframework.boot.loader.tools.layout; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dedicating new package for layouts because referencing nested classes in Gradle is tricky (you need to use dollar which must be escaped in Gradle, and also, this is difference between Maven and Gradle, hence confusing...) |
||
|
||
import org.springframework.boot.loader.tools.Layout; | ||
import org.springframework.boot.loader.tools.LibraryScope; | ||
|
||
/** | ||
* @author <a href="mailto:patrikbeno@gmail.com">Patrik Beno</a> | ||
*/ | ||
public class BaseLayout implements Layout { | ||
@Override | ||
public String getLauncherClassName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getLibraryDestination(String libraryName, LibraryScope scope) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getClassesLocation() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public boolean isExecutable() { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,12 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { | |
@Parameter | ||
private String classifier; | ||
|
||
/** | ||
* Name of the launcher class; overrides the one specified by layout. | ||
*/ | ||
@Parameter | ||
private String launcherClass; | ||
|
||
/** | ||
* The name of the main class. If not specified the first compiled class found that | ||
* contains a 'main' method will be used. | ||
|
@@ -118,7 +124,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { | |
* @since 1.0 | ||
*/ | ||
@Parameter | ||
private LayoutType layout; | ||
private String layout; | ||
|
||
/** | ||
* A list of the libraries that must be unpacked from fat jars in order to run. | ||
|
@@ -182,10 +188,10 @@ protected String findMainMethod(JarFile source) throws IOException { | |
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nulls and defaults handled in |
||
}; | ||
repackager.setMainClass(this.mainClass); | ||
if (this.layout != null) { | ||
getLog().info("Layout: " + this.layout); | ||
repackager.setLayout(this.layout.layout()); | ||
} | ||
repackager.setLauncherClass(this.launcherClass); | ||
|
||
getLog().info("Layout: " + this.layout); | ||
repackager.setLayout(Layouts.resolve(this.layout)); | ||
|
||
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), | ||
getFilters()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nulls and defaults handled in
Layouts.resolve()