使用运行时从Java运行maven命令

使用运行时从Java运行maven命令,java,maven,runtime,Java,Maven,Runtime,我正试图从Javamain运行maven命令,但它并没有按我所希望的那样工作 当我运行下面的代码时,它会在这个主类所在的同一个现有项目上运行maven命令,但是我想从这个类运行这个maven命令到任何其他项目文件夹 请帮忙 提前谢谢 package com.codecoverage.runner; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class

我正试图从Javamain运行maven命令,但它并没有按我所希望的那样工作

当我运行下面的代码时,它会在这个主类所在的同一个现有项目上运行maven命令,但是我想从这个类运行这个maven命令到任何其他项目文件夹

请帮忙

提前谢谢

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);
            }
        }
        }
在命令行中使用-f

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

在命令行中使用-f

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

在命令行中使用-f

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

在命令行中使用-f


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

您应该使用
Runtime.exec(String命令,String[]envp,File dir)
方法, 它在具有指定环境和工作目录的单独进程中执行指定的字符串命令


您需要设置工作目录,使其指向您需要运行Maven的位置。

您应该使用
Runtime.exec(String命令,String[]envp,File dir)
方法, 它在具有指定环境和工作目录的单独进程中执行指定的字符串命令


您需要设置工作目录,使其指向您需要运行Maven的位置。

您应该使用
Runtime.exec(String命令,String[]envp,File dir)
方法, 它在具有指定环境和工作目录的单独进程中执行指定的字符串命令


您需要设置工作目录,使其指向您需要运行Maven的位置。

您应该使用
Runtime.exec(String命令,String[]envp,File dir)
方法, 它在具有指定环境和工作目录的单独进程中执行指定的字符串命令

您想要的是设置工作目录,使其指向您需要运行Maven的位置