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?

- 14,080
- 5
- 48
- 107

- 1,151
- 2
- 15
- 28
-
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 '11 at 12:59
-
@Paul Verest, my answer is not working for you? – MariuszS Jan 28 '14 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 https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.maven . 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. – Paul Verest Jan 30 '14 at 07: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 '14 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 https://stackoverflow.com/questions/22410706/error-when-execute-mavencli-in-the-loop-maven-embedder with my added comments. – Infernoz Dec 07 '20 at 14:18
3 Answers
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 );

- 42,748
- 17
- 132
- 221

- 631
- 5
- 3
-
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 [https://stackoverflow.com/questions/44911102/how-to-execute-maven-plugins-from-gradle-without-having-maven-and-java-installed] – Kasun Siyambalapitiya Jul 05 '17 at 05:40
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

- 30,646
- 12
- 114
- 155
-
1Can the maven embedder be downloaded as a jar if the project using it isn't using maven? – tbodt Jan 30 '14 at 18:48
-
Yes, you can use this jar https://dl.dropboxusercontent.com/u/1012436/maven-jar-with-dependencies.jar from example project and invoke maven build with `java -jar maven-jar-with-dependencies.jar` from maven project directory. – MariuszS Jan 30 '14 at 18:54
-
1no, what I mean is, can you use maven embedder in a project that doesn't use maven? and how? – tbodt Jan 30 '14 at 18:56
-
1Yes, 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 '14 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 [https://stackoverflow.com/questions/44911102/how-to-execute-maven-plugins-from-gradle-without-having-maven-and-java-installed] – Kasun Siyambalapitiya Jul 05 '17 at 05:39
-
@tbodt: You can download ALL maven artifacts simply from maven central manually or by some other mechanism, e.g. gradle or ivy. In this case just go to https://mvnrepository.com/artifact/org.apache.maven/maven-embedder/3.1.1 – JFK Nov 24 '17 at 09:36
-
I got a `NoSuchMethodError` during (an internal call of) `isThreadConfigurationPresent()Z`. What solved the problem for me was changing the version of `maven-embedder` from `3.1.1.` to `3.2.1.`. – Aleksandar Jun 20 '18 at 14:43
-
-
1See https://stackoverflow.com/questions/22410706/error-when-execute-mavencli-in-the-loop-maven-embedder for why more work can be required, especially inside a custom Maven plugin! – Infernoz Dec 07 '20 at 14:20
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.

- 15,454
- 8
- 66
- 101
-
Maven embedder is cross platform, but your first solution is just for Unix. For Windows, see http://stackoverflow.com/questions/9674220/running-maven-from-java-code-in-windows – fglez Jun 15 '12 at 08:36
-
This will not execute, as executed process is not shell, so there is JAVA_HONE, MAVEN_HOME etc settings. http://stackoverflow.com/questions/21397170/java-process-launched-with-runtime-getruntime-exec-cannot-create-temp-file – Paul Verest Jan 28 '14 at 13:55
-
I would think that mvn needing to be in the path goes without saying; no need to downvote for that. – Stijn Geukens Jan 28 '14 at 14:37
-
1This 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 '14 at 15:50
-
-
@StijnGeukens Yes, because he thinks Embeedder is not supported. This is not true. – MariuszS Jan 28 '14 at 19:17
-
At the time of writing (2 years ago) Maven embedder was officially not supported anymore. Now it indeed seems to be supported again. – Stijn Geukens Jan 29 '14 at 08:43