From 06ffd9dd860c24f9de00ae5e139d5fa1479c2810 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sun, 7 Sep 2014 18:23:16 -0500 Subject: [PATCH] Fix configuration of Spring Loaded on Gradle 1.6 The applicationDefaultJvmArgs property was added in Gradle 1.7. This commit updates RunPluginFeatures to access the property defensively so that the plugin can be used with Gradle 1.6. Fixes gh-1511 --- .../boot/gradle/ProjectCreator.java | 12 ++++++++- .../boot/gradle/SpringLoadedTests.java | 15 +++++++++++ .../spring-loaded-old-gradle/build.gradle | 26 +++++++++++++++++++ .../src/main/java/test/Application.java | 25 ++++++++++++++++++ .../boot/gradle/run/RunPluginFeatures.java | 10 +++++-- 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/build.gradle create mode 100644 spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/src/main/java/test/Application.java diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/ProjectCreator.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/ProjectCreator.java index 8bea9ef8036..ff531c40124 100644 --- a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/ProjectCreator.java +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/ProjectCreator.java @@ -30,6 +30,16 @@ import org.springframework.util.FileSystemUtils; */ public class ProjectCreator { + private String gradleVersion; + + public ProjectCreator() { + this("1.12"); + } + + public ProjectCreator(String gradleVersion) { + this.gradleVersion = gradleVersion; + } + public ProjectConnection createProject(String name) throws IOException { File projectDirectory = new File("target/" + name); projectDirectory.mkdirs(); @@ -46,7 +56,7 @@ public class ProjectCreator { } GradleConnector gradleConnector = GradleConnector.newConnector(); - gradleConnector.useGradleVersion("1.12"); + gradleConnector.useGradleVersion(this.gradleVersion); ((DefaultGradleConnector) gradleConnector).embedded(true); return gradleConnector.forProjectDirectory(projectDirectory).connect(); diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/SpringLoadedTests.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/SpringLoadedTests.java index 5cd2f78ab47..996cce67bc4 100644 --- a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/SpringLoadedTests.java +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/SpringLoadedTests.java @@ -60,6 +60,21 @@ public class SpringLoadedTests { "-javaagent:.*springloaded-" + SPRING_LOADED_VERSION + ".jar", output); } + @Test + public void springLoadedCanBeUsedWithGradle16() throws IOException { + ProjectConnection project = new ProjectCreator("1.6") + .createProject("spring-loaded-old-gradle"); + project.newBuild() + .forTasks("bootRun") + .withArguments("-PbootVersion=" + BOOT_VERSION, + "-PspringLoadedVersion=" + SPRING_LOADED_VERSION, "--stacktrace") + .run(); + + List output = getOutput(); + assertOutputMatches( + "-javaagent:.*springloaded-" + SPRING_LOADED_VERSION + ".jar", output); + } + private List getOutput() throws IOException { BufferedReader reader = new BufferedReader(new FileReader(new File( "target/spring-loaded-jvm-args/build/output.txt"))); diff --git a/spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/build.gradle b/spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/build.gradle new file mode 100644 index 00000000000..70d15523ad3 --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/build.gradle @@ -0,0 +1,26 @@ +buildscript { + repositories { + mavenLocal() + mavenCentral() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}") + classpath("org.springframework:springloaded:${project.springLoadedVersion}") + } +} + +apply plugin: 'java' +apply plugin: 'spring-boot' + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + compile("org.springframework.boot:spring-boot-starter") +} + +jar { + baseName = 'spring-loaded-old-gradle' +} \ No newline at end of file diff --git a/spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/src/main/java/test/Application.java b/spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/src/main/java/test/Application.java new file mode 100644 index 00000000000..12b8be6e6dc --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/spring-loaded-old-gradle/src/main/java/test/Application.java @@ -0,0 +1,25 @@ +package test; + +import java.io.File; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.lang.management.ManagementFactory; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; + +public class Application { + + public static void main(String[] args) throws Exception { + PrintWriter writer = new PrintWriter(new FileWriter(new File("build/output.txt"))); + for (String argument: ManagementFactory.getRuntimeMXBean().getInputArguments()) { + writer.println(argument); + } + writer.close(); + } +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java index 99c9b84d76e..8f6feb2a503 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java @@ -16,6 +16,7 @@ package org.springframework.boot.gradle.run; +import java.util.Collections; import java.util.concurrent.Callable; import org.gradle.api.Action; @@ -74,9 +75,14 @@ public class RunPluginFeatures implements PluginFeatures { run.getConventionMapping().map("jvmArgs", new Callable() { @Override public Object call() throws Exception { - return project.property("applicationDefaultJvmArgs"); + if (project.hasProperty("applicationDefaultJvmArgs")) { + return project.property("applicationDefaultJvmArgs"); + } + else { + return Collections.emptyList(); + } + } }); } - }