Commit 76f03a8c authored by Andy Wilkinson's avatar Andy Wilkinson

Fix reflective access to archiveBaseName property

Previously, reflective access to the archiveBaseName property
incorrectly treated the property as a String. It should have been
treated as a Property<String>. This caused an exception to be thrown
and the deprecated baseName property to be used as a fallback.

This commit corrects the reflective access to the archiveBaseName
property. It also updates the tests to fail if a build outputs a
deprecation warning. Tests that use Gradle's Maven plugin have been
updated to expect deprecation warnings when run with Gradle 6.0 where
the plugin is deprecated. Tests that configure an archive's base name
have been updated to use archiveBaseName when running with Gradle 6.0
and later.

Closes gh-18663
parent a58ae98f
...@@ -24,6 +24,7 @@ import org.gradle.api.Project; ...@@ -24,6 +24,7 @@ import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.AbstractArchiveTask; import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.jvm.tasks.Jar; import org.gradle.jvm.tasks.Jar;
...@@ -128,11 +129,12 @@ public class SpringBootExtension { ...@@ -128,11 +129,12 @@ public class SpringBootExtension {
return (Jar) this.project.getTasks().findByName("bootJar"); return (Jar) this.project.getTasks().findByName("bootJar");
} }
@SuppressWarnings("unchecked")
private static String getArchiveBaseName(AbstractArchiveTask task) { private static String getArchiveBaseName(AbstractArchiveTask task) {
try { try {
Method method = findMethod(task.getClass(), "getArchiveBaseName"); Method method = findMethod(task.getClass(), "getArchiveBaseName");
if (method != null) { if (method != null) {
return (String) method.invoke(task); return ((Property<String>) method.invoke(task)).get();
} }
} }
catch (Exception ex) { catch (Exception ex) {
......
...@@ -25,6 +25,7 @@ import java.util.Map; ...@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.bundling.AbstractArchiveTask; import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.springframework.boot.loader.tools.FileUtils; import org.springframework.boot.loader.tools.FileUtils;
...@@ -57,11 +58,12 @@ public class LaunchScriptConfiguration implements Serializable { ...@@ -57,11 +58,12 @@ public class LaunchScriptConfiguration implements Serializable {
putIfMissing(this.properties, "initInfoDescription", augmentLineBreaks(project.getDescription()), baseName); putIfMissing(this.properties, "initInfoDescription", augmentLineBreaks(project.getDescription()), baseName);
} }
@SuppressWarnings("unchecked")
private static String getArchiveBaseName(AbstractArchiveTask task) { private static String getArchiveBaseName(AbstractArchiveTask task) {
try { try {
Method method = findMethod(task.getClass(), "getArchiveBaseName"); Method method = findMethod(task.getClass(), "getArchiveBaseName");
if (method != null) { if (method != null) {
return (String) method.invoke(task); return ((Property<String>) method.invoke(task)).get();
} }
} }
catch (Exception ex) { catch (Exception ex) {
......
...@@ -22,12 +22,12 @@ import java.io.IOException; ...@@ -22,12 +22,12 @@ import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo; import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.gradle.testkit.GradleBuildExtension;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -37,12 +37,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -37,12 +37,12 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@ExtendWith(GradleBuildExtension.class) @ExtendWith(GradleCompatibilityExtension.class)
class BuildInfoDslIntegrationTests { class BuildInfoDslIntegrationTests {
final GradleBuild gradleBuild = new GradleBuild(); GradleBuild gradleBuild;
@Test @TestTemplate
void basicJar() throws IOException { void basicJar() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome()) assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS); .isEqualTo(TaskOutcome.SUCCESS);
...@@ -53,7 +53,7 @@ class BuildInfoDslIntegrationTests { ...@@ -53,7 +53,7 @@ class BuildInfoDslIntegrationTests {
assertThat(properties).containsEntry("build.version", "1.0"); assertThat(properties).containsEntry("build.version", "1.0");
} }
@Test @TestTemplate
void jarWithCustomName() throws IOException { void jarWithCustomName() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome()) assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS); .isEqualTo(TaskOutcome.SUCCESS);
...@@ -64,7 +64,7 @@ class BuildInfoDslIntegrationTests { ...@@ -64,7 +64,7 @@ class BuildInfoDslIntegrationTests {
assertThat(properties).containsEntry("build.version", "1.0"); assertThat(properties).containsEntry("build.version", "1.0");
} }
@Test @TestTemplate
void basicWar() throws IOException { void basicWar() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome()) assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS); .isEqualTo(TaskOutcome.SUCCESS);
...@@ -75,7 +75,7 @@ class BuildInfoDslIntegrationTests { ...@@ -75,7 +75,7 @@ class BuildInfoDslIntegrationTests {
assertThat(properties).containsEntry("build.version", "1.0"); assertThat(properties).containsEntry("build.version", "1.0");
} }
@Test @TestTemplate
void warWithCustomName() throws IOException { void warWithCustomName() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome()) assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS); .isEqualTo(TaskOutcome.SUCCESS);
...@@ -86,7 +86,7 @@ class BuildInfoDslIntegrationTests { ...@@ -86,7 +86,7 @@ class BuildInfoDslIntegrationTests {
assertThat(properties).containsEntry("build.version", "1.0"); assertThat(properties).containsEntry("build.version", "1.0");
} }
@Test @TestTemplate
void additionalProperties() throws IOException { void additionalProperties() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome()) assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS); .isEqualTo(TaskOutcome.SUCCESS);
...@@ -99,7 +99,7 @@ class BuildInfoDslIntegrationTests { ...@@ -99,7 +99,7 @@ class BuildInfoDslIntegrationTests {
assertThat(properties).containsEntry("build.b", "bravo"); assertThat(properties).containsEntry("build.b", "bravo");
} }
@Test @TestTemplate
void classesDependency() throws IOException { void classesDependency() throws IOException {
assertThat(this.gradleBuild.build("classes", "--stacktrace").task(":bootBuildInfo").getOutcome()) assertThat(this.gradleBuild.build("classes", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS); .isEqualTo(TaskOutcome.SUCCESS);
......
...@@ -36,7 +36,8 @@ public class MavenPluginActionIntegrationTests { ...@@ -36,7 +36,8 @@ public class MavenPluginActionIntegrationTests {
@TestTemplate @TestTemplate
public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() { public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() {
assertThat(this.gradleBuild.build("conf2ScopeMappings").getOutput()).contains("Conf2ScopeMappings = 0"); assertThat(this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("6.0.0").build("conf2ScopeMappings")
.getOutput()).contains("Conf2ScopeMappings = 0");
} }
} }
...@@ -42,7 +42,8 @@ public class MavenIntegrationTests { ...@@ -42,7 +42,8 @@ public class MavenIntegrationTests {
@TestTemplate @TestTemplate
public void bootJarCanBeUploaded() throws FileNotFoundException, IOException { public void bootJarCanBeUploaded() throws FileNotFoundException, IOException {
BuildResult result = this.gradleBuild.build("uploadBootArchives"); BuildResult result = this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("6.0.0")
.build("uploadBootArchives");
assertThat(result.task(":uploadBootArchives").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(result.task(":uploadBootArchives").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(artifactWithSuffix("jar")).isFile(); assertThat(artifactWithSuffix("jar")).isFile();
assertThat(artifactWithSuffix("pom")).is(pomWith().groupId("com.example") assertThat(artifactWithSuffix("pom")).is(pomWith().groupId("com.example")
...@@ -51,7 +52,8 @@ public class MavenIntegrationTests { ...@@ -51,7 +52,8 @@ public class MavenIntegrationTests {
@TestTemplate @TestTemplate
public void bootWarCanBeUploaded() throws IOException { public void bootWarCanBeUploaded() throws IOException {
BuildResult result = this.gradleBuild.build("uploadBootArchives"); BuildResult result = this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("6.0.0")
.build("uploadBootArchives");
assertThat(result.task(":uploadBootArchives").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(result.task(":uploadBootArchives").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(artifactWithSuffix("war")).isFile(); assertThat(artifactWithSuffix("war")).isFile();
assertThat(artifactWithSuffix("pom")) assertThat(artifactWithSuffix("pom"))
......
...@@ -32,6 +32,7 @@ import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension; ...@@ -32,6 +32,7 @@ import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveEntry;
import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner; import org.gradle.testkit.runner.GradleRunner;
import org.gradle.util.GradleVersion;
import org.jetbrains.kotlin.cli.common.PropertiesKt; import org.jetbrains.kotlin.cli.common.PropertiesKt;
import org.jetbrains.kotlin.compilerRunner.KotlinLogger; import org.jetbrains.kotlin.compilerRunner.KotlinLogger;
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient; import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient;
...@@ -44,6 +45,8 @@ import org.springframework.boot.loader.tools.LaunchScript; ...@@ -44,6 +45,8 @@ import org.springframework.boot.loader.tools.LaunchScript;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* A {@code GradleBuild} is used to run a Gradle build using {@link GradleRunner}. * A {@code GradleBuild} is used to run a Gradle build using {@link GradleRunner}.
* *
...@@ -59,6 +62,8 @@ public class GradleBuild { ...@@ -59,6 +62,8 @@ public class GradleBuild {
private String gradleVersion; private String gradleVersion;
private GradleVersion expectDeprecationWarnings;
public GradleBuild() { public GradleBuild() {
this(Dsl.GROOVY); this(Dsl.GROOVY);
} }
...@@ -100,9 +105,19 @@ public class GradleBuild { ...@@ -100,9 +105,19 @@ public class GradleBuild {
return this; return this;
} }
public GradleBuild expectDeprecationWarningsWithAtLeastVersion(String gradleVersion) {
this.expectDeprecationWarnings = GradleVersion.version(gradleVersion);
return this;
}
public BuildResult build(String... arguments) { public BuildResult build(String... arguments) {
try { try {
return prepareRunner(arguments).build(); BuildResult result = prepareRunner(arguments).build();
if (this.gradleVersion != null && this.expectDeprecationWarnings != null
&& this.expectDeprecationWarnings.compareTo(GradleVersion.version(this.gradleVersion)) > 0) {
assertThat(result.getOutput()).doesNotContain("Deprecated").doesNotContain("deprecated");
}
return result;
} }
catch (Exception ex) { catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
......
import org.gradle.util.GradleVersion
plugins { plugins {
id 'java' id 'java'
id 'org.springframework.boot' version '{version}' id 'org.springframework.boot' version '{version}'
...@@ -7,7 +9,12 @@ group = 'com.example' ...@@ -7,7 +9,12 @@ group = 'com.example'
version = '1.0' version = '1.0'
bootJar { bootJar {
baseName = 'foo' if (GradleVersion.current().compareTo(GradleVersion.version('6.0.0')) < 0) {
baseName = 'foo'
}
else {
archiveBaseName = 'foo'
}
} }
springBoot { springBoot {
......
import org.gradle.util.GradleVersion
plugins { plugins {
id 'war' id 'war'
id 'org.springframework.boot' version '{version}' id 'org.springframework.boot' version '{version}'
...@@ -7,7 +9,12 @@ group = 'com.example' ...@@ -7,7 +9,12 @@ group = 'com.example'
version = '1.0' version = '1.0'
bootWar { bootWar {
baseName = 'foo' if (GradleVersion.current().compareTo(GradleVersion.version('6.0.0')) < 0) {
baseName = 'foo'
}
else {
archiveBaseName = 'foo'
}
} }
springBoot { springBoot {
......
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