7

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?

David Portabella
  • 12,390
  • 27
  • 101
  • 182

3 Answers3

4

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.

mguymon
  • 8,946
  • 2
  • 39
  • 61
  • so, aether is a library to use maven repositories (or also types of repositories also?). but using a command such as "mvn clean install" already downaloads and install artifacts, so i guess that the maven source code already includes this fonctionallity. or the "mvn clean install" uses aether? and then, there is your project "Naether" that extends Aether; what extra fonctionality it uses? i am overwhelmed by these three layers just to access the maven artifacts. ??? – David Portabella Apr 18 '13 at 14:43
  • I added a little blurb that will hopefully explain how it all fits together. – mguymon Apr 18 '13 at 15:47
  • thanks; do you know also the answer to this new question? http://stackoverflow.com/questions/16101685/aether-demo-does-not-compile-because-of-renaming-of-packages – David Portabella Apr 19 '13 at 09:41
  • I have not used the latest version of Aether, only on the previous release. – mguymon Apr 19 '13 at 16:18
  • 1
    thanks for writing naether. it definitely made resolving dependencies in a couple of plugins (one maven and one gradle) I was developing easier – jett Apr 22 '16 at 08:05
1

Some suggest using Aether, examples here.

Ali Shakiba
  • 20,549
  • 18
  • 61
  • 88
1

Take a look at jcabi-aether (I'm a developer). You will be able to resolve transitive dependencies of any arbitrary Maven artifact.

yegor256
  • 102,010
  • 123
  • 446
  • 597