phedon rousou
phedon rousou

Reputation: 1780

How to run a mvn command from a java program?

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".

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

Upvotes: 3

Views: 16152

Answers (5)

BaranwalG
BaranwalG

Reputation: 1

try this:

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

Upvotes: -1

Elamparuthi
Elamparuthi

Reputation: 11

For windows, Try this

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");

For linux, **"cmd.exe /c"** is not needed.

Upvotes: 1

phedon rousou
phedon rousou

Reputation: 1780

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

Upvotes: 1

abalogh
abalogh

Reputation: 8281

You should check out maven embedder; which is exactly the tool which you should use in case of embedding maven.


OK apparently maven embedder is not supported any more. Probably you can still get it working though, but I rather created a small sample for you which should work. Of course, replace the path for 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);
    }
}
}

Upvotes: 3

user1352498
user1352498

Reputation:

If you want to run it with other configuration options, try Jenkins as the continous integration tool. It is free and can be used with Tomcat.

Upvotes: 1

Related Questions