Avoid duplication between similar Gradle tasks?

后端 未结 4 1059
醉话见心
醉话见心 2021-02-02 10:47

It there any way to avoid duplication in configuration between two similar tasks of the same type?

For example, I\'d like to create a debugSomething task, w

4条回答
  •  粉色の甜心
    2021-02-02 11:06

    This can be solved using plain Groovy:

    task runSomething(dependsOn: jar, type: JavaExec, group: "Run") {
    }
    
    task debugSomething(dependsOn: jar, type: JavaExec, group: "Run") {
        jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=y"
    }
    
    [runSomething, debugSomething].each { task ->
        task.main = "com.some.Main"
        task.classpath = sourceSets.main.runtimeClasspath
        task.jvmArgs "-Xmx1024m", "-XX:MaxPermSize=128m"
    }
    

    Even though debugSomething.jvmArgs is invoked twice, all three arguments are supplied to the JVM.

    Single arguments can be set using Groovy's Spread operator:

    [runSomething, debugSomething]*.main = "com.some.Main"
    

提交回复
热议问题