Executing commands with Gradle?

前端 未结 2 442
后悔当初
后悔当初 2021-02-13 02:41

I am trying to execute a command with gradle with the below task:

task stopServer(dependsOn: war, type: Exec) << {
    commandLine \'pkill -9 tomcat\'
}
         


        
2条回答
  •  囚心锁ツ
    2021-02-13 03:33

    Here is another solution that works well for the same error if you have a standalone exec task that you want to run from the commandline but do not want to run in Android Studio as part of your build.

    This will always run the configuration of "myExecTask" but will only execute doMyExecTask when it is explicitly run via "gradle myExecTask"

    /**
     * Actually run exec task in doLast phase
     */
    task doMyExecTask << {
        def hasProperties = project.hasProperty('SOME_PROPERTY');
    
        if (hasProperties) {
            myExecTask.commandLine "echo", "$SOME_PROPERTY"
        } else {
            println "ERROR: Unable to run task. Missing properties."
        }
    }
    
    /**
     * Configure exec task, this always runs
     */
    task myExecTask(type: Exec) {
        dependsOn doMyExecTask
        workingDir 'path/to/executable'
    }
    

提交回复
热议问题