java - 如何使用GroovyShell传递JVM参数和脚本参数?

标签 java spring-boot gradle groovy

所以我有一个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/

相关文章:

java - Gradle console() 为空,守护进程被禁用

java - 在以下上下文中如何确保线程安全?

使用 printf 舍入到小数点后两位时出现 java.util.IllegalFormatPrecisionException 错误

java - 前向 header 可变 Spring Boot

groovy - Groovy 中的 Spring Boot 外部配置

java - 在 Eclipse 中导入 Gradle 项目时对等体未通过身份验证

java - 为什么我绘制的图像在 JPanel 上闪烁?

java - 联合。有没有办法让输出格式化为 rspec 输出?

java - 从 Asciidoctor 创建 PDF 文件时出现异常

android - 如何增加 Gradle 守护程序的最大堆大小?