0

I am trying to run maven command from java main but it is not working for me as desired.

When i run the below code it runs the maven command on the same existing project in which this main class residing, but i want to run this maven command from this class to any another project folder.

Please help!

Thanks in advance!

package com.codecoverage.runner;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class MavenCoberturaRunner {

    public static void main(String[] args) throws IOException, InterruptedException {

         Process p = null;

            try {
                p = Runtime.getRuntime().exec("C:/apache-maven-2.0.9/apache-maven-2.0.9/bin/mvn.bat clean cobertura:cobertura -Dcobertura.report.format=xml");
            } 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);
            }
        }
        }

2 Answers 2

2

You should use Runtime.exec(String command, String[] envp, File dir) method, which executes the specified string command in a separate process with the specified environment and working directory.

What you want is to set working directory so it points to the place you need to run Maven at.

Sign up to request clarification or add additional context in comments.

Comments

1

use -f in command line

C:/apache-maven-2.0.9/apache-maven-2.0.9/bin/mvn.bat -f path/to/your/pom.xml clean cobertura:cobertura -Dcobertura.report.format=xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.