Skip to content

Commit 9717b7d

Browse files
committed
Preserve JVM args when auto-configuring a Java agent in Gradle bootRun
Fixes #1411
1 parent c757824 commit 9717b7d

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/ProjectCreator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public ProjectConnection createProject(String name) throws IOException {
4646
}
4747

4848
GradleConnector gradleConnector = GradleConnector.newConnector();
49+
gradleConnector.useGradleVersion("1.12");
50+
4951
((DefaultGradleConnector) gradleConnector).embedded(true);
5052
return gradleConnector.forProjectDirectory(projectDirectory).connect();
5153
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.gradle;
18+
19+
import java.io.BufferedReader;
20+
import java.io.File;
21+
import java.io.FileReader;
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.gradle.tooling.ProjectConnection;
27+
import org.junit.Test;
28+
import org.springframework.boot.dependency.tools.ManagedDependencies;
29+
30+
import static org.junit.Assert.fail;
31+
32+
/**
33+
* Integration tests for the Gradle plugin's Spring Loaded support
34+
*
35+
* @author Andy Wilkinson
36+
*/
37+
public class SpringLoadedTests {
38+
39+
private static final String BOOT_VERSION = ManagedDependencies.get()
40+
.find("spring-boot").getVersion();
41+
42+
private static final String SPRING_LOADED_VERSION = ManagedDependencies.get()
43+
.find("springloaded").getVersion();
44+
45+
@Test
46+
public void defaultJvmArgsArePreservedWhenLoadedAgentIsConfigured()
47+
throws IOException {
48+
ProjectConnection project = new ProjectCreator()
49+
.createProject("spring-loaded-jvm-args");
50+
project.newBuild()
51+
.forTasks("bootRun")
52+
.withArguments("-PbootVersion=" + BOOT_VERSION,
53+
"-PspringLoadedVersion=" + SPRING_LOADED_VERSION, "--stacktrace")
54+
.run();
55+
56+
List<String> output = getOutput();
57+
assertOutputContains("-DSOME_ARG=someValue", output);
58+
assertOutputContains("-Xverify:none", output);
59+
assertOutputMatches(
60+
"-javaagent:.*springloaded-" + SPRING_LOADED_VERSION + ".jar", output);
61+
}
62+
63+
private List<String> getOutput() throws IOException {
64+
BufferedReader reader = new BufferedReader(new FileReader(new File(
65+
"target/spring-loaded-jvm-args/build/output.txt")));
66+
try {
67+
List<String> lines = new ArrayList<String>();
68+
69+
String line;
70+
71+
while ((line = reader.readLine()) != null) {
72+
lines.add(line);
73+
}
74+
return lines;
75+
}
76+
finally {
77+
reader.close();
78+
}
79+
}
80+
81+
private void assertOutputContains(String requiredOutput, List<String> actualOutput) {
82+
for (String line : actualOutput) {
83+
if (line.equals(requiredOutput)) {
84+
return;
85+
}
86+
}
87+
fail("Required output '" + requiredOutput + "' not found in " + actualOutput);
88+
}
89+
90+
private void assertOutputMatches(String requiredPattern, List<String> actualOutput) {
91+
for (String line : actualOutput) {
92+
if (line.matches(requiredPattern)) {
93+
return;
94+
}
95+
}
96+
fail("Required pattern '" + requiredPattern + "' not matched in " + actualOutput);
97+
}
98+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
buildscript {
2+
repositories {
3+
mavenLocal()
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}")
8+
classpath("org.springframework:springloaded:${project.springLoadedVersion}")
9+
}
10+
}
11+
12+
apply plugin: 'java'
13+
apply plugin: 'spring-boot'
14+
15+
repositories {
16+
mavenLocal()
17+
mavenCentral()
18+
}
19+
20+
dependencies {
21+
compile("org.springframework.boot:spring-boot-starter")
22+
}
23+
24+
applicationDefaultJvmArgs = [
25+
"-DSOME_ARG=someValue"
26+
]
27+
28+
jar {
29+
baseName = 'spring-loaded-jvm-args'
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test;
2+
3+
import java.io.File;
4+
import java.io.FileWriter;
5+
import java.io.PrintWriter;
6+
import java.lang.management.ManagementFactory;
7+
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.ComponentScan;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.util.Assert;
15+
16+
public class Application {
17+
18+
public static void main(String[] args) throws Exception {
19+
PrintWriter writer = new PrintWriter(new FileWriter(new File("build/output.txt")));
20+
for (String argument: ManagementFactory.getRuntimeMXBean().getInputArguments()) {
21+
writer.println(argument);
22+
}
23+
writer.close();
24+
}
25+
}

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/agent/AgentTasksEnhancer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ private void addAgent(Project project, JavaExec exec) {
108108
if (this.noverify != null && this.noverify) {
109109
exec.jvmArgs("-noverify");
110110
}
111+
Iterable<?> defaultJvmArgs = exec.getConventionMapping().getConventionValue(
112+
null, "jvmArgs", false);
113+
if (defaultJvmArgs != null) {
114+
exec.jvmArgs(defaultJvmArgs);
115+
}
111116
}
112117
}
113118

0 commit comments

Comments
 (0)