创建可执行的gradle任务

时间:2017-04-10 19:08:40

标签: gradle groovy

目前,我有一个gradle这样的任务

class MyTask  extends DefaultTask {
    @TaskAction
    public void run() {

    }
}

我可以在build.gradle中执行此类操作

task stopTomcat(type:Exec) {
  workingDir '../tomcat/bin'

  //on windows:
  commandLine 'cmd', '/c', 'stop.bat'

  //on linux
  commandLine './stop.sh'

  //store the output instead of printing to the console:
  standardOutput = new ByteArrayOutputStream()

  //extension method stopTomcat.output() can be used to obtain the output:
  ext.output = {
    return standardOutput.toString()
  }
}

我想让这个任务可执行访问commandLine,workingDir等。我该怎么做到这一点?

1 个答案:

答案 0 :(得分:0)

你可以做这种事(如果那是你的意思):

class MyTask  extends DefaultTask {
    private workingDir
    private commandLine

    void workingDir(String workingDir) {
        this.workingDir = workingDir
    }

    void commandLine(String commandLine) {
        this.commandLine = commandLine
    }

    @TaskAction
    void run() {
        println "I will run $commandLine in $workingDir"
    }
}