Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument from gradle exec task to commandline

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.

like image 330
Nidhi jain Avatar asked Dec 21 '17 16:12

Nidhi jain


People also ask

How do you pass arguments in Gradle task?

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.

How do I run a Gradle task in CMD?

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.

How do you pass properties in Gradle?

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.


1 Answers

pass the arg via the commandLine

Windows

commandLine 'cmd', '/c', 'run.bat' ,arg1

Linux / mac

commandLine './run.sh' , arg1
like image 153
Daniel Taub Avatar answered Nov 15 '22 11:11

Daniel Taub