Yevhenii Nadtochii
Yevhenii Nadtochii

Reputation: 704

How to get the exact copy of original Run task in Gradle?

Does anybody know what the simplest way to register several run tasks that represent exact copy of original one with changed app’s arguments ? Or may be how to supply run task with an optional argument that would represent app’s arguments. Basically, I want my build to contain some pre-defined options how to run an application and don’t want to declare new JavaExec that requires its manual configuring while I have already had ready-to-go run task supplied by default.

gradle run --args='--mode=middle'      ---> gradle run || gradle runDefault || gradle run default
gradle run --args='--mode=greed'       ---> gradle runGreed || gradle run greed
gradle run --args='--mode=lavish'      ---> gradle runLavish || gradle run lavish

As for now I came up only with the option that suggests implementing my own JavaExec_Custom task class with supplied arguments property. And it seems to be too complex and boilerplating as for my goal.

Upvotes: 0

Views: 124

Answers (2)

Lukas Körfer
Lukas Körfer

Reputation: 14523

You can create tasks that modify the configuration of the actual run task in a doFirst or doLast closure:

// Groovy-DSL

task runGreed {
    doFirst {
        run.args '--mode=greed'
    }
    finalizedBy run
}


// Kotlin-DSL

register("runGreed") {
    doFirst {
        run.get().args = listOf("--mode=greed")
    }
    finalizedBy(run)
}

Upvotes: 1

lance-java
lance-java

Reputation: 28016

You can use a property for the args

task run(type: JavaExec) {
   args property('run.args').split(' ')
   ...
} 

Usage

gradle run -Prun.args='foo bar baz'

Upvotes: 0

Related Questions