简体   繁体   English

将动态参数传递给Gradle Jenkins插件

[英]Pass dynamic parameters to Gradle Jenkins plugin

I am using the Gradle Jenkins plugin to create a Jenkins Job that executes a Gradle build. 我正在使用Gradle Jenkins插件创建执行Gradle构建的Jenkins Job。 I have a couple of parameters that are always required and these are passed to a Jenkins "Parameterized build". 我有几个始终需要的参数,这些参数传递给Jenkins的“参数化构建”。 These parameters are correctly passed to gradlew.bat with the following format: 这些参数将使用以下格式正确传递到gradlew.bat:

-DParam1=value1 

Now there are some additional parameters that are dynamic in nature and I'd prefer to use a single Jenkins parameter such as "AdditionalParamters" and pass these values like so: 现在,有一些其他参数是动态的,我更喜欢使用单个Jenkins参数,例如“ AdditionalParamters”,并像这样传递这些值:

 "-DoptionalParam1=value1 -DOptionalParam2=value2". 

If I use such a parameter, then the value passed to Gradle is "-DAdditionalParamters=-DoptionalParam1=value1 -DOptionalParam2=value2" and this is not what I'd intended to pass. 如果我使用这样的参数,则传递给Gradle的值是“ -DAdditionalParamters = -DOptionalParam1 = value1 -DOptionalParam2 = value2”,这不是我想要传递的。

So are there any means of passing multiple System parameters to gradle by using a single Jenkins parameter? 那么,有没有办法使用单个Jenkins参数将多个系统参数传递给gradle?

When Jenkins passes the parameters to Gradle, they are set as Systems Properties. 当詹金斯将参数传递给Gradle时,它们将被设置为“系统属性”。 The task now is to parse the "-DoptionalParam1=value1 -DOptionalParam2=value2" passed as an "Additional Parameters" and then pass these further to the Java application as JVM paramters. 现在的任务是解析作为“附加参数”传递的“ -DoptionalParam1 = value1 -DOptionalParam2 = value2”,然后将它们作为JVM参数传递给Java应用程序。 Below is the Gralde code snippet that does it. 以下是执行此操作的Gralde代码段。 To simplify the "Additional Paramter" is passed as "optionalParam1=value1 OptionalParam2=value2", ie without the -D syntax. 为了简化“附加参数”,将其传递为“ optionalParam1 = value1 OptionalParam2 = value2”,即不带-D语法。

    task runJavaApp(type: JavaExec, dependsOn: build) {

        def jvmParams = []
        project.gradle.startParameter.systemPropertiesArgs.entrySet().each{
        if (it.key!="Additional Parameters") {
            jvmParams <<  "-D${it.key}=${it.value}"
        } else {
            def tokens = (it.value).split(/\s{1,}/)
            tokens.each {
                def key, value
                if (it.split('=').length == 2)
                    (key, value )= it.split('=')
                else
                    throw new Exception("'${it}' should be of type key=value");
                jvmParams <<  "-D${key}=${value}"
                }
            }//if
        }
        jvmArgs jvmParams
        workingDir = project.executeDir
        classpath = sourceSets.main.runtimeClasspath + files(project.executeDir)
        main = 'myApplicaionMainClass'
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM