Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Maven goals from Java

Tags:

Is it possible to call Maven goals from Java, for instance, can I do the equivalent of:

mvn clean package

from a Java class?

thanks, Nick

like image 339
eldoctoro Avatar asked Sep 28 '09 15:09

eldoctoro


People also ask

How do I run a Maven goal in eclipse?

How to Run Web Application. Create a run configuration by click "Run → Run Configurations", select Maven Build and click "New launch configuration", type Name "OpenMRS", select working directory to be the root of webapp project, type goal "jetty:run" and save. Now you select "OpenMRS" and run or debug it.

What are the Maven goals?

A Maven plugin is a group of goals; however, these goals aren't necessarily all bound to the same phase. As we can see, the Failsafe plugin has two main goals configured here: integration-test: run integration tests. verify: verify all integration tests passed.


2 Answers

absolutely, you need to use the Maven Embedder API.

Updated link is http://maven.apache.org/ref/3-LATEST/maven-embedder/index.html

like image 175
dfa Avatar answered Sep 23 '22 03:09

dfa


This is easy :)

Java code

MavenCli cli = new MavenCli();
cli.doMain(new String[]{"clean", "package"}, "project_dir", System.out, System.out);

Project configuration:

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-embedder</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-connector-wagon</artifactId>
        <version>0.9.0.M2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http-lightweight</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>

Fully working example: https://github.com/mariuszs/maven-cli-example

like image 38
MariuszS Avatar answered Sep 24 '22 03:09

MariuszS