简体   繁体   English

Groovy 执行 mvn 命令

[英]Groovy executing mvn command

I'm writing groovy script and trying to execute following shell command using execute() method我正在编写 groovy 脚本并尝试使用 execute() 方法执行以下 shell 命令

"mvn clean install -f \{pom directory}\pom.xml".execute()

but got an error with following message但收到以下消息错误

"Cannot run program "mvn": error=2, No such file or directory" “无法运行程序“mvn”:错误=2,没有这样的文件或目录”

Of course, maven is already installed and the command works well on terminal....当然,maven 已经安装,并且该命令在终端上运行良好....

I also tried with "sc -c" prefix.我也尝试使用“sc -c”前缀。

"sh -c mvn clean install -f \<pom directory\pom.xml".execute()

But this does execute nothing and no error at all.但这并没有执行任何操作,也没有任何错误。 it seems like the command just ignored.似乎该命令被忽略了。

So how should I execute maven install in groovy script?那么我应该如何在 groovy 脚本中执行 maven install 呢?

I'm writing groovy script and trying to execute following shell command using execute() method我正在编写 groovy 脚本并尝试使用 execute() 方法执行以下 shell 命令

"mvn clean install -f \{pom directory}\pom.xml".execute()

but got an error with following message但收到以下消息的错误

"Cannot run program "mvn": error=2, No such file or directory" “无法运行程序“mvn”:错误=2,没有那个文件或目录”

Of course, maven is already installed and the command works well on terminal....当然,maven 已经安装好了,并且命令在终端上运行良好....

I also tried with "sc -c" prefix.我也尝试过使用“sc -c”前缀。

"sh -c mvn clean install -f \<pom directory\pom.xml".execute()

But this does execute nothing and no error at all.但这确实不执行任何操作,也根本没有错误。 it seems like the command just ignored.似乎该命令被忽略了。

So how should I execute maven install in groovy script?那么我应该如何在 groovy 脚本中执行 maven install 呢?

On windows, mvn is not an exe-file but a command (mvn.cmd).在 windows 上,mvn 不是 exe 文件,而是命令 (mvn.cmd)。 I fixed the issue the following way:我通过以下方式解决了这个问题:

def cmd = ""
if (System.properties['os.name'].toLowerCase().contains('windows')) {
    println "On Windows mvn is a CMD and not an EXE"
    cmd = "mvn.cmd wrapper:wrapper"
} else {
    println "Execute just the mvn executable on non Windows systems"
    cmd = "mvn wrapper:wrapper"
}
def process = cmd.execute(null, rootDir)
process.waitForProcessOutput((Appendable)System.out, System.err)
if (process.exitValue() != 0) {
    println "WARNING: Command '$cmd' exited with code: ${process.exitValue()}"
    println "WARNING: you can initialize the maven wrapper by yourself:}"
    println "WARNING: go to $rootDir and type in 'mvn wrapper:wrapper'}"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM