bootRun에서 JVM 옵션을 전달하는 방법
원격 호스트와 통신하는 간단한 Spring 웹 애플리케이션을 개발 중이며 회사 프록시 뒤에서 로컬로 테스트하고 싶습니다. "Spring Boot"gradle 플러그인 을 사용하는데 JVM에 대한 프록시 설정을 어떻게 지정할 수 있습니까?
몇 가지 방법을 시도했습니다.
gradle -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080 bootRunexport JAVA_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"export GRADLE_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"
그러나 그들 중 어느 것도 작동하지 않는 것 같습니다. "NoRouteToHostException"은 "네트워크"코드에서 발생합니다. 또한 JVM 시작 인수를 디버그하기 위해 몇 가지 추가 코드를 추가했습니다.
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
for (String arg: arguments) System.out.println(arg);
"-Dfile.encoding = UTF-8"인수 만 인쇄되었습니다.
코드에서 시스템 속성을 설정하면 :
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
모든 것이 잘 작동합니다!
원래 답변 (Gradle 1.12 및 Spring Boot 1.0.x 사용) :
bootRun봄 부팅 Gradle을 플러그인의 작업은 Gradle을 JavaExec 작업을 확장합니다. 참조 이 .
즉, 다음을 추가하여 프록시를 사용하도록 플러그인을 구성 할 수 있습니다.
bootRun {
jvmArgs = "-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"
}
빌드 파일에.
물론 systemProperties대신 사용할 수 있습니다.jvmArgs
명령 줄에서 jvmArgs를 조건부로 추가하려면 다음을 수행 할 수 있습니다.
bootRun {
if ( project.hasProperty('jvmArgs') ) {
jvmArgs project.jvmArgs.split('\\s+')
}
}
gradle bootRun -PjvmArgs="-Dwhatever1=value1 -Dwhatever2=value2"
업데이트 된 답변 :
Spring Boot 1.2.6.RELEASE 및 Gradle 2.7을 사용하여 위의 솔루션을 시도한 후 일부 주석에서 언급 한대로 작동하지 않는 것으로 나타났습니다. 그러나 작업 상태를 복구하기 위해 몇 가지 사소한 조정을 할 수 있습니다.
새 코드는 다음과 같습니다.
bootRun {
jvmArgs = ["-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"]
}
하드 코딩 된 인수의 경우
bootRun {
if ( project.hasProperty('jvmArgs') ) {
jvmArgs = (project.jvmArgs.split("\\s+") as List)
}
}
명령 줄에서 제공된 인수
bootRun {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
를 통해 시작된 앱에 모든 JVM 옵션을 전달해야합니다 bootRun.
gradle 빌드 스크립트에서 실행 작업에 대한 systemProperties를 정의하십시오.
//to provide the properties while running the application using spring-boot's run task
run {
systemProperties['property name'] = 'value'
}
그리고 gradle run이 값을 받아 들여야한다.
Or define a project level property as mentioned in http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run
@marvin, thanks for your post it was very helpful.
Sharing how I used it:
test {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
I have JUnit tests that I wanted to skip unless a property was used to include such tests. Using JUnit Assume for including the tests conditionally:
//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)
Doing this with gradle required that the system property provided at the time of running gradle build, shown here,
gradle build -Ddeep.test.run=true
was indeed passed through to the tests.
Hope this helps others trying out this approach for running tests conditionally.
It seems to work:
bootRun {
systemProperties "property1": "value1", "property2": "value2"
}
bootRun {
args = ['myProgramArgument1', 'myProgramArgument2']
}
Using jvmArgs may cause JVM start issues. Using args allows you to pass your custom program arguments
I got into a similar problem, bootRun needed some parameters but I wouldn't feel like modifying bootRun as I want to keep some flexibility and stick to standard bootRun behaviour. My suggestion is to add some custom tasks (let's say bootRunDev, bootRunProxy) that extends bootRun, as described in the following code snippet
task bootRunPxy(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'http.proxyHost', 'xxxxx'
systemProperty 'http.proxyPort', 'yyyyy'
}
}
I don't have an environment to exercise the script but I used this approach to pass profile to spring using the property spring.profiles.active. Credits should go to Karol Kaliński
참고URL : https://stackoverflow.com/questions/25079244/how-to-pass-jvm-options-from-bootrun
'developer tip' 카테고리의 다른 글
| 인증 : JWT 사용 vs 세션 (0) | 2020.09.16 |
|---|---|
| Kotlin 배열을 Java varargs로 변환 (0) | 2020.09.16 |
| AngularJS 지시문 요소 메서드 바인딩-TypeError : 'in'연산자를 사용하여 1에서 'functionName'을 검색 할 수 없습니다. (0) | 2020.09.16 |
| 문자열 배열 초기화 옵션 (0) | 2020.09.16 |
| boolean (물음표 없음)을 반환하는 Java 메서드에 대한 명명 규칙 (0) | 2020.09.16 |