Skip to content

Commit f47ea9c

Browse files
author
Dave Syer
committed
Add LayoutFactory in spring.factories
Instead of a fixed enum of layout types, user can provide custom layouts via implementations of LayoutFactory in spring.factories.
1 parent 719ef39 commit f47ea9c

File tree

6 files changed

+192
-82
lines changed

6 files changed

+192
-82
lines changed

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222

23-
import groovy.lang.Closure;
2423
import org.gradle.api.Project;
2524
import org.gradle.api.plugins.JavaPlugin;
2625

2726
import org.springframework.boot.gradle.buildinfo.BuildInfo;
2827
import org.springframework.boot.loader.tools.Layout;
29-
import org.springframework.boot.loader.tools.Layouts;
28+
import org.springframework.boot.loader.tools.LayoutType;
29+
30+
import groovy.lang.Closure;
3031

3132
/**
3233
* Gradle DSL Extension for 'Spring Boot'. Most of the time Spring Boot can guess the
@@ -88,7 +89,7 @@ public class SpringBootPluginExtension {
8889
* the MANIFEST.MF 'Main-Class' to be PropertiesLauncher. Gradle will coerce literal
8990
* String values to the correct type.
9091
*/
91-
LayoutType layout;
92+
String layout;
9293

9394
/**
9495
* Libraries that must be unpacked from fat jars in order to run. Use Strings in the
@@ -145,7 +146,7 @@ public SpringBootPluginExtension(Project project) {
145146
* @return the Layout to use or null if not explicitly set
146147
*/
147148
public Layout convertLayout() {
148-
return (this.layout == null ? null : this.layout.layout);
149+
return (this.layout == null ? null : LayoutType.layout(this.layout));
149150
}
150151

151152
public String getMainClass() {
@@ -188,11 +189,11 @@ public void setBackupSource(boolean backupSource) {
188189
this.backupSource = backupSource;
189190
}
190191

191-
public LayoutType getLayout() {
192+
public String getLayout() {
192193
return this.layout;
193194
}
194195

195-
public void setLayout(LayoutType layout) {
196+
public void setLayout(String layout) {
196197
this.layout = layout;
197198
}
198199

@@ -276,29 +277,4 @@ public void buildInfo(Closure<?> taskConfigurer) {
276277
}
277278
}
278279

279-
/**
280-
* Layout Types.
281-
*/
282-
enum LayoutType {
283-
284-
JAR(new Layouts.Jar()),
285-
286-
WAR(new Layouts.War()),
287-
288-
ZIP(new Layouts.Expanded()),
289-
290-
DIR(new Layouts.Expanded()),
291-
292-
MODULE(new Layouts.Module()),
293-
294-
NONE(new Layouts.None());
295-
296-
Layout layout;
297-
298-
LayoutType(Layout layout) {
299-
this.layout = layout;
300-
}
301-
302-
}
303-
304280
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
/**
20+
* Strategy for creating instances of {@link Layout}.
21+
*
22+
* @author Dave Syer
23+
*
24+
*/
25+
public interface LayoutFactory {
26+
27+
Layout getLayout();
28+
29+
String getName();
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.springframework.core.io.support.SpringFactoriesLoader;
24+
25+
/**
26+
* Archive layout types.
27+
*/
28+
public enum LayoutType {
29+
30+
/**
31+
* Jar Layout.
32+
*/
33+
JAR(new Layouts.Jar()),
34+
35+
/**
36+
* War Layout.
37+
*/
38+
WAR(new Layouts.War()),
39+
40+
/**
41+
* Zip Layout.
42+
*/
43+
ZIP(new Layouts.Expanded()),
44+
45+
/**
46+
* Dir Layout.
47+
*/
48+
DIR(new Layouts.Expanded()),
49+
50+
/**
51+
* Module Layout.
52+
*/
53+
MODULE(new Layouts.Module()),
54+
55+
/**
56+
* No Layout.
57+
*/
58+
NONE(new Layouts.None());
59+
60+
private static Map<String, Layout> customTypes;
61+
62+
private final Layout layout;
63+
64+
public Layout layout() {
65+
return this.layout;
66+
}
67+
68+
LayoutType(Layout layout) {
69+
this.layout = layout;
70+
}
71+
72+
public static Layout layout(String value) {
73+
try {
74+
return valueOf(value).layout();
75+
}
76+
catch (IllegalArgumentException e) {
77+
if (customTypes == null) {
78+
customTypes = new HashMap<String, Layout>();
79+
lookupCustomTypes();
80+
}
81+
Layout layout = customTypes.get(value);
82+
if (layout == null) {
83+
throw new IllegalArgumentException(
84+
"Cannot resolve custom layout type: " + value);
85+
}
86+
return layout;
87+
}
88+
}
89+
90+
private static void lookupCustomTypes() {
91+
ClassLoader classLoader = LayoutType.class.getClassLoader();
92+
List<LayoutFactory> factories = SpringFactoriesLoader
93+
.loadFactories(LayoutFactory.class, classLoader);
94+
for (LayoutFactory factory : factories) {
95+
customTypes.put(factory.getName(), factory.getLayout());
96+
}
97+
}
98+
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
import org.junit.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* @author Dave Syer
25+
*
26+
*/
27+
public class LayoutTypeTests {
28+
29+
@Test
30+
public void standardType() {
31+
assertThat(LayoutType.layout("DIR")).isEqualTo(LayoutType.valueOf("DIR").layout());
32+
}
33+
34+
@Test
35+
public void customType() {
36+
assertThat(LayoutType.layout("CUSTOM")).isNotNull();
37+
}
38+
39+
public static class TestLayoutFactory implements LayoutFactory {
40+
41+
@Override
42+
public Layout getLayout() {
43+
return new Layouts.Jar();
44+
}
45+
46+
@Override
47+
public String getName() {
48+
return "CUSTOM";
49+
}}
50+
51+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.loader.tools.LayoutFactory=\
2+
org.springframework.boot.loader.tools.LayoutTypeTests.TestLayoutFactory

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

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242

4343
import org.springframework.boot.loader.tools.DefaultLaunchScript;
4444
import org.springframework.boot.loader.tools.LaunchScript;
45-
import org.springframework.boot.loader.tools.Layout;
46-
import org.springframework.boot.loader.tools.Layouts;
45+
import org.springframework.boot.loader.tools.LayoutType;
4746
import org.springframework.boot.loader.tools.Libraries;
4847
import org.springframework.boot.loader.tools.Repackager;
4948

@@ -131,7 +130,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
131130
* @since 1.0
132131
*/
133132
@Parameter
134-
private LayoutType layout;
133+
private String layout;
135134

136135
/**
137136
* A list of the libraries that must be unpacked from fat jars in order to run.
@@ -228,7 +227,6 @@ private Repackager getRepackager(File source) {
228227
repackager.setMainClass(this.mainClass);
229228
if (this.layout != null) {
230229
getLog().info("Layout: " + this.layout);
231-
repackager.setLayout(this.layout.layout());
232230
}
233231
return repackager;
234232
}
@@ -309,53 +307,6 @@ else if (!source.equals(repackaged)) {
309307
}
310308
}
311309

312-
/**
313-
* Archive layout types.
314-
*/
315-
public enum LayoutType {
316-
317-
/**
318-
* Jar Layout.
319-
*/
320-
JAR(new Layouts.Jar()),
321-
322-
/**
323-
* War Layout.
324-
*/
325-
WAR(new Layouts.War()),
326-
327-
/**
328-
* Zip Layout.
329-
*/
330-
ZIP(new Layouts.Expanded()),
331-
332-
/**
333-
* Dir Layout.
334-
*/
335-
DIR(new Layouts.Expanded()),
336-
337-
/**
338-
* Module Layout.
339-
*/
340-
MODULE(new Layouts.Module()),
341-
342-
/**
343-
* No Layout.
344-
*/
345-
NONE(new Layouts.None());
346-
347-
private final Layout layout;
348-
349-
public Layout layout() {
350-
return this.layout;
351-
}
352-
353-
LayoutType(Layout layout) {
354-
this.layout = layout;
355-
}
356-
357-
}
358-
359310
private static class LoggingRepackager extends Repackager {
360311

361312
private final Log log;

0 commit comments

Comments
 (0)