Skip to content

Commit 7ec646d

Browse files
committed
Repackager: layouts refactoring. Layouts are now pluggable: specify fully qualified name of the class implementing the Layout interface, or use known legacy aliases (JAR, ZIP, DIR, ...)
1 parent 7c6ee87 commit 7ec646d

File tree

5 files changed

+71
-76
lines changed

5 files changed

+71
-76
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPluginExtension.groovy

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,6 @@ import org.springframework.boot.loader.tools.Layouts
3838
*/
3939
public class SpringBootPluginExtension {
4040

41-
static enum LayoutType {
42-
43-
JAR(new Layouts.Jar()),
44-
45-
WAR(new Layouts.War()),
46-
47-
ZIP(new Layouts.Expanded()),
48-
49-
DIR(new Layouts.Expanded()),
50-
51-
MODULE(new Layouts.Module()),
52-
53-
NONE(new Layouts.None());
54-
55-
Layout layout;
56-
57-
private LayoutType(Layout layout) {
58-
this.layout = layout;
59-
}
60-
}
61-
6241
/**
6342
* The main class that should be run. Instead of setting this explicitly you can use the
6443
* 'mainClassName' of the project or the 'main' of the 'run' task. If not specified the
@@ -99,14 +78,14 @@ public class SpringBootPluginExtension {
9978
* PropertiesLauncher. Gradle will coerce literal String values to the
10079
* correct type.
10180
*/
102-
LayoutType layout;
81+
String layout;
10382

10483
/**
10584
* Convenience method for use in a custom task.
10685
* @return the Layout to use or null if not explicitly set
10786
*/
10887
Layout convertLayout() {
109-
(layout == null ? null : layout.layout)
88+
Layouts.resolve(layout)
11089
}
11190

11291
/**

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.gradle.api.tasks.TaskAction;
2929
import org.gradle.api.tasks.bundling.Jar;
3030
import org.springframework.boot.gradle.SpringBootPluginExtension;
31+
import org.springframework.boot.loader.tools.Layout;
3132
import org.springframework.boot.loader.tools.Repackager;
3233
import org.springframework.util.FileCopyUtils;
3334

@@ -165,9 +166,7 @@ private void repackage(File file) {
165166
}
166167
Repackager repackager = new LoggingRepackager(file);
167168
setMainClass(repackager);
168-
if (this.extension.convertLayout() != null) {
169-
repackager.setLayout(this.extension.convertLayout());
170-
}
169+
repackager.setLayout(this.extension.convertLayout());
171170
repackager.setBackupSource(this.extension.isBackupSource());
172171
try {
173172
repackager.repackage(file, this.libraries);

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Layouts.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@
3333
*/
3434
public class Layouts {
3535

36+
static private Class<? extends Layout> defaultLayout = Layouts.Jar.class;
37+
38+
static private Map<String, Class<?>> map = new HashMap<String, Class<?>>() {{
39+
for (Class<?> cls : Layouts.class.getClasses()) {
40+
if (Layout.class.isAssignableFrom(cls)) {
41+
put(toSimpleLayoutName(cls), (Class) cls);
42+
}
43+
}
44+
}};
45+
46+
static public Layout resolve(String name) throws RuntimeException {
47+
try {
48+
Class<?> cls = null;
49+
if (cls == null) { cls = map.get(name); };
50+
if (cls == null) { cls = Class.forName(name); }
51+
if (cls == null) { cls = defaultLayout; }
52+
return (Layout) cls.newInstance();
53+
}
54+
catch (Exception e) {
55+
throw new RuntimeException(String.format(
56+
"Cannot resolve layout `%s`. Provide a fully qualified class name or of the following: %s",
57+
name, map.keySet()
58+
), e);
59+
}
60+
}
61+
62+
static private String toSimpleLayoutName(Class<?> cls) {
63+
return cls.getSimpleName().toUpperCase();
64+
}
65+
3666
/**
3767
* Return the a layout for the given source file.
3868
* @param file the source file
@@ -90,9 +120,12 @@ public static class Expanded extends Jar {
90120
public String getLauncherClassName() {
91121
return "org.springframework.boot.loader.PropertiesLauncher";
92122
}
93-
94123
}
95124

125+
public static class Zip extends Expanded {}
126+
127+
public static class Dir extends Expanded {}
128+
96129
/**
97130
* No layout.
98131
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springframework.boot.loader.tools.layout;
2+
3+
import org.springframework.boot.loader.tools.Layout;
4+
import org.springframework.boot.loader.tools.LibraryScope;
5+
6+
/**
7+
* @author <a href="mailto:patrikbeno@gmail.com">Patrik Beno</a>
8+
*/
9+
public class BaseLayout implements Layout {
10+
@Override
11+
public String getLauncherClassName() {
12+
return null;
13+
}
14+
15+
@Override
16+
public String getLibraryDestination(String libraryName, LibraryScope scope) {
17+
return null;
18+
}
19+
20+
@Override
21+
public String getClassesLocation() {
22+
return "";
23+
}
24+
25+
@Override
26+
public boolean isExecutable() {
27+
return false;
28+
}
29+
}

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.apache.maven.plugins.annotations.ResolutionScope;
3535
import org.apache.maven.project.MavenProject;
3636
import org.apache.maven.project.MavenProjectHelper;
37-
import org.springframework.boot.loader.tools.Layout;
3837
import org.springframework.boot.loader.tools.Layouts;
3938
import org.springframework.boot.loader.tools.Libraries;
4039
import org.springframework.boot.loader.tools.Repackager;
@@ -115,7 +114,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
115114
* @since 1.0
116115
*/
117116
@Parameter
118-
private LayoutType layout;
117+
private String layout;
119118

120119
/**
121120
* A list of the libraries that must be unpacked from fat jars in order to run.
@@ -156,10 +155,9 @@ protected String findMainMethod(JarFile source) throws IOException {
156155
}
157156
};
158157
repackager.setMainClass(this.mainClass);
159-
if (this.layout != null) {
160-
getLog().info("Layout: " + this.layout);
161-
repackager.setLayout(this.layout.layout());
162-
}
158+
159+
getLog().info("Layout: " + this.layout);
160+
repackager.setLayout(Layouts.resolve(this.layout));
163161

164162
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
165163
getFilters());
@@ -190,47 +188,4 @@ private File getTargetFile() {
190188
+ this.project.getPackaging());
191189
}
192190

193-
public static enum LayoutType {
194-
195-
/**
196-
* Jar Layout
197-
*/
198-
JAR(new Layouts.Jar()),
199-
200-
/**
201-
* War Layout
202-
*/
203-
WAR(new Layouts.War()),
204-
205-
/**
206-
* Zip Layout
207-
*/
208-
ZIP(new Layouts.Expanded()),
209-
210-
/**
211-
* Dir Layout
212-
*/
213-
DIR(new Layouts.Expanded()),
214-
215-
/**
216-
* Module Layout
217-
*/
218-
MODULE(new Layouts.Module()),
219-
220-
/**
221-
* No Layout
222-
*/
223-
NONE(new Layouts.None());
224-
225-
private final Layout layout;
226-
227-
public Layout layout() {
228-
return this.layout;
229-
}
230-
231-
private LayoutType(Layout layout) {
232-
this.layout = layout;
233-
}
234-
}
235-
236191
}

0 commit comments

Comments
 (0)