如何从java程序运行mvn命令? [英] How to run a mvn command from a java program?

查看:130
本文介绍了如何从java程序运行mvn命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Java程序,用于在服务器端自动执行程序。通常我cd到Desktop / GIT /并使用这个maven命令mvn integration-test -DskipTests -P interactive -e。

I am building a Java program for automating a procedure in my server side. Normally I cd to Desktop/GIT/ and use this maven command "mvn integration-test -DskipTests -P interactive -e".

我正在构建一个java程序而我我试图运行该命令行,但到目前为止我没有成功。

I am building a java program and I am trying to run that command line but so far I wasn't successful.

到目前为止,这里是代码:

So far, here is the code:

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


推荐答案

我设法运行mvn使用以下代码:
(我使用此命令:Runtime.getRuntime()。exec(cmd);)

I managed to run the mvn using the following code: (I use this command: 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程序运行mvn命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆