如何从java程序运行mvn命令?
我正在构建一个Java程序,用于在服务器端自动化一个过程。通常,我会将cd刻录到Desktop/GIT/并使用这个maven命令“mvn integration test-DskipTests-P interactive-e” 我正在构建一个java程序,我正在尝试运行该命令行,但到目前为止我没有成功 到目前为止,代码如下:如何从java程序运行mvn命令?,java,maven,Java,Maven,我正在构建一个Java程序,用于在服务器端自动化一个过程。通常,我会将cd刻录到Desktop/GIT/并使用这个maven命令“mvn integration test-DskipTests-P interactive-e” 我正在构建一个java程序,我正在尝试运行该命令行,但到目前为止我没有成功 到目前为止,代码如下: public static void main(String[] args) throws FileNotFoundException { // TODO Auto-gen
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Process theProcess = null;
try
{
theProcess = Runtime.getRuntime().exec("mvn integration-test -DskipTests -P interactive -e");
}
catch(IOException e)
{
System.err.println("Error on exec() method");
e.printStackTrace();
}
// read from the called program's standard output stream
try
{
inStream = new BufferedReader(new InputStreamReader( theProcess.getInputStream()));
System.out.println(inStream.readLine());
}
catch(IOException e)
{
System.err.println("Error on inStream.readLine()");
e.printStackTrace();
}
break;
}
}
in.close();
}
你应该退房;这正是嵌入maven时应该使用的工具
好的,显然maven embedder不再是了。也许你仍然可以让它工作,但是我为你创建了一个小样本,应该可以工作。当然,替换Maven的路径:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Q {
public static void main(String[] args) throws IOException, InterruptedException {
Process p = null;
try {
p = Runtime.getRuntime().exec("C:/Applications/apache-maven-3.0.3/bin/mvn.bat integration-test -DskipTests -P interactive -e");
} 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);
}
}
}
如果您想使用其他配置选项运行它,请尝试作为持续集成工具。它是免费的,可以与Tomcat一起使用。我使用以下代码运行了mvn: (我使用这个命令:Runtime.getRuntime().exec(cmd);)
import java.io.*;
导入java.util.ArrayList;
公共类RunMvnFromJava{
静态公共字符串[]runCommand(字符串cmd)引发IOException
{
//流程执行的实际程序:
//runCommand(字符串cmd);
//创建用于存储输出的列表。
ArrayList=新建ArrayList();
//执行命令并获取其进程句柄
processproc=Runtime.getRuntime().exec(cmd);
//获取进程InputStream的句柄
InputStream istr=proc.getInputStream();
//创建BufferedReader并指定其读取
//从输入流。
BufferedReader br=新的BufferedReader(新的InputStreamReader(istr));
String str;//临时字符串变量
//读取临时变量,然后检查null
//添加到(ArrayList)列表
而((str=br.readLine())!=null)
添加(str);
//等待进程终止并捕获任何异常。
试试{
进程waitFor();
}
捕捉(中断异常e){
System.err.println(“进程被中断”);
}
//注意:proc.exitValue()返回退出值。
//(必要时使用)
br.close();//完成。
//将列表转换为字符串并返回
return(String[])list.toArray(新字符串[0]);
}
//实际执行从这里开始
公共静态void main(字符串args[])引发IOException
{
尝试
{
//运行并获取输出。
字符串outlist[]=runCommand(“mvn集成测试-DskipTests-P交互-e”);
//逐字符将输出打印到屏幕。
//安全且效率不高。
for(int i=0;i
对于windows,请尝试以下操作
Process p=Runtime.getRuntime().exec("cmd.exe /c mvn install:install-file -Dfile=C:\\Users\\Desktop\\Desktop\\sqljdbc4-4.0.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0");
对于linux,不需要使用**“cmd.exe/c”**
试试这个:
List<String> commands=new ArrayList<>();
commands.add("mvn");
commands.add("-f");
commands.add();
commnads.add("integration-test");
commands.add("-DskipTests");
commands.add("-P");
commands.add("interactive");
commands.add("-e");
ProcessBuilder pb=new ProcessBuilder(commands);
pb.start();``
List命令=new ArrayList();
命令。添加(“mvn”);
命令。添加(“-f”);
commands.add();
通信添加(“集成测试”);
添加(“-DskipTests”);
命令。添加(“-P”);
命令。添加(“交互式”);
命令。添加(“-e”);
ProcessBuilder pb=新的ProcessBuilder(命令);
pb.start()``
感谢您的快速回答,我试图理解其中的示例,但无法确定代码行“mvn集成测试-DskipTests-P interactive-e”的位置。有什么想法吗?当然,编写自定义启动器是浪费时间!
List<String> commands=new ArrayList<>();
commands.add("mvn");
commands.add("-f");
commands.add();
commnads.add("integration-test");
commands.add("-DskipTests");
commands.add("-P");
commands.add("interactive");
commands.add("-e");
ProcessBuilder pb=new ProcessBuilder(commands);
pb.start();``