Skip to content

Commit 76f03a8

Browse files
committed
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
1 parent a58ae98 commit 76f03a8

File tree

8 files changed

+54
-18
lines changed

8 files changed

+54
-18
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/dsl/SpringBootExtension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.gradle.api.plugins.BasePlugin;
2525
import org.gradle.api.plugins.JavaPlugin;
2626
import org.gradle.api.plugins.JavaPluginConvention;
27+
import org.gradle.api.provider.Property;
2728
import org.gradle.api.tasks.SourceSet;
2829
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
2930
import org.gradle.jvm.tasks.Jar;
@@ -128,11 +129,12 @@ private Jar findArtifactTask() {
128129
return (Jar) this.project.getTasks().findByName("bootJar");
129130
}
130131

132+
@SuppressWarnings("unchecked")
131133
private static String getArchiveBaseName(AbstractArchiveTask task) {
132134
try {
133135
Method method = findMethod(task.getClass(), "getArchiveBaseName");
134136
if (method != null) {
135-
return (String) method.invoke(task);
137+
return ((Property<String>) method.invoke(task)).get();
136138
}
137139
}
138140
catch (Exception ex) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.regex.Pattern;
2626

2727
import org.gradle.api.Project;
28+
import org.gradle.api.provider.Property;
2829
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
2930

3031
import org.springframework.boot.loader.tools.FileUtils;
@@ -57,11 +58,12 @@ public LaunchScriptConfiguration() {
5758
putIfMissing(this.properties, "initInfoDescription", augmentLineBreaks(project.getDescription()), baseName);
5859
}
5960

61+
@SuppressWarnings("unchecked")
6062
private static String getArchiveBaseName(AbstractArchiveTask task) {
6163
try {
6264
Method method = findMethod(task.getClass(), "getArchiveBaseName");
6365
if (method != null) {
64-
return (String) method.invoke(task);
66+
return ((Property<String>) method.invoke(task)).get();
6567
}
6668
}
6769
catch (Exception ex) {

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/dsl/BuildInfoDslIntegrationTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import java.util.Properties;
2323

2424
import org.gradle.testkit.runner.TaskOutcome;
25-
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.TestTemplate;
2626
import org.junit.jupiter.api.extension.ExtendWith;
2727

28+
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
2829
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
2930
import org.springframework.boot.gradle.testkit.GradleBuild;
30-
import org.springframework.boot.gradle.testkit.GradleBuildExtension;
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
3333

@@ -37,12 +37,12 @@
3737
*
3838
* @author Andy Wilkinson
3939
*/
40-
@ExtendWith(GradleBuildExtension.class)
40+
@ExtendWith(GradleCompatibilityExtension.class)
4141
class BuildInfoDslIntegrationTests {
4242

43-
final GradleBuild gradleBuild = new GradleBuild();
43+
GradleBuild gradleBuild;
4444

45-
@Test
45+
@TestTemplate
4646
void basicJar() throws IOException {
4747
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
4848
.isEqualTo(TaskOutcome.SUCCESS);
@@ -53,7 +53,7 @@ void basicJar() throws IOException {
5353
assertThat(properties).containsEntry("build.version", "1.0");
5454
}
5555

56-
@Test
56+
@TestTemplate
5757
void jarWithCustomName() throws IOException {
5858
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
5959
.isEqualTo(TaskOutcome.SUCCESS);
@@ -64,7 +64,7 @@ void jarWithCustomName() throws IOException {
6464
assertThat(properties).containsEntry("build.version", "1.0");
6565
}
6666

67-
@Test
67+
@TestTemplate
6868
void basicWar() throws IOException {
6969
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
7070
.isEqualTo(TaskOutcome.SUCCESS);
@@ -75,7 +75,7 @@ void basicWar() throws IOException {
7575
assertThat(properties).containsEntry("build.version", "1.0");
7676
}
7777

78-
@Test
78+
@TestTemplate
7979
void warWithCustomName() throws IOException {
8080
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
8181
.isEqualTo(TaskOutcome.SUCCESS);
@@ -86,7 +86,7 @@ void warWithCustomName() throws IOException {
8686
assertThat(properties).containsEntry("build.version", "1.0");
8787
}
8888

89-
@Test
89+
@TestTemplate
9090
void additionalProperties() throws IOException {
9191
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
9292
.isEqualTo(TaskOutcome.SUCCESS);
@@ -99,7 +99,7 @@ void additionalProperties() throws IOException {
9999
assertThat(properties).containsEntry("build.b", "bravo");
100100
}
101101

102-
@Test
102+
@TestTemplate
103103
void classesDependency() throws IOException {
104104
assertThat(this.gradleBuild.build("classes", "--stacktrace").task(":bootBuildInfo").getOutcome())
105105
.isEqualTo(TaskOutcome.SUCCESS);

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/MavenPluginActionIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public class MavenPluginActionIntegrationTests {
3636

3737
@TestTemplate
3838
public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() {
39-
assertThat(this.gradleBuild.build("conf2ScopeMappings").getOutput()).contains("Conf2ScopeMappings = 0");
39+
assertThat(this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("6.0.0").build("conf2ScopeMappings")
40+
.getOutput()).contains("Conf2ScopeMappings = 0");
4041
}
4142

4243
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class MavenIntegrationTests {
4242

4343
@TestTemplate
4444
public void bootJarCanBeUploaded() throws FileNotFoundException, IOException {
45-
BuildResult result = this.gradleBuild.build("uploadBootArchives");
45+
BuildResult result = this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("6.0.0")
46+
.build("uploadBootArchives");
4647
assertThat(result.task(":uploadBootArchives").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
4748
assertThat(artifactWithSuffix("jar")).isFile();
4849
assertThat(artifactWithSuffix("pom")).is(pomWith().groupId("com.example")
@@ -51,7 +52,8 @@ public void bootJarCanBeUploaded() throws FileNotFoundException, IOException {
5152

5253
@TestTemplate
5354
public void bootWarCanBeUploaded() throws IOException {
54-
BuildResult result = this.gradleBuild.build("uploadBootArchives");
55+
BuildResult result = this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("6.0.0")
56+
.build("uploadBootArchives");
5557
assertThat(result.task(":uploadBootArchives").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
5658
assertThat(artifactWithSuffix("war")).isFile();
5759
assertThat(artifactWithSuffix("pom"))

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.compress.archivers.ArchiveEntry;
3333
import org.gradle.testkit.runner.BuildResult;
3434
import org.gradle.testkit.runner.GradleRunner;
35+
import org.gradle.util.GradleVersion;
3536
import org.jetbrains.kotlin.cli.common.PropertiesKt;
3637
import org.jetbrains.kotlin.compilerRunner.KotlinLogger;
3738
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient;
@@ -44,6 +45,8 @@
4445
import org.springframework.util.FileCopyUtils;
4546
import org.springframework.util.FileSystemUtils;
4647

48+
import static org.assertj.core.api.Assertions.assertThat;
49+
4750
/**
4851
* A {@code GradleBuild} is used to run a Gradle build using {@link GradleRunner}.
4952
*
@@ -59,6 +62,8 @@ public class GradleBuild {
5962

6063
private String gradleVersion;
6164

65+
private GradleVersion expectDeprecationWarnings;
66+
6267
public GradleBuild() {
6368
this(Dsl.GROOVY);
6469
}
@@ -100,9 +105,19 @@ public GradleBuild script(String script) {
100105
return this;
101106
}
102107

108+
public GradleBuild expectDeprecationWarningsWithAtLeastVersion(String gradleVersion) {
109+
this.expectDeprecationWarnings = GradleVersion.version(gradleVersion);
110+
return this;
111+
}
112+
103113
public BuildResult build(String... arguments) {
104114
try {
105-
return prepareRunner(arguments).build();
115+
BuildResult result = prepareRunner(arguments).build();
116+
if (this.gradleVersion != null && this.expectDeprecationWarnings != null
117+
&& this.expectDeprecationWarnings.compareTo(GradleVersion.version(this.gradleVersion)) > 0) {
118+
assertThat(result.getOutput()).doesNotContain("Deprecated").doesNotContain("deprecated");
119+
}
120+
return result;
106121
}
107122
catch (Exception ex) {
108123
throw new RuntimeException(ex);

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/dsl/BuildInfoDslIntegrationTests-jarWithCustomName.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.gradle.util.GradleVersion
2+
13
plugins {
24
id 'java'
35
id 'org.springframework.boot' version '{version}'
@@ -7,7 +9,12 @@ group = 'com.example'
79
version = '1.0'
810

911
bootJar {
10-
baseName = 'foo'
12+
if (GradleVersion.current().compareTo(GradleVersion.version('6.0.0')) < 0) {
13+
baseName = 'foo'
14+
}
15+
else {
16+
archiveBaseName = 'foo'
17+
}
1118
}
1219

1320
springBoot {

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/dsl/BuildInfoDslIntegrationTests-warWithCustomName.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.gradle.util.GradleVersion
2+
13
plugins {
24
id 'war'
35
id 'org.springframework.boot' version '{version}'
@@ -7,7 +9,12 @@ group = 'com.example'
79
version = '1.0'
810

911
bootWar {
10-
baseName = 'foo'
12+
if (GradleVersion.current().compareTo(GradleVersion.version('6.0.0')) < 0) {
13+
baseName = 'foo'
14+
}
15+
else {
16+
archiveBaseName = 'foo'
17+
}
1118
}
1219

1320
springBoot {

0 commit comments

Comments
 (0)