Commit 081ef2d9 authored by Andy Wilkinson's avatar Andy Wilkinson

Update BootJar to support Gradle's configuration cache

See gh-22922
parent c8285219
...@@ -19,6 +19,7 @@ package org.springframework.boot.gradle.tasks.bundling; ...@@ -19,6 +19,7 @@ package org.springframework.boot.gradle.tasks.bundling;
import java.io.File; import java.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.function.Function;
import org.gradle.api.Action; import org.gradle.api.Action;
import org.gradle.api.Project; import org.gradle.api.Project;
...@@ -72,7 +73,7 @@ public class BootJar extends Jar implements BootArchive { ...@@ -72,7 +73,7 @@ public class BootJar extends Jar implements BootArchive {
* Creates a new {@code BootJar} task. * Creates a new {@code BootJar} task.
*/ */
public BootJar() { public BootJar() {
this.support = new BootArchiveSupport(LAUNCHER, this::isLibrary, this::resolveZipCompression); this.support = new BootArchiveSupport(LAUNCHER, new LibrarySpec(), new ZipCompressionResolver());
this.bootInfSpec = getProject().copySpec().into("BOOT-INF"); this.bootInfSpec = getProject().copySpec().into("BOOT-INF");
this.mainClass = getProject().getObjects().property(String.class); this.mainClass = getProject().getObjects().property(String.class);
configureBootInfSpec(this.bootInfSpec); configureBootInfSpec(this.bootInfSpec);
...@@ -325,4 +326,22 @@ public class BootJar extends Jar implements BootArchive { ...@@ -325,4 +326,22 @@ public class BootJar extends Jar implements BootArchive {
return this.resolvedDependencies; return this.resolvedDependencies;
} }
private final class LibrarySpec implements Spec<FileCopyDetails> {
@Override
public boolean isSatisfiedBy(FileCopyDetails details) {
return isLibrary(details);
}
}
private final class ZipCompressionResolver implements Function<FileCopyDetails, ZipCompression> {
@Override
public ZipCompression apply(FileCopyDetails details) {
return resolveZipCompression(details);
}
}
} }
...@@ -32,7 +32,6 @@ import org.gradle.testkit.runner.TaskOutcome; ...@@ -32,7 +32,6 @@ import org.gradle.testkit.runner.TaskOutcome;
import org.gradle.testkit.runner.UnexpectedBuildFailure; import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.TestTemplate;
import org.springframework.boot.gradle.junit.GradleCompatibility;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.loader.tools.FileUtils; import org.springframework.boot.loader.tools.FileUtils;
...@@ -43,7 +42,6 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -43,7 +42,6 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@GradleCompatibility
abstract class AbstractBootArchiveIntegrationTests { abstract class AbstractBootArchiveIntegrationTests {
private final String taskName; private final String taskName;
...@@ -99,25 +97,25 @@ abstract class AbstractBootArchiveIntegrationTests { ...@@ -99,25 +97,25 @@ abstract class AbstractBootArchiveIntegrationTests {
@TestTemplate @TestTemplate
void notUpToDateWhenLaunchScriptWasNotIncludedAndThenIsIncluded() { void notUpToDateWhenLaunchScriptWasNotIncludedAndThenIsIncluded() {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName).getOutcome()) assertThat(this.gradleBuild.scriptProperty("launchScript", "").build(this.taskName).task(":" + this.taskName)
.isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PincludeLaunchScript=true", this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); .getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.scriptProperty("launchScript", "launchScript()").build(this.taskName)
.task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
} }
@TestTemplate @TestTemplate
void notUpToDateWhenLaunchScriptWasIncludedAndThenIsNotIncluded() { void notUpToDateWhenLaunchScriptWasIncludedAndThenIsNotIncluded() {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName).getOutcome()) assertThat(this.gradleBuild.scriptProperty("launchScript", "launchScript()").build(this.taskName)
.isEqualTo(TaskOutcome.SUCCESS); .task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PincludeLaunchScript=true", this.taskName).task(":" + this.taskName) assertThat(this.gradleBuild.scriptProperty("launchScript", "").build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); .getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
} }
@TestTemplate @TestTemplate
void notUpToDateWhenLaunchScriptPropertyChanges() { void notUpToDateWhenLaunchScriptPropertyChanges() {
assertThat(this.gradleBuild.build("-PincludeLaunchScript=true", "-PlaunchScriptProperty=foo", this.taskName) assertThat(this.gradleBuild.scriptProperty("launchScriptProperty", "alpha").build(this.taskName)
.task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS); .task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PincludeLaunchScript=true", "-PlaunchScriptProperty=bar", this.taskName) assertThat(this.gradleBuild.scriptProperty("launchScriptProperty", "bravo").build(this.taskName)
.task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS); .task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
} }
......
...@@ -43,6 +43,7 @@ import org.gradle.testkit.runner.TaskOutcome; ...@@ -43,6 +43,7 @@ import org.gradle.testkit.runner.TaskOutcome;
import org.gradle.testkit.runner.UnexpectedBuildFailure; import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.TestTemplate;
import org.springframework.boot.gradle.junit.GradleCompatibility;
import org.springframework.boot.loader.tools.JarModeLibrary; import org.springframework.boot.loader.tools.JarModeLibrary;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -55,43 +56,39 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -55,43 +56,39 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Madhura Bhave * @author Madhura Bhave
* @author Paddy Drury * @author Paddy Drury
*/ */
@GradleCompatibility(configurationCache = true)
class BootJarIntegrationTests extends AbstractBootArchiveIntegrationTests { class BootJarIntegrationTests extends AbstractBootArchiveIntegrationTests {
BootJarIntegrationTests() { BootJarIntegrationTests() {
super("bootJar", "BOOT-INF/lib/", "BOOT-INF/classes/"); super("bootJar", "BOOT-INF/lib/", "BOOT-INF/classes/");
} }
@TestTemplate
void upToDateWhenBuiltTwiceWithLayers() throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.UP_TO_DATE);
}
@TestTemplate @TestTemplate
void upToDateWhenBuiltWithDefaultLayeredAndThenWithExplicitLayered() void upToDateWhenBuiltWithDefaultLayeredAndThenWithExplicitLayered()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure { throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(this.gradleBuild.scriptProperty("layered", "").build("bootJar").task(":bootJar").getOutcome())
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome()) .isEqualTo(TaskOutcome.SUCCESS);
.isEqualTo(TaskOutcome.UP_TO_DATE); assertThat(
this.gradleBuild.scriptProperty("layered", "layered {}").build("bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.UP_TO_DATE);
} }
@TestTemplate @TestTemplate
void notUpToDateWhenBuiltWithoutLayersAndThenWithLayers() void notUpToDateWhenBuiltWithoutLayersAndThenWithLayers()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure { throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "-PdisableLayers=true", "bootJar").task(":bootJar") assertThat(this.gradleBuild.scriptProperty("layerEnablement", "enabled = false").build("bootJar")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); .task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome()) assertThat(this.gradleBuild.scriptProperty("layerEnablement", "enabled = true").build("bootJar")
.isEqualTo(TaskOutcome.SUCCESS); .task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
} }
@TestTemplate @TestTemplate
void notUpToDateWhenBuiltWithLayerToolsAndThenWithoutLayerTools() void notUpToDateWhenBuiltWithLayerToolsAndThenWithoutLayerTools()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure { throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(this.gradleBuild.scriptProperty("layerTools", "").build("bootJar").task(":bootJar").getOutcome())
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "-PexcludeTools=true", "bootJar").task(":bootJar") .isEqualTo(TaskOutcome.SUCCESS);
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(this.gradleBuild.scriptProperty("layerTools", "includeLayerTools = false").build("bootJar")
.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
} }
@TestTemplate @TestTemplate
......
...@@ -16,11 +16,14 @@ ...@@ -16,11 +16,14 @@
package org.springframework.boot.gradle.tasks.bundling; package org.springframework.boot.gradle.tasks.bundling;
import org.springframework.boot.gradle.junit.GradleCompatibility;
/** /**
* Integration tests for {@link BootJar}. * Integration tests for {@link BootJar}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@GradleCompatibility
class BootWarIntegrationTests extends AbstractBootArchiveIntegrationTests { class BootWarIntegrationTests extends AbstractBootArchiveIntegrationTests {
BootWarIntegrationTests() { BootWarIntegrationTests() {
......
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClass = 'com.example.Application'
}
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClass = 'com.example.Application'
layered {
{layerEnablement}
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClass = 'com.example.Application'
launchScript {
properties 'prop' : '{launchScriptProperty}'
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClassName = 'com.example.Application'
launchScript()
}
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClass = 'com.example.Application'
if (project.hasProperty('includeLaunchScript') ? includeLaunchScript : false) {
launchScript {
properties 'prop' : project.hasProperty('launchScriptProperty') ? launchScriptProperty : 'default'
}
}
if (project.hasProperty('customizeLayered') && project.getProperty('customizeLayered')) {
layered {
includeLayerTools = project.hasProperty('excludeTools') && project.getProperty('excludeTools') ? false : true
enabled = project.hasProperty('disableLayers') && project.getProperty('disableLayers') ? false : true
}
}
}
plugins {
id 'war'
id 'org.springframework.boot' version '{version}'
}
bootWar {
mainClassName = 'com.example.Application'
launchScript {
properties 'prop' : '{launchScriptProperty}'
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment