Search code examples
javaeclipsemavensvnmaven-embedder

Running maven goals from Java without a Maven executable installed on the machine


I am trying to build a project to checkout code from SCM and build it using maven. Is there a way we can achieve running maven goals from java without a maven home being provided(no maven installation). For SVN, I am using svnkit to achieve the same. For maven, I tried using the below 2 options.

http://maven.apache.org/ref/3.0-beta-3/maven-embedder/ Can anyone give a good example of using org.apache.maven.cli.MavenCli programattically?

For both the above options, I need to have a maven installation. Any suggestions?


Solution

  • No, just include the jar in your project and the following, to compile code:

    MavenCli cli = new MavenCli();
    int result = cli.doMain(new String[]{"compile"},
            "workspace/MiscMaven",
            System.out, System.out);
    System.out.println("result: " + result);
    

    Per a comment, here's how you add the dependency to your project. This does not install maven on your system, merely includes the JARs in your project build:

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-embedder</artifactId>
        <version>3.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-connector-basic</artifactId>
        <version>1.0.2.v20150114</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-transport-wagon</artifactId>
        <version>1.0.2.v20150114</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http</artifactId>
        <version>2.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-provider-api</artifactId>
        <version>2.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http-lightweight</artifactId>
        <version>2.9</version>
    </dependency>