12

I am attempting to retrieve the entire dependency tree and their poms starting at the root of the project. I am starting with a POM already existing in my file system, but I'm not sure how to retrieve the dependency poms from the repository.

I am using the following code to access the dependency list. From the list I have all the information on the artifacts. I'm just not sure how to access the repository.

FileReader reader = null;
Model model = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();

File pomfile = new File("pom.xml");

model.setPomFile(pomfile);
MavenProject project = new MavenProject(model);

List<Dependency> deps = project.getDependencies();

// Get dependency details
for (Dependency d: deps) {          
    System.out.print(d.getArtifactId());
    System.out.print(":");
    System.out.println(d.getVersion()); 
}           
davdic
  • 249
  • 3
  • 10

2 Answers2

5

Actually you were pretty close to reading the dependencies. All that was missing was:

Model model = mavenreader.read(new FileReader(pomFile));

Full example:

MavenXpp3Reader mavenreader = new MavenXpp3Reader();

File pomfile = new File("pom.xml");
Model model = mavenreader.read(new FileReader(pomFile));

List<Dependency> deps = model.getDependencies();

for (Dependency d: deps) {          
    System.out.print(d.getArtifactId());
} 

This doesn't get you a dependency tree but you can repeat it for the dependencies you find.

Andrejs
  • 26,885
  • 12
  • 107
  • 96
1

Unfotunately that is not that trivial :-) I can give you some advices though (taking into account that you need artifacts only from the central repo).

Here's a great example code I found at Github. An alternative could be using the REST API of the central repo. Here's an example how to do that programmatically.

BTW if you need the dependencies only, you could also use the maven-dependency-plugin directly (this is called on e.g. mvn dependency:tree - see this thread for an example).

Probably your method can also work, but I guess it needs some missing parts.

Community
  • 1
  • 1
rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • I'm actually trying to programmaticaly retrieve the poms from my local repository (not the central repo) using the Maven API. I could look for the local repository through the file system, but this needs to be able to work anywhere with any user, so I thought that the API would be more appropriate. – davdic Jul 26 '12 at 15:04
  • @rlegendi I don't know your use case but if you need only the dependencies of some public maven libraries you can use the VersionEye API: https://www.versioneye.com/api?version=v2. It is a REST JSON API and very easy to use. – Robert Reiz Jul 30 '13 at 10:22
  • @RobertReiz Didn't you want to address your comment to @davdic? :-) Anyway, thx for the link, but it seems commercial and a beta product. – rlegendi Aug 05 '13 at 09:55
  • @rlegendi Yes. It is out of Beta now. it is commercial but the API you can use for free. – Robert Reiz Aug 08 '13 at 11:39
  • @rlegendi In the mean while we have even a maven plugin. Check it out here: https://github.com/versioneye/versioneye_maven_plugin. Let me know what you think. – Robert Reiz Nov 29 '13 at 13:07
  • @RobertReiz Hey Robert, thx for the ping! Let me take a look at it! :-) – rlegendi Nov 30 '13 at 17:11