Maybe this is going to be a larger task than I had originally thought, but regardless, I'm trying to load a MavenProject
from a file and then resolve its dependencies. I've got the code for both bits but I'm missing some object references that I need; specifically I need to get instances of RepositorySystemSession
and RepositorySystem
. Any tips?
Note: I have tagged this question with maven-plugin, but this is not a Maven plugin. I am happy to mandate Maven 3 (think I already have anyway..)
Here's the code I have so far:
Constructing the MavenProject
:
public static MavenProject loadProject(File pomFile) throws Exception
{
MavenProject ret = null;
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
if (pomFile != null && pomFile.exists())
{
FileReader reader = null;
try
{
reader = new FileReader(pomFile);
Model model = mavenReader.read(reader);
model.setPomFile(pomFile);
ret = new MavenProject(model);
}
finally
{
// Close reader
}
}
return ret;
}
Resolving dependencies:
public static List<Dependency> getArtifactsDependencies(MavenProject project, String dependencyType, String scope) throws Exception
{
DefaultArtifact pomArtifact = new DefaultArtifact(project.getId());
RepositorySystemSession repoSession = null; // TODO
RepositorySystem repoSystem = null; // TODO
List<RemoteRepository> remoteRepos = project.getRemoteProjectRepositories();
List<Dependency> ret = new ArrayList<Dependency>();
Dependency dependency = new Dependency(pomArtifact, scope);
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(dependency);
collectRequest.setRepositories(remoteRepos);
DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
DependencyRequest projectDependencyRequest = new DependencyRequest(node, null);
repoSystem.resolveDependencies(repoSession, projectDependencyRequest);
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept(nlg);
ret.addAll(nlg.getDependencies(true));
return ret;
}
I realise this might be an unusual request, maybe I should just scrap what I was trying to do and wrap it as a plugin...but I kind of just want to finish what I started now! Thanks in advance.
I just whipped up a solution to both your and my problem:
pom.xml:
The code first loads the Maven project (using your function provided in the original question), then uses jcabi-aether to find the artifact in your local repository. You will need to change the two parameters in the main function: the location for the pom.xml of your project, and the location of your local repository.
Enjoy! :)
I would recommend to read the information about Aether lib which is exactly is for such kind of purposes.
Note: Aether was previousely developed at Sonatype, but has since moved to Eclipse.
Try this (as can be seen from the ather-demo):
There is a nice set of standalone examples for Eclipses Aether API which is used in the latest Maven (3.1.1) and it can be found here.
Note: Maven 3.1.X still uses Aether
0.9.0.M2
(and the latest version which us used in the examples is0.9.0.M3
). So to run these examples inside a Maven plugin, version M2 is required, and a standalone application can use the latest M3 version.Try jcabi-aether, which is a wrapper around Apache Aether from Sonatype:
If you are outside of Maven plugin:
This is covered in "Aether/Setting Aether Up" for the RepositorySystem and in "Aether/Creating a Repository System Session" in the eclipse wiki.
There is not a ton of documentation, but you can create a RepositorySystem as follows:
Do note that this requires the dependencies detailed in "Getting Aether", most notably
maven-aether-provider
.When you have your repository system the tutorial goes on to create a RepositorySystemSession with the following factory method: