所以我有一个Groovy脚本:
// TestScript.groovy
println args
然后在Gradle任务中test {
String profile = System.getenv("spring.profiles.active")
jvmArgs '-Dspring.profiles.active=$profile" // THIS DOES NOT WORK! :(
doLast {
new GroovyShell().run(file('package.TestScript.groovy'))
}
}
我需要做的两件事:a)传入
TestScript.groovy
程序参数,以便将其打印出args
数组b)将Spring Boot配置文件即
spring.profiles.active=dev
传递给JVM有什么建议么?
注意,我正在使用Groovy 2.4.3并引用此文档:http://docs.groovy-lang.org/2.4.3/html/api/groovy/lang/GroovyShell.html
我尝试了以下失败的尝试:
doLast {
Binding b = new Binding();
b.setVariable('spring.profiles.active', $profile)
new GroovyShell(b).run(file('package.TestScript.groovy'))
}
最佳答案
工作示例here ...
如果我们有一个脚本将参数写入文件:
// TestScript.groovy
try {
new File("out.txt").withWriter { writer ->
args.each { arg ->
writer.write("TRACER arg : ${arg}\n")
}
}
} catch (Exception ex) {
new File("error.txt").withWriter { writer ->
writer.write("TRACER caught exception ${ex.message}\n")
}
}
然后我们可以使用以下Gradle任务对其进行测试:test {
doLast {
def profile = project["spring.profiles.active"]
def script = new File("${projectDir}/TestScript.groovy")
def args = ["exampleArgVal1", "exampleArgVal2", profile]
new GroovyShell().run(script, args)
}
}
请注意,参数像这样传递给Gradle:gradle clean test -Pspring.profiles.active=TEST_PROFILE
关于java - 如何使用GroovyShell传递JVM参数和脚本参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64084870/