我正在尝试从 java main 运行 Maven 命令,但它没有按我的要求工作。
当我运行下面的代码时,它会在该主类所在的同一现有项目上运行 maven 命令,但我想从该类到任何其他项目文件夹运行此 maven 命令。
请帮忙!
提前致谢!
package com.codecoverage.runner;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MavenCoberturaRunner {
public static void main(String[] args) throws IOException, InterruptedException {
Process p = null;
try {
p = Runtime.getRuntime().exec("C:/apache-maven-2.0.9/apache-maven-2.0.9/bin/mvn.bat clean cobertura:cobertura -Dcobertura.report.format=xml");
} catch (IOException e) {
System.err.println("Error on exec() method");
e.printStackTrace();
}
copy(p.getInputStream(), System.out);
p.waitFor();
}
static void copy(InputStream in, OutputStream out) throws IOException {
while (true) {
int c = in.read();
if (c == -1)
break;
out.write((char) c);
}
}
}
最佳答案
您应该使用Runtime.exec(String command, String[] envp, File dir)
方法,
它在具有指定环境和工作目录的单独进程中执行指定的字符串命令。
您想要的是设置工作目录,使其指向您需要运行 Maven 的位置。
关于java - 使用 Runtime 从 Java 运行 Maven 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31608040/