Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic builds using Maven 3 API

Tags:

java

maven

Before you ask why I can just spawn a process to execute mvn, I wish to execute a Maven build through the Maven API, so that I can gather info on what goes on in the build, the artifacts produced etc etc. After depending on org.apache.maven:maven-core:jar:3.0.4, I have written the following method in an attempt to do such a thing:

public static void build(File directory, File pom) {
    Maven maven = new DefaultMaven();
    MavenExecutionRequest exec = new DefaultMavenExecutionRequest();
    exec.setBaseDirectory(directory);
    exec.setPom(pom);
    MavenExecutionResult result = maven.execute(exec);
    MavenProject proj = result.getProject();
    Artifact art = proj.getArtifact();
    System.out.println(art);
}

However this code fails at maven.execute due to null pointer exceptions. These null pointer exceptions are basically everywhere due to private fields in DefaultMaven not being initialized. They are all annotated with @Required, so I am guessing this is something to do with Plexus.

How can I successfully use Maven to execute such a build?

like image 997
md_5 Avatar asked Jan 11 '13 10:01

md_5


People also ask

What is Maven build?

Maven is a popular open-source build tool developed by the Apache Group to build, publish, and deploy several projects at once for better project management. The tool provides allows developers to build and document the lifecycle framework.

How do I build a project using mvn clean package?

Let's open the command console, go the C:\MVN\consumerBanking directory and execute the following mvn command. Maven will start building the project. We give maven two goals, first to clean the target directory (clean) and then package the project build output as jar (package).

Is Maven an API?

all tiers. This is the API documentation for Maven Packages. This API is used by the Maven package manager client and is generally not meant for manual consumption.

How do I mvn a package?

mvn install is the option that is most often used. mvn package is seldom used, only if you're debugging some issue with the maven build process. Note that mvn package will only create a jar file. mvn install will do that and install the jar (and class etc.)


2 Answers

You'll want to use the actual Maven embedding API:

http://maven.apache.org/ref/3.0/maven-embedder/apidocs/index.html

To see examples, look towards the open source of M2Eclipse.

Now, this component is not really very well-named. It's actually a convenience wrapper aimed at making an CLI. so, what you'll want to do is read the source of it.

like image 152
bmargulies Avatar answered Sep 22 '22 11:09

bmargulies


Internet searches for how to do Maven builds using the API always lead me here, so I'd like to document how I've solved this issue. Do not use DefaultMaven directly. Instead, use the maven-verifier project:

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-verifier</artifactId>
    <version>1.6</version>
</dependency>

Then you can use the Verifier class to build a project. The only catch is to pass the proper environment variable:

String baseDir = "<root path of the maven project you want to build>";
Map<String,String> env=new HashMap<>();
env.put("maven.multiModuleProjectDirectory", baseDir);

try {
    Verifier v=new Verifier(baseDir);
    v.executeGoals(Arrays.asList("clean","package"),env);
} catch (Exception e) {
    e.printStackTrace();
}

This resulted for me in the project being built, and a log.txt file being created in baseDir with the output of maven.

Hope this helps!

like image 34
JP Moresmau Avatar answered Sep 20 '22 11:09

JP Moresmau