Preserve JVM args when auto-configuring a Java agent in Gradle bootRun

Fixes #1411
This commit is contained in:
Andy Wilkinson 2014-08-21 15:25:02 +01:00
parent c75782424c
commit 9717b7d840
5 changed files with 160 additions and 0 deletions

View File

@ -46,6 +46,8 @@ public class ProjectCreator {
}
GradleConnector gradleConnector = GradleConnector.newConnector();
gradleConnector.useGradleVersion("1.12");
((DefaultGradleConnector) gradleConnector).embedded(true);
return gradleConnector.forProjectDirectory(projectDirectory).connect();
}

View File

@ -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<String> output = getOutput();
assertOutputContains("-DSOME_ARG=someValue", output);
assertOutputContains("-Xverify:none", output);
assertOutputMatches(
"-javaagent:.*springloaded-" + SPRING_LOADED_VERSION + ".jar", output);
}
private List<String> getOutput() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(
"target/spring-loaded-jvm-args/build/output.txt")));
try {
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
return lines;
}
finally {
reader.close();
}
}
private void assertOutputContains(String requiredOutput, List<String> actualOutput) {
for (String line : actualOutput) {
if (line.equals(requiredOutput)) {
return;
}
}
fail("Required output '" + requiredOutput + "' not found in " + actualOutput);
}
private void assertOutputMatches(String requiredPattern, List<String> actualOutput) {
for (String line : actualOutput) {
if (line.matches(requiredPattern)) {
return;
}
}
fail("Required pattern '" + requiredPattern + "' not matched in " + actualOutput);
}
}

View File

@ -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'
}

View File

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

View File

@ -108,6 +108,11 @@ public class AgentTasksEnhancer implements Action<Project> {
if (this.noverify != null && this.noverify) {
exec.jvmArgs("-noverify");
}
Iterable<?> defaultJvmArgs = exec.getConventionMapping().getConventionValue(
null, "jvmArgs", false);
if (defaultJvmArgs != null) {
exec.jvmArgs(defaultJvmArgs);
}
}
}