Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically call a Maven-task

Tags:

I'm using Maven in the context of another build-tool (leiningen for Clojure, but this should not matter), and I would like to know how I would call a plugin like dependency:build-classpath programmatically (i.e. via the Maven-API, not via the mvn-command).

like image 668
pmf Avatar asked Jan 27 '10 11:01

pmf


1 Answers

Use the Maven Invoker API. Its quick and easy.

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "install" ) );

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("/usr"));

try
{
  invoker.execute( request );
}
catch (MavenInvocationException e)
{
  e.printStackTrace();
}

pom.xml:

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-invoker</artifactId>
    <version>2.1.1</version>
</dependency>
like image 192
Ring Avatar answered Oct 02 '22 01:10

Ring