2012-04-25 68 views
3

我正在構建一個Java程序,用於在我的服務器端自動執行一個過程。通常,我cd到Desktop/GIT /並使用這個maven命令「mvn integration-test -DskipTests -P interactive -e」。如何從java程序運行mvn命令?

我正在構建一個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(); 
} 

回答

1

我管理使用以下代碼來運行MVN: (I使用這個命令:調用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); 
       } 
      } 
} 
3

你應該看看maven embedder;這正是您在嵌入maven時應該使用的工具。


好的,顯然maven embedder是not supported以上。可能你仍然可以讓它工作,但我寧願爲你創建一個小樣本,它應該工作。如果你想與其他配置選項運行它,嘗試Jenkins作爲持續集成工具

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); 
    } 
} 
} 
+0

感謝您的快速回答,我想了解那裏的例子,但我無法確定在哪裏放置代碼行「mvn integration-test -DskipTests -P interactive -e」。有什麼想法嗎? – 2012-04-25 11:58:54

1

:當然,Maven的替代路徑。它是免費的,可以和Tomcat一起使用。

+0

當然,寫一個自定義啓動器是浪費時間! – 2012-04-25 19:28:34

0

對於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"**