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?
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 );
-
Is there any way that we can run
maven
plugins on a system where bothjava
andmaven
is not present. Please have a look at the question [stackoverflow.com/questions/44911102/… Jul 5, 2017 at 5: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
-
1Can the maven embedder be downloaded as a jar if the project using it isn't using maven?– tbodtJan 30, 2014 at 18:48
-
1no, what I mean is, can you use maven embedder in a project that doesn't use maven? and how?– tbodtJan 30, 2014 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 preparingpom.xml
.– MariuszSJan 30, 2014 at 18:57 -
1@MariuszS Is there any way that we can run
maven
plugins on a system where bothjava
andmaven
is not present. Please have a look at the question [stackoverflow.com/questions/44911102/… Jul 5, 2017 at 5:39 -
1See stackoverflow.com/questions/22410706/… for why more work can be required, especially inside a custom Maven plugin!– InfernozDec 7, 2020 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.
-
Maven embedder is cross platform, but your first solution is just for Unix. For Windows, see stackoverflow.com/questions/9674220/…– fglezJun 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
-
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. Jan 28, 2014 at 15:50 -
exec()
. A good answer would be from those who really accomplishes and can compare or give a new option.