gradle传递system property

执行:

gradle test -DmyParameter=123 --info

在gradle脚本中可以读到myParameter的值,但是在java程序System.getProperty("myParameter")是读不到的(-P传参也一样),因为测试用例在新的jvm执行,传入的参数不会传播。这时需要在gradle脚本中将读到的值重新设到系统属性里面,才可以被java程序读到


参考1:http://stackoverflow.com/questions/21406265/how-to-give-system-property-to-my-test-via-gradle-and-d

The -P flag is for gradle properties, and the -D flag is for JVM properties. Because the test may be forked in a new JVM, the -D argument passed to gradle will not be propagated to the test - it sounds like that is the behavior you are seeing.

You can use the systemProperty in your test block as you have done but base it on the incoming gradle property by passing it with it -P:

test {
    systemProperty "cassandra.ip", project.getProperty("cassandra.ip")
}

or alternatively, if you are passing it in via -D

test {
    systemProperty "cassandra.ip", System.getProperty("cassandra.ip")
}


参考2:http://stackoverflow.com/questions/23689054/problems-passing-system-properties-and-parameters-when-running-java-class-via-gr

JavaExec Approach

Command Line:

gradle execute -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"

build.gradle:

task execute (type:JavaExec) {

    main = "com.myCompany.MyMain"
    classpath = sourceSets.main.runtimeClasspath 

    /* Can pass all the properties: */
    systemProperties System.getProperties()

    /* Or just each by name: */
    systemProperty "myvariable", System.getProperty("myvariable")

    /* Need to split the space-delimited value in the exec.args */
    args System.getProperty("exec.args").split()    
}

Application Plugin Approach

Command Line:

gradle run -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"

build.gradle:

apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"
run {    
    /* Can pass all the properties: */
    systemProperties System.getProperties()

    /* Or just each by name: */
    systemProperty "myvariable", System.getProperty("myvariable")

    /* Need to split the space-delimited value in the exec.args */
    args System.getProperty("exec.args").split()    
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Gradle 传递依赖不生效可能有多种原因,以下是一些可能的解决方法: 1. 检查依赖声明是否正确:在模块的 build.gradle 文件中,确保所有依赖项都正确声明。尤其是检查版本号是否正确,因为错误的版本号可能会导致依赖传递失败。 2. 清除 Gradle 缓存:有时 Gradle 缓存可能会导致依赖传递失败。您可以尝试清除 Gradle 缓存并重新构建项目。 3. 检查依赖项的传递性:如果您的依赖项有多个层级,那么您可能需要检查每个依赖项的传递性。确保每个依赖项都正确地传递给您的模块。 4. 禁用 Gradle 的自动导入:有时 Gradle 的自动导入功能可能会导致依赖传递失败。您可以尝试禁用自动导入并手动声明所有依赖项。例如,在您的 settings.gradle 文件中添加以下行: ```groovy gradle.settingsEvaluated { // Disable automatic module import rootProject.children.each { it.buildFileName = "build-no-imports.gradle" } } ``` 并在每个模块的 build-no-imports.gradle 文件中手动声明所有依赖项。 5. 检查依赖项的冲突:如果您的依赖项有多个版本存在冲突,那么您需要解决这些冲突。您可以使用 Gradle 提供的 `dependencyInsight` 命令来检查依赖项的冲突。例如,在命令行中运行以下命令: ``` gradle dependencyInsight --dependency my-dependency ``` 这将显示所有依赖项的版本及其来源,以帮助您解决依赖项冲突问题。 希望这些解决方法可以帮助您解决 Gradle 传递依赖不生效的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值