用Gradle执行命令? [英] Executing commands with Gradle?

查看:670
本文介绍了用Gradle执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 任务stopServer(dependsOn:war,输入: Exec)<< {
commandLine'pkill -9 tomcat'
}

当我运行它时我得到以下错误:

  *出错:
任务':stopServer'的执行失败。
> execCommand == null!

当我的任务是这样的:

 任务stopServer(dependsOn:war)<< {
exec {
commandLine'pkill -9 tomcat'
}
}

我得到这个错误:

  *出错了:
任务执行失败' :stopServer。
>启动过程中出现问题'command'pkill -9 tomcat''

你能告诉我我在哪里每种方法都会出错?

如果上述两种都不是正确的执行方式,请用一个例子详细说明实现方式。

 任务stopServer(dependsOn: war,输入:Exec){
commandLinepkill,-9,java
}

主要区别非常微妙 - 我只删除了两个字符。 << 已从任务定义中消失。另一个区别是 commandLine 期望可执行文件可以独立于它的参数传递。



I由于gradle:<< 。 htmlrel =noreferrer>构建生命周期。有配置和执行阶段(这不是全部,但它足以解释这一点)。



<< 就像是说 doLast - 它将传递的闭包添加到此任务的动作结束(执行阶段)。所以这意味着在这里,它会尝试执行像普通的命令(毕竟它是一个 Exec 对象),并且只有然后是,一次它会被执行,它会调用你的块 - 块设置 commandLine 。所以当它执行时, execCommand 确实为null,并且会一直运行到您的块。这是你问题的核心。



没有< (也称为左移) ,相同的块在配置阶段运行。所以exec命令在运行之前就被设置好了,并且它可以工作。


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

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

When I run it I get the following error:

* What went wrong:
Execution failed for task ':stopServer'.
> execCommand == null!

And when my task is like this:

task stopServer(dependsOn: war) << {
    exec {
        commandLine 'pkill -9 tomcat'
    }
}

I get this error:

* What went wrong:
Execution failed for task ':stopServer'.
> A problem occurred starting process 'command 'pkill -9 tomcat''

Can you tell me where I am going wrong in each of these approaches?

If neither of above are right way of executing then please specify the way of doing it probably with an example.

解决方案

I believe you're looking for this:

task stopServer(dependsOn: war, type: Exec) {
     commandLine "pkill", " -9", "java"
}

The main difference is very subtle - I just deleted two characters. The << is gone from the task definition. The other difference is that the commandLine expects the executable to be passed in separately from the arguments to it.

I removed the << because of an important idea in gradle: the build lifecycle. There's configuration and execution phases (that's not all, but it's enough to explain this).

The << is like saying doLast - it adds the closure you pass to the end of the actions (the execution phase) for this task. So that means here, it's going to try and execute the command like normal (it's an Exec object, after all), and only then, once it's executed, will it call your block - the block setting commandLine. So when it's executing, execCommand really is null, and would be until your block was run. This is the heart of your problem.

Without the << (also known as left-shift), that same block runs during the configuration phase. So the exec command gets set before it runs, and it works.

这篇关于用Gradle执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆