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
This commit is contained in:
Scott Frederick 2020-03-18 16:21:40 -05:00
parent 06cefabb5b
commit e607c6842f
6 changed files with 21 additions and 14 deletions

View File

@ -10,7 +10,7 @@ bootJar {
// tag::layered[] // tag::layered[]
bootJar { bootJar {
layers { layers {
layers "dependencies", "snapshot-dependencies", "resources", "application" layersOrder "dependencies", "snapshot-dependencies", "resources", "application"
libraries { libraries {
layerContent("snapshot-dependencies") { layerContent("snapshot-dependencies") {
coordinates { coordinates {

View File

@ -8,8 +8,7 @@ plugins {
// tag::layered[] // tag::layered[]
tasks.getByName<BootJar>("bootJar") { tasks.getByName<BootJar>("bootJar") {
layers { layers {
includeLayerTools = false layersOrder("dependencies", "snapshot-dependencies", "resources", "application")
layers("dependencies", "snapshot-dependencies", "resources", "application")
libraries { libraries {
layerContent("snapshot-dependencies") { layerContent("snapshot-dependencies") {
coordinates { coordinates {

View File

@ -208,11 +208,11 @@ public class BootJar extends Jar implements BootArchive {
return; return;
} }
if (this.layerConfiguration.getLayers() == null || this.layerConfiguration.getLayers().isEmpty()) { if (this.layerConfiguration.getLayersOrder() == null || this.layerConfiguration.getLayersOrder().isEmpty()) {
this.layers = Layers.IMPLICIT; this.layers = Layers.IMPLICIT;
} }
else { else {
List<Layer> customLayers = this.layerConfiguration.getLayers().stream().map(Layer::new) List<Layer> customLayers = this.layerConfiguration.getLayersOrder().stream().map(Layer::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
this.layers = new CustomLayers(customLayers, this.layerConfiguration.getClasses(), this.layers = new CustomLayers(customLayers, this.layerConfiguration.getClasses(),
this.layerConfiguration.getLibraries()); this.layerConfiguration.getLibraries());

View File

@ -44,7 +44,7 @@ public class LayerConfiguration {
private boolean includeLayerTools = true; private boolean includeLayerTools = true;
private List<String> layerNames = new ArrayList<>(); private List<String> layersOrder = new ArrayList<>();
private List<ResourceStrategy> resourceStrategies = new ArrayList<>(); private List<ResourceStrategy> resourceStrategies = new ArrayList<>();
@ -66,16 +66,16 @@ public class LayerConfiguration {
} }
@Input @Input
public List<String> getLayers() { public List<String> getLayersOrder() {
return this.layerNames; return this.layersOrder;
} }
public void layers(String... layers) { public void layersOrder(String... layers) {
this.layerNames = Arrays.asList(layers); this.layersOrder = Arrays.asList(layers);
} }
public void layers(List<String> layers) { public void layersOrder(List<String> layers) {
this.layerNames = layers; this.layersOrder = layers;
} }
@Input @Input
@ -84,10 +84,12 @@ public class LayerConfiguration {
} }
public void classes(ResourceStrategy... resourceStrategies) { public void classes(ResourceStrategy... resourceStrategies) {
assertLayersOrderConfigured();
this.resourceStrategies = Arrays.asList(resourceStrategies); this.resourceStrategies = Arrays.asList(resourceStrategies);
} }
public void classes(Action<LayerConfiguration> config) { public void classes(Action<LayerConfiguration> config) {
assertLayersOrderConfigured();
this.strategySpec = StrategySpec.forResources(); this.strategySpec = StrategySpec.forResources();
config.execute(this); config.execute(this);
} }
@ -98,14 +100,20 @@ public class LayerConfiguration {
} }
public void libraries(LibraryStrategy... strategies) { public void libraries(LibraryStrategy... strategies) {
assertLayersOrderConfigured();
this.libraryStrategies = Arrays.asList(strategies); this.libraryStrategies = Arrays.asList(strategies);
} }
public void libraries(Action<LayerConfiguration> configure) { public void libraries(Action<LayerConfiguration> configure) {
assertLayersOrderConfigured();
this.strategySpec = StrategySpec.forLibraries(); this.strategySpec = StrategySpec.forLibraries();
configure.execute(this); configure.execute(this);
} }
private void assertLayersOrderConfigured() {
Assert.state(!this.layersOrder.isEmpty(), "'layersOrder' must be configured before filters can be applied.");
}
public void layerContent(String layerName, Action<LayerConfiguration> config) { public void layerContent(String layerName, Action<LayerConfiguration> config) {
this.strategySpec.newStrategy(); this.strategySpec.newStrategy();
config.execute(this); config.execute(this);

View File

@ -122,7 +122,7 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
@Test @Test
void whenJarIsLayeredWithCustomStrategiesThenContentsAreMovedToLayerDirectories() throws IOException { void whenJarIsLayeredWithCustomStrategiesThenContentsAreMovedToLayerDirectories() throws IOException {
File jar = createLayeredJar((configuration) -> { File jar = createLayeredJar((configuration) -> {
configuration.layers("my-deps", "my-internal-deps", "my-snapshot-deps", "resources", "application"); configuration.layersOrder("my-deps", "my-internal-deps", "my-snapshot-deps", "resources", "application");
configuration.libraries(createLibraryStrategy("my-snapshot-deps", "com.example:*:*.SNAPSHOT"), configuration.libraries(createLibraryStrategy("my-snapshot-deps", "com.example:*:*.SNAPSHOT"),
createLibraryStrategy("my-internal-deps", "com.example:*:*"), createLibraryStrategy("my-internal-deps", "com.example:*:*"),
createLibraryStrategy("my-deps", "*:*")); createLibraryStrategy("my-deps", "*:*"));

View File

@ -6,7 +6,7 @@ plugins {
bootJar { bootJar {
mainClassName = 'com.example.Application' mainClassName = 'com.example.Application'
layers { layers {
layers "dependencies", "commons-dependencies", "snapshot-dependencies", "static", "app" layersOrder "dependencies", "commons-dependencies", "snapshot-dependencies", "static", "app"
libraries { libraries {
layerContent("snapshot-dependencies") { coordinates { include "*:*:*SNAPSHOT" } } layerContent("snapshot-dependencies") { coordinates { include "*:*:*SNAPSHOT" } }
layerContent("commons-dependencies") { coordinates { include "org.apache.commons:*" } } layerContent("commons-dependencies") { coordinates { include "org.apache.commons:*" } }