Skip to content

Commit c7e8198

Browse files
author
Dave Syer
committed
Locate a default LayoutFactory in spring.factories if possible
As a courtesy to the user, if there is only one LayoutFactory defined in spring.factories then there is no need to explicitly configure it.
1 parent 0789ff9 commit c7e8198

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ springBoot {
563563
}
564564
----
565565

566+
If there is only one custom `LayoutFactory` on the build classpath and
567+
it is listed in `META-INF/spring.factories` then it is unnecessary to
568+
explicitly set it in the `springBoot` configuration.
566569

567570
[[build-tool-plugins-understanding-the-gradle-plugin]]
568571
=== Understanding how the Gradle plugin works

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import org.gradle.api.plugins.JavaPlugin;
2727

2828
import org.springframework.boot.gradle.buildinfo.BuildInfo;
29-
import org.springframework.boot.loader.tools.DefaultLayoutFactory;
3029
import org.springframework.boot.loader.tools.Layout;
3130
import org.springframework.boot.loader.tools.LayoutFactory;
3231
import org.springframework.boot.loader.tools.LayoutType;
32+
import org.springframework.boot.loader.tools.Layouts;
3333

3434
/**
3535
* Gradle DSL Extension for 'Spring Boot'. Most of the time Spring Boot can guess the
@@ -96,7 +96,7 @@ public class SpringBootPluginExtension {
9696
/**
9797
* The layout factory to use to convert a layout type into an actual layout.
9898
*/
99-
LayoutFactory layoutFactory = new DefaultLayoutFactory();
99+
LayoutFactory layoutFactory = Layouts.getDefaultLayoutFactory();
100100

101101
/**
102102
* Libraries that must be unpacked from fat jars in order to run. Use Strings in the

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import java.util.Collections;
2222
import java.util.HashMap;
2323
import java.util.HashSet;
24+
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Set;
2627

28+
import org.springframework.core.io.support.SpringFactoriesLoader;
29+
2730
/**
2831
* Common {@link Layout}s.
2932
*
@@ -47,7 +50,22 @@ private Layouts() {
4750
* @return a {@link Layout}
4851
*/
4952
public static Layout forFile(File file) {
50-
return new DefaultLayoutFactory().getLayout(LayoutType.forFile(file));
53+
return Layouts.getDefaultLayoutFactory().getLayout(LayoutType.forFile(file));
54+
}
55+
56+
/**
57+
* Gets a default layout factory, trying first to find a unique one in spring
58+
* factories, and then falling back to {@link DefaultLayoutFactory} if there isn't
59+
* one.
60+
* @return the default layout factory
61+
*/
62+
public static LayoutFactory getDefaultLayoutFactory() {
63+
List<LayoutFactory> factories = SpringFactoriesLoader
64+
.loadFactories(LayoutFactory.class, null);
65+
if (factories.size() == 1) {
66+
return factories.get(0);
67+
}
68+
return new DefaultLayoutFactory();
5169
}
5270

5371
/**

spring-boot-tools/spring-boot-loader-tools/src/test/resources/META-INF/spring.factories

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
4242

4343
import org.springframework.boot.loader.tools.DefaultLaunchScript;
44-
import org.springframework.boot.loader.tools.DefaultLayoutFactory;
4544
import org.springframework.boot.loader.tools.LaunchScript;
4645
import org.springframework.boot.loader.tools.LayoutFactory;
4746
import org.springframework.boot.loader.tools.LayoutType;
47+
import org.springframework.boot.loader.tools.Layouts;
4848
import org.springframework.boot.loader.tools.Libraries;
4949
import org.springframework.boot.loader.tools.Repackager;
5050

@@ -139,7 +139,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
139139
* @since 1.0
140140
*/
141141
@Parameter
142-
private LayoutFactory layoutFactory = new DefaultLayoutFactory();
142+
private LayoutFactory layoutFactory = Layouts.getDefaultLayoutFactory();
143143

144144
/**
145145
* A list of the libraries that must be unpacked from fat jars in order to run.

spring-boot-tools/spring-boot-maven-plugin/src/site/apt/examples/custom-layout.apt.vm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
---
5050

5151
The layout is provided as an implementation of <<LayoutFactory>>
52-
(from spring-boot-loader-tools) explicitly specified in the pom.
53-
52+
(from spring-boot-loader-tools) explicitly specified in the pom. If
53+
there is only one custom `LayoutFactory` on the plugin classpath and
54+
it is listed in `META-INF/spring.factories` then it is unnecessary
55+
to explicitly set it in the plugin configuration.
5456

5557

5658

0 commit comments

Comments
 (0)