Using maven programmatically

maven

Solution

The Good

I have a project, Naether, that is a wrapper for Maven's dependency resolution lib Aether.

Using Naether, you can resolve Dependencies

import com.tobedevoured.naether.api.Naether;
import com.tobedevoured.naether.impl.NaetherImpl;

Naether naether = new NaetherImpl();
naether.addDependency( "ch.qos.logback:logback-classic:jar:0.9.29" );
naether.addDependency( "junit:junit:jar:4.8.2" );
naether.resolveDependencies();
System.out.println( naether.getDependenciesNotation().toString() );

Will output:

["ch.qos.logback:logback-core:jar:0.9.29",
 "ch.qos.logback:logback-classic:jar:0.9.29",
 "junit:junit:jar:4.8.2",
 "org.slf4j:slf4j-api:jar:1.6.1" ]

The Bad

I have no idea how to build (such as compile the source) a pom.xml via Java. I have searched around a little, but have not found a concrete example. A ProjectArtifact is just a artifact descriptor that Maven uses to resolve a POM, such as a parent POM. It does not expose build actions. Since there are a million ways to build a Maven project, there is no simple install method. You have to start the lifecycle of the install process somehow.

What Naether can do, build the project first and have Naether install it:

import com.tobedevoured.naether.api.Naether;
import com.tobedevoured.naether.impl.NaetherImpl;

Naether naether = new NaetherImpl();
naether.install( "com.example:sample:0.0.1", "/tmp/myproject/pom.xml", "/tmp/myproject/target/sample-0.0.1.jar" )

Update - How does it all fit together?

Building, deploy, installing, etc a Project is complicated. Maven does a pretty good job of simplifying it. Even though the Maven task is only install, there are numerous steps involved for that to work. For a simple Java project, that means populating the class path, compiling source, packaging the jar, and than installing it in the local Maven repository. Things only get more complicated when you talk about other ways to package a Java project, like a war.

The folk at Maven did the hard work and spun off the dependency resolution to its own library, Aether. This does all the heavy lifting of working with artifacts. Aether lets you figuring out what the dependencies are for a Project, download the dependencies. Aether also lets you install an artifact locally or deploy it to a remote repo.

What Aether does not do is manage a project. It does not clean up the target dir or compile source.

What I have created with Naether is just a simplified way to access Aether.

Problem

I need to make a small program that downloads maven projects and prints its dependencies something like this: ``` MavenArtifactRepository repository = new MavenArtifactRepository("typesafe", "http://repo.typesafe.com/typesafe/releases/", ..., ..., ...); downloadAndPrintDependencies(repository, "org.hsqldb", "hsqldb", "2.2.9"); void downloadAndPrintDependencies(repository, groupId, artifactId, version) { MavenProject projectDescription = new MavenProject("org.hsqldb", "hsqldb", "2.2.9"); Artifact artifact = repository.getProject(projectDescription); // this would download the artificat in the local repository if necessary List<Dependency> dependecies = artifact.getDependencies(); ... } ``` and, that can execute goals on a maven project, something like this: ``` String pomXmlFile = "/tmp/myproject/pom.xml"; Reader reader = new FileReader(pomXmlFile); MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); Model model = xpp3Reader.read(reader); ProjectArtifact projectArtifact = new ProjectArtifact(model); projectArtifact.clean(); projectArtifact.install(); ``` any feedback on the pseudo-code? What is the correct class and function that fetches an artifact from the repository? what is the correct class and function that executes goals (such as clean and install) on maven projects?

Original source