【问题标题】:Gradle pass dynamic value as system property to methodGradle 将动态值作为系统属性传递给方法
【发布时间】:2019-01-31 10:18:37
【问题描述】:

我有一个 gradle 项目,该项目有一个计算变量,我需要将其传递给应用程序。当我尝试这样做时,传递的是一个空值,而不是我想要的设定值。

我有下面的示例文件,它演示了要运行的问题,只需执行 gradle foo 我希望两行输出都是 4。

def String sum

task add {
    doLast {
        new ByteArrayOutputStream().withStream { os ->
            def result = exec {
                executable = 'sh'
                args = ['-c', 'echo $((2 + 2))']
                standardOutput = os
            }
            sum = os.toString()
            afterEvaluate {
                tasks.foo {
                    systemProperty "foo.bar", "${sum}"
                }
            }
        }
    }
}

task foo {
    doLast{
        println System.properties['foo.bar']
        println "${sum}"
    }
}

tasks.foo.dependsOn( add )

【问题讨论】:

    标签: gradle android-gradle-plugin build.gradle gradlew gradle-eclipse


    【解决方案1】:

    鉴于此(注意对System.properties['foo.bar'] 的简单分配):

    def String sum
    
    task add {
        doLast {
            new ByteArrayOutputStream().withStream { os ->
                def result = exec {
                    executable = 'sh'
                    args = ['-c', 'echo $((2 + 2))']
                    standardOutput = os
                }
                sum = os.toString()
                System.properties['foo.bar'] = sum
            }
        }
    }
    
    task foo {
        doLast{
            println System.properties['foo.bar']
            println "${sum}"
        }
    }
    
    tasks.foo.dependsOn( add )
    

    那么这是输出:

    $ gradle foo 
    :add
    :foo
    4
    
    4
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2017-11-23
      • 2017-02-10
      • 2019-12-08
      • 2017-06-21
      相关资源
      最近更新 更多