44

I want to execute Maven command from Java for development of a plugin. I tried maven-embedder but looks like it is now not supported. Is someone aware of some other tool which can be used?

5
  • If you are developing a Maven Plugin you should take a deep look into the Maven Invoker Plugin to make integration test for the plugin
    – khmarbaise
    Feb 28, 2011 at 12:59
  • @Paul Verest, my answer is not working for you?
    – MariuszS
    Jan 28, 2014 at 14:45
  • @MariuszS In Eclipse plugin embedding Maven will make it heavy, I want let user to select the same version he/she has on PC github.com/Nodeclipse/nodeclipse-1/tree/master/… . There were only 2 option named: embedder and lower level exec(). A good answer would be from those who really accomplishes and can compare or give a new option. Jan 30, 2014 at 7:24
  • @PaulVerest I have used both options in my project and prefer embedded version. Maven is heavy, and it will always make program using it heavier. Do you ask for something like Maven Light? There is no such thing at all, maven embedder configuration from my answer has minimal configuration. You can disable wagon for smaller and lighter maven version.
    – MariuszS
    Jan 30, 2014 at 19:04
  • Maven Invoker Plugin does not look useful in all instances, thus I have written a plugin using MavenCLI, with some help from stackoverflow.com/questions/22410706/… with my added comments.
    – Infernoz
    Dec 7, 2020 at 14:18

3 Answers 3

58
+50

A simple invocation API : maven-invoker.

Project documentation : http://maven.apache.org/shared/maven-invoker/

Usage : http://maven.apache.org/shared/maven-invoker/usage.html

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Arrays.asList( "clean", "install" ) );

Invoker invoker = new DefaultInvoker();
invoker.execute( request );
1
17

Use Maven Embedder

Maven embedder is still supported and easy to configure, so this is better option for you.

Java code

MavenCli cli = new MavenCli();
cli.doMain(new String[]{"clean", "install"}, "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

9
  • 1
    Can the maven embedder be downloaded as a jar if the project using it isn't using maven?
    – tbodt
    Jan 30, 2014 at 18:48
  • 1
    no, what I mean is, can you use maven embedder in a project that doesn't use maven? and how?
    – tbodt
    Jan 30, 2014 at 18:56
  • 1
    Yes, this is also possible but compliacted :) Prepare pom.xml for this project and run maven embedder on it. The compliacted part is preparing pom.xml.
    – MariuszS
    Jan 30, 2014 at 18:57
  • 1
    @MariuszS Is there any way that we can run maven plugins on a system where both java and maven is not present. Please have a look at the question [stackoverflow.com/questions/44911102/… Jul 5, 2017 at 5:39
  • 1
    See stackoverflow.com/questions/22410706/… for why more work can be required, especially inside a custom Maven plugin!
    – Infernoz
    Dec 7, 2020 at 14:20
5

Maven embedder is indeed no longer supported (only hudson still uses it). But, as in hudson, there are several other ways to run maven. You could simply run maven as an external program:

Runtime.getRuntime().exec("mvn clean install");

Or you could consider creating an ant script for maven. This script could then be called either as an external program or (if you need more control) adding ant to your classpath and calling the Antrunner.

UPDATE

Maven embedder is now supported again so that is your best option.

7
  • Maven embedder is cross platform, but your first solution is just for Unix. For Windows, see stackoverflow.com/questions/9674220/…
    – fglez
    Jun 15, 2012 at 8:36
  • This will not execute, as executed process is not shell, so there is JAVA_HONE, MAVEN_HOME etc settings. stackoverflow.com/questions/21397170/… Jan 28, 2014 at 13:55
  • I would think that mvn needing to be in the path goes without saying; no need to downvote for that. Jan 28, 2014 at 14:37
  • 1
    This will fork a separate process for Maven in it's own JVM. MariuszS' answer is the proper way to do it through Java itself. Invoking Maven through Runtime is also an option, but it would be less efficient as it will require more resources.
    – carlspring
    Jan 28, 2014 at 15:50
  • The OP asked for alternatives to maven embedder! Jan 28, 2014 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.