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

问题描述

我设法使用以下代码运行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); 
                }
            }
}

解决方法

我正在构建一个Java程序来自动化服务器端的过程。通常,我cd到Desktop / 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();
}

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”