Skip to content

Commit 24ec410

Browse files
author
Dave Syer
committed
Simplify contract of LayoutFactory
It's simpler if the build tools just use a factory if one is provided otherwise derive the layout from its type.
1 parent aa514a4 commit 24ec410

File tree

7 files changed

+48
-108
lines changed

7 files changed

+48
-108
lines changed

spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,11 @@ The following configuration options are available:
481481

482482
|`layout`
483483
|The `LayoutType` of archive, corresponding to how the dependencies are laid out inside
484-
(defaults to a guess based on the archive type).
484+
(defaults to a guess based on the archive type). See
485+
<<build-tool-plugins-gradle-configuration-layouts,available layouts for more details>>.
485486

486487
|`layoutFactory`
487-
|A factory for the actual `Layout` derived from the `LayoutType`. See
488-
<<build-tool-plugins-gradle-configuration-layouts,available layouts for more details>>.
488+
|A factory for the actual `Layout` derived from the `LayoutType`. If the factory is provided then the layout is ignored.
489489

490490
|`requiresUnpack`
491491
|A list of dependencies (in the form "`groupId:artifactId`" that must be unpacked from

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,17 @@ public SpringBootPluginExtension(Project project) {
154154
* @return the Layout to use or null if not explicitly set
155155
*/
156156
public Layout convertLayout(File file) {
157-
if (this.layout == null) {
158-
this.layout = LayoutType.forFile(file);
157+
Layout result;
158+
if (this.layoutFactory != null) {
159+
result = this.layoutFactory.getLayout();
159160
}
160-
return this.layoutFactory.getLayout(this.layout);
161+
else if (this.layout == null) {
162+
result = Layouts.forFile(file);
163+
}
164+
else {
165+
result = Layouts.forType(this.layout);
166+
}
167+
return result;
161168
}
162169

163170
public String getMainClass() {

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
*/
2525
public interface LayoutFactory {
2626

27-
Layout getLayout(LayoutType type);
27+
Layout getLayout();
2828

2929
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,27 @@ private Layouts() {
5050
* @return a {@link Layout}
5151
*/
5252
public static Layout forFile(File file) {
53-
return Layouts.getDefaultLayoutFactory().getLayout(LayoutType.forFile(file));
53+
return forType(LayoutType.forFile(file));
54+
}
55+
56+
/**
57+
* Return a layout for the given type.
58+
* @param type the layout type
59+
* @return a {@link Layout}
60+
*/
61+
public static Layout forType(LayoutType type) {
62+
switch (type) {
63+
case JAR:
64+
return new Layouts.Jar();
65+
case WAR:
66+
return new Layouts.War();
67+
case ZIP:
68+
return new Layouts.Expanded();
69+
case MODULE:
70+
return new Layouts.Module();
71+
default:
72+
return new Layouts.None();
73+
}
5474
}
5575

5676
/**
@@ -65,7 +85,7 @@ public static LayoutFactory getDefaultLayoutFactory() {
6585
if (factories.size() == 1) {
6686
return factories.get(0);
6787
}
68-
return new DefaultLayoutFactory();
88+
return null;
6989
}
7090

7191
/**

spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/LayoutTypeTests.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +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;
4546
import org.springframework.boot.loader.tools.LayoutFactory;
4647
import org.springframework.boot.loader.tools.LayoutType;
4748
import org.springframework.boot.loader.tools.Layouts;
@@ -234,12 +235,19 @@ private File getTargetFile() {
234235
private Repackager getRepackager(File source) {
235236
Repackager repackager = new LoggingRepackager(source, getLog());
236237
repackager.setMainClass(this.mainClass);
237-
if (this.layout == null) {
238-
this.layout = LayoutType.forFile(source);
238+
Layout forUse;
239+
if (this.layoutFactory != null) {
240+
forUse = this.layoutFactory.getLayout();
239241
}
240-
getLog().info("Layout: " + this.layout);
242+
else if (this.layout == null) {
243+
forUse = Layouts.forFile(source);
244+
}
245+
else {
246+
forUse = Layouts.forType(this.layout);
247+
}
248+
getLog().info("Layout: " + forUse.getClass());
241249
try {
242-
repackager.setLayout(this.layoutFactory.getLayout(this.layout));
250+
repackager.setLayout(forUse);
243251
}
244252
catch (Exception e) {
245253
throw new IllegalStateException("Cannot create layout", e);

0 commit comments

Comments
 (0)