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 8fffb5677af..8bea9ef8036 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 @@ -46,6 +46,8 @@ public class ProjectCreator { } GradleConnector gradleConnector = GradleConnector.newConnector(); + gradleConnector.useGradleVersion("1.12"); + ((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 new file mode 100644 index 00000000000..5cd2f78ab47 --- /dev/null +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/SpringLoadedTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.gradle; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.gradle.tooling.ProjectConnection; +import org.junit.Test; +import org.springframework.boot.dependency.tools.ManagedDependencies; + +import static org.junit.Assert.fail; + +/** + * Integration tests for the Gradle plugin's Spring Loaded support + * + * @author Andy Wilkinson + */ +public class SpringLoadedTests { + + private static final String BOOT_VERSION = ManagedDependencies.get() + .find("spring-boot").getVersion(); + + private static final String SPRING_LOADED_VERSION = ManagedDependencies.get() + .find("springloaded").getVersion(); + + @Test + public void defaultJvmArgsArePreservedWhenLoadedAgentIsConfigured() + throws IOException { + ProjectConnection project = new ProjectCreator() + .createProject("spring-loaded-jvm-args"); + project.newBuild() + .forTasks("bootRun") + .withArguments("-PbootVersion=" + BOOT_VERSION, + "-PspringLoadedVersion=" + SPRING_LOADED_VERSION, "--stacktrace") + .run(); + + List output = getOutput(); + assertOutputContains("-DSOME_ARG=someValue", output); + assertOutputContains("-Xverify:none", output); + 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"))); + try { + List lines = new ArrayList(); + + String line; + + while ((line = reader.readLine()) != null) { + lines.add(line); + } + return lines; + } + finally { + reader.close(); + } + } + + private void assertOutputContains(String requiredOutput, List actualOutput) { + for (String line : actualOutput) { + if (line.equals(requiredOutput)) { + return; + } + } + fail("Required output '" + requiredOutput + "' not found in " + actualOutput); + } + + private void assertOutputMatches(String requiredPattern, List actualOutput) { + for (String line : actualOutput) { + if (line.matches(requiredPattern)) { + return; + } + } + fail("Required pattern '" + requiredPattern + "' not matched in " + actualOutput); + } +} diff --git a/spring-boot-integration-tests/src/test/resources/spring-loaded-jvm-args/build.gradle b/spring-boot-integration-tests/src/test/resources/spring-loaded-jvm-args/build.gradle new file mode 100644 index 00000000000..7cc675f6ab8 --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/spring-loaded-jvm-args/build.gradle @@ -0,0 +1,30 @@ +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") +} + +applicationDefaultJvmArgs = [ + "-DSOME_ARG=someValue" +] + +jar { + baseName = 'spring-loaded-jvm-args' +} \ No newline at end of file diff --git a/spring-boot-integration-tests/src/test/resources/spring-loaded-jvm-args/src/main/java/test/Application.java b/spring-boot-integration-tests/src/test/resources/spring-loaded-jvm-args/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-jvm-args/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/agent/AgentTasksEnhancer.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/agent/AgentTasksEnhancer.java index b830cd9c64a..ad4ea061cc1 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/agent/AgentTasksEnhancer.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/agent/AgentTasksEnhancer.java @@ -108,6 +108,11 @@ public class AgentTasksEnhancer implements Action { if (this.noverify != null && this.noverify) { exec.jvmArgs("-noverify"); } + Iterable defaultJvmArgs = exec.getConventionMapping().getConventionValue( + null, "jvmArgs", false); + if (defaultJvmArgs != null) { + exec.jvmArgs(defaultJvmArgs); + } } }