0

I have a task, something like:

task doSomething(type: Exec){

   commandLine "cmd", "/c", "flavour"

}

How can I made a specific variable and pass it into this method ? I tried something like this:

task doSomething(type: Exec, myVariable: String){
    
       commandLine "cmd", "/c", "$myVariable"
    
    }

but it doesn't work

and i would like to assing this value in productFlavours like:

productFLavors {

flavor1{
myVariable = "flavor1"
}

flavour2{
myVariable = "flavor2"
}

}

I tried everything and I'm still getting errors :/

discCard
  • 421
  • 1
  • 5
  • 16

1 Answers1

1

Looks like you're confusing bash/groovy string interpolation with just using the variable...

Try:

task doSomething(type: Exec) {
       commandLine "cmd", "/c", myVariable
}
./gradlew doSomething -P myVariable="something"
Rob Evans
  • 2,822
  • 1
  • 9
  • 15
  • "Could not create task 'doSomething': Unknown argument(s) in task definition: [myVariable] – discCard Oct 22 '20 at 11:08
  • Updated comment with working solution. Also see https://stackoverflow.com/questions/11696521/how-to-pass-arguments-from-command-line-to-gradle – Rob Evans Oct 22 '20 at 11:17
  • nice, but do you know also how to pass argument if you want to run task it in this way: preBuild.dependsOn doSomething - from the gradle, not command line – discCard Oct 22 '20 at 11:48
  • https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:adding_dependencies_to_tasks its all in the documentation :) You'll find a google search for "dependsOn gradle" likely returns 1000s of examples you can readily adapt – Rob Evans Oct 22 '20 at 11:50