Skip to content

Commit

Permalink
Fix reflective access to archiveBaseName property
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Nov 27, 2019
1 parent a58ae98 commit 76f03a8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 18 deletions.
Expand Up @@ -24,6 +24,7 @@
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.jvm.tasks.Jar;
Expand Down Expand Up @@ -128,11 +129,12 @@ private Jar findArtifactTask() {
return (Jar) this.project.getTasks().findByName("bootJar");
}

@SuppressWarnings("unchecked")
private static String getArchiveBaseName(AbstractArchiveTask task) {
try {
Method method = findMethod(task.getClass(), "getArchiveBaseName");
if (method != null) {
return (String) method.invoke(task);
return ((Property<String>) method.invoke(task)).get();
}
}
catch (Exception ex) {
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.regex.Pattern;

import org.gradle.api.Project;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;

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

@SuppressWarnings("unchecked")
private static String getArchiveBaseName(AbstractArchiveTask task) {
try {
Method method = findMethod(task.getClass(), "getArchiveBaseName");
if (method != null) {
return (String) method.invoke(task);
return ((Property<String>) method.invoke(task)).get();
}
}
catch (Exception ex) {
Expand Down
Expand Up @@ -22,12 +22,12 @@
import java.util.Properties;

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.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.gradle.testkit.GradleBuildExtension;

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

Expand All @@ -37,12 +37,12 @@
*
* @author Andy Wilkinson
*/
@ExtendWith(GradleBuildExtension.class)
@ExtendWith(GradleCompatibilityExtension.class)
class BuildInfoDslIntegrationTests {

final GradleBuild gradleBuild = new GradleBuild();
GradleBuild gradleBuild;

@Test
@TestTemplate
void basicJar() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
Expand All @@ -53,7 +53,7 @@ void basicJar() throws IOException {
assertThat(properties).containsEntry("build.version", "1.0");
}

@Test
@TestTemplate
void jarWithCustomName() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
Expand All @@ -64,7 +64,7 @@ void jarWithCustomName() throws IOException {
assertThat(properties).containsEntry("build.version", "1.0");
}

@Test
@TestTemplate
void basicWar() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
Expand All @@ -75,7 +75,7 @@ void basicWar() throws IOException {
assertThat(properties).containsEntry("build.version", "1.0");
}

@Test
@TestTemplate
void warWithCustomName() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
Expand All @@ -86,7 +86,7 @@ void warWithCustomName() throws IOException {
assertThat(properties).containsEntry("build.version", "1.0");
}

@Test
@TestTemplate
void additionalProperties() throws IOException {
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
Expand All @@ -99,7 +99,7 @@ void additionalProperties() throws IOException {
assertThat(properties).containsEntry("build.b", "bravo");
}

@Test
@TestTemplate
void classesDependency() throws IOException {
assertThat(this.gradleBuild.build("classes", "--stacktrace").task(":bootBuildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
Expand Down
Expand Up @@ -36,7 +36,8 @@ public class MavenPluginActionIntegrationTests {

@TestTemplate
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");
}

}
Expand Up @@ -42,7 +42,8 @@ public class MavenIntegrationTests {

@TestTemplate
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(artifactWithSuffix("jar")).isFile();
assertThat(artifactWithSuffix("pom")).is(pomWith().groupId("com.example")
Expand All @@ -51,7 +52,8 @@ public void bootJarCanBeUploaded() throws FileNotFoundException, IOException {

@TestTemplate
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(artifactWithSuffix("war")).isFile();
assertThat(artifactWithSuffix("pom"))
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.util.GradleVersion;
import org.jetbrains.kotlin.cli.common.PropertiesKt;
import org.jetbrains.kotlin.compilerRunner.KotlinLogger;
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient;
Expand All @@ -44,6 +45,8 @@
import org.springframework.util.FileCopyUtils;
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}.
*
Expand All @@ -59,6 +62,8 @@ public class GradleBuild {

private String gradleVersion;

private GradleVersion expectDeprecationWarnings;

public GradleBuild() {
this(Dsl.GROOVY);
}
Expand Down Expand Up @@ -100,9 +105,19 @@ public GradleBuild script(String script) {
return this;
}

public GradleBuild expectDeprecationWarningsWithAtLeastVersion(String gradleVersion) {
this.expectDeprecationWarnings = GradleVersion.version(gradleVersion);
return this;
}

public BuildResult build(String... arguments) {
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) {
throw new RuntimeException(ex);
Expand Down
@@ -1,3 +1,5 @@
import org.gradle.util.GradleVersion

plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
Expand All @@ -7,7 +9,12 @@ group = 'com.example'
version = '1.0'

bootJar {
baseName = 'foo'
if (GradleVersion.current().compareTo(GradleVersion.version('6.0.0')) < 0) {
baseName = 'foo'
}
else {
archiveBaseName = 'foo'
}
}

springBoot {
Expand Down
@@ -1,3 +1,5 @@
import org.gradle.util.GradleVersion

plugins {
id 'war'
id 'org.springframework.boot' version '{version}'
Expand All @@ -7,7 +9,12 @@ group = 'com.example'
version = '1.0'

bootWar {
baseName = 'foo'
if (GradleVersion.current().compareTo(GradleVersion.version('6.0.0')) < 0) {
baseName = 'foo'
}
else {
archiveBaseName = 'foo'
}
}

springBoot {
Expand Down

0 comments on commit 76f03a8

Please sign in to comment.