如何从Java程序中运行mvn命令?

3

我正在构建一个用于自动化服务器端过程的Java程序。通常情况下我会进入“桌面/GIT/”目录并使用以下maven命令:“mvn integration-test -DskipTests -P interactive -e”。

我正在构建一个java程序,并尝试运行该命令行,但迄今为止我没有成功。

目前的代码如下:

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();
}
5个回答

3
你应该查看maven embedder;这正是在嵌入maven时应该使用的工具。

好的,显然Maven嵌入器不再支持。虽然可能仍然可以让它工作,但我更愿意为您创建一个应该能够工作的小样本。当然,替换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);
    }
}
}

谢谢您的快速回答,我正在尝试理解那个例子,但是我无法确定在哪里放置代码行“mvn integration-test -DskipTests -P interactive -e”。有什么想法吗? - phedon rousou

1
我成功地运行了mvn,使用以下代码: (我使用此命令:Runtime.getRuntime().exec(cmd);)
    import java.io.*;
    import java.util.ArrayList;


    public class RunMvnFromJava     {
            static public String[] runCommand(String cmd)throws IOException
{

                // The actual procedure for process execution:
                //runCommand(String cmd);
                // Create a list for storing output.
                ArrayList list = new ArrayList();
                // Execute a command and get its process handle
                Process proc = Runtime.getRuntime().exec(cmd);
                // Get the handle for the processes InputStream
                InputStream istr = proc.getInputStream();
                // Create a BufferedReader and specify it reads
                // from an input stream.

                BufferedReader br = new BufferedReader(new InputStreamReader(istr));
                String str; // Temporary String variable
                // Read to Temp Variable, Check for null then
                // add to (ArrayList)list
                while ((str = br.readLine()) != null) 
                    list.add(str);
                    // Wait for process to terminate and catch any Exceptions.
                        try { 
                            proc.waitFor(); 
                            }
                        catch (InterruptedException e) {
                            System.err.println("Process was interrupted"); 
                            }
                        // Note: proc.exitValue() returns the exit value.
                        // (Use if required)
                        br.close(); // Done.
                        // Convert the list to a string and return
                        return (String[])list.toArray(new String[0]);
}
// Actual execution starts here
            public static void main(String args[]) throws IOException
            {
                try
                {
                    // Run and get the output.
                    String outlist[] = runCommand("mvn integration-test -DskipTests -P interactive -e");
                    // Print the output to screen character by character.
                    // Safe and not very inefficient.
                    for (int i = 0; i < outlist.length; i++)
                        System.out.println(outlist[i]);
                }   
                catch (IOException e) { 
                    System.err.println(e); 
                }
            }
}

1
对于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"**

1

如果您想使用其他配置选项运行它,请尝试 Jenkins 作为持续集成工具。它是免费的,可以与Tomcat一起使用。


绝对是浪费时间去编写自定义启动器! - Michael-O

-1

试一下这个:

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();``

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接