I'm trying to hook up a bat and sh file (depending on the OS being run on) with the gradle exec task. But I'm unable to figure out how to send an argument to bat/sh from the exec task.
task testing(type:Exec) {
workingDir '.'
if (System.properties['os.name'].toLowerCase().contains('windows')) {
if ( project.hasProperty("arg1") ) {
println arg1
args arg1
commandLine 'cmd', '/c', 'run.bat'
}else{
commandLine 'cmd', '/c', 'run.bat'
}
}else {
if ( project.hasProperty("arg1") ) {
args arg1
commandLine './run.sh'
}else{
commandLine './run.sh'
}
}
}
If I run this task as : gradle testing -Parg1=test
, in println arg1
, it prints test
But how do I pass on this test as argument to the bat/sh file.
Here, we don't use properties to pass arguments. Instead, we pass the –args flag and the corresponding inputs there. This is a nice wrapper provided by the application plugin. However, this is only available from Gradle 4.9 onward.
To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … e.g. gradlew clean allTests.
Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.
pass the arg via the commandLine
Windows
commandLine 'cmd', '/c', 'run.bat' ,arg1
Linux / mac
commandLine './run.sh' , arg1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With