Skip to content

Commit e607c68

Browse files
Polish Gradle layer configuration DSL
This commit modifies the DSL for custom layer configuration in the Gradle plugin to avoid duplication of terms that could be confusing. Fixes gh-20563
1 parent 06cefab commit e607c68

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/packaging/boot-jar-layered-custom.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bootJar {
1010
// tag::layered[]
1111
bootJar {
1212
layers {
13-
layers "dependencies", "snapshot-dependencies", "resources", "application"
13+
layersOrder "dependencies", "snapshot-dependencies", "resources", "application"
1414
libraries {
1515
layerContent("snapshot-dependencies") {
1616
coordinates {

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/packaging/boot-jar-layered-custom.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ plugins {
88
// tag::layered[]
99
tasks.getByName<BootJar>("bootJar") {
1010
layers {
11-
includeLayerTools = false
12-
layers("dependencies", "snapshot-dependencies", "resources", "application")
11+
layersOrder("dependencies", "snapshot-dependencies", "resources", "application")
1312
libraries {
1413
layerContent("snapshot-dependencies") {
1514
coordinates {

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ private void applyLayers() {
208208
return;
209209
}
210210

211-
if (this.layerConfiguration.getLayers() == null || this.layerConfiguration.getLayers().isEmpty()) {
211+
if (this.layerConfiguration.getLayersOrder() == null || this.layerConfiguration.getLayersOrder().isEmpty()) {
212212
this.layers = Layers.IMPLICIT;
213213
}
214214
else {
215-
List<Layer> customLayers = this.layerConfiguration.getLayers().stream().map(Layer::new)
215+
List<Layer> customLayers = this.layerConfiguration.getLayersOrder().stream().map(Layer::new)
216216
.collect(Collectors.toList());
217217
this.layers = new CustomLayers(customLayers, this.layerConfiguration.getClasses(),
218218
this.layerConfiguration.getLibraries());

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/LayerConfiguration.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class LayerConfiguration {
4444

4545
private boolean includeLayerTools = true;
4646

47-
private List<String> layerNames = new ArrayList<>();
47+
private List<String> layersOrder = new ArrayList<>();
4848

4949
private List<ResourceStrategy> resourceStrategies = new ArrayList<>();
5050

@@ -66,16 +66,16 @@ public void setIncludeLayerTools(boolean includeLayerTools) {
6666
}
6767

6868
@Input
69-
public List<String> getLayers() {
70-
return this.layerNames;
69+
public List<String> getLayersOrder() {
70+
return this.layersOrder;
7171
}
7272

73-
public void layers(String... layers) {
74-
this.layerNames = Arrays.asList(layers);
73+
public void layersOrder(String... layers) {
74+
this.layersOrder = Arrays.asList(layers);
7575
}
7676

77-
public void layers(List<String> layers) {
78-
this.layerNames = layers;
77+
public void layersOrder(List<String> layers) {
78+
this.layersOrder = layers;
7979
}
8080

8181
@Input
@@ -84,10 +84,12 @@ public List<ResourceStrategy> getClasses() {
8484
}
8585

8686
public void classes(ResourceStrategy... resourceStrategies) {
87+
assertLayersOrderConfigured();
8788
this.resourceStrategies = Arrays.asList(resourceStrategies);
8889
}
8990

9091
public void classes(Action<LayerConfiguration> config) {
92+
assertLayersOrderConfigured();
9193
this.strategySpec = StrategySpec.forResources();
9294
config.execute(this);
9395
}
@@ -98,14 +100,20 @@ public List<LibraryStrategy> getLibraries() {
98100
}
99101

100102
public void libraries(LibraryStrategy... strategies) {
103+
assertLayersOrderConfigured();
101104
this.libraryStrategies = Arrays.asList(strategies);
102105
}
103106

104107
public void libraries(Action<LayerConfiguration> configure) {
108+
assertLayersOrderConfigured();
105109
this.strategySpec = StrategySpec.forLibraries();
106110
configure.execute(this);
107111
}
108112

113+
private void assertLayersOrderConfigured() {
114+
Assert.state(!this.layersOrder.isEmpty(), "'layersOrder' must be configured before filters can be applied.");
115+
}
116+
109117
public void layerContent(String layerName, Action<LayerConfiguration> config) {
110118
this.strategySpec.newStrategy();
111119
config.execute(this);

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void whenJarIsLayeredThenContentsAreMovedToLayerDirectories() throws IOException
122122
@Test
123123
void whenJarIsLayeredWithCustomStrategiesThenContentsAreMovedToLayerDirectories() throws IOException {
124124
File jar = createLayeredJar((configuration) -> {
125-
configuration.layers("my-deps", "my-internal-deps", "my-snapshot-deps", "resources", "application");
125+
configuration.layersOrder("my-deps", "my-internal-deps", "my-snapshot-deps", "resources", "application");
126126
configuration.libraries(createLibraryStrategy("my-snapshot-deps", "com.example:*:*.SNAPSHOT"),
127127
createLibraryStrategy("my-internal-deps", "com.example:*:*"),
128128
createLibraryStrategy("my-deps", "*:*"));

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootJarIntegrationTests-customLayers.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
bootJar {
77
mainClassName = 'com.example.Application'
88
layers {
9-
layers "dependencies", "commons-dependencies", "snapshot-dependencies", "static", "app"
9+
layersOrder "dependencies", "commons-dependencies", "snapshot-dependencies", "static", "app"
1010
libraries {
1111
layerContent("snapshot-dependencies") { coordinates { include "*:*:*SNAPSHOT" } }
1212
layerContent("commons-dependencies") { coordinates { include "org.apache.commons:*" } }

0 commit comments

Comments
 (0)