I am working on an application for my company which needs to resolve dependencies for a maven project. This is a standalone application, not a maven plugin. The only thing I am trying to do at the moment is print the resolved dependencies to confirm that they were found. I am using Apache Maven Model (v4.0.0-alpha-2), Apache Maven Artifact Resolver (v1.8.2), and Maven Artifact Resolver Implementation (v1.8.2) to support my endeavor.
The initial setup is really what's nerfing me. I haven't had any luck finding update-to-date examples or documentation. This is the code I drafted up:
public static void main(String[] args)
throws LoadException, IOException, XmlPullParserException, DependencyResolutionException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader(new File("C:\\Users\\lc70844\\eclipse-workspace\\test\\pom.xml")));
DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
DefaultRepositorySystem repositorySystem = new DefaultRepositorySystem();
for (Dependency dependency : model.getDependencies()) {
DependencyRequest request = new DependencyRequest();
request.setRoot(new DefaultDependencyNode(
new org.eclipse.aether.graph.Dependency(toArtifact(dependency), dependency.getScope())));
DependencyResult result = repositorySystem.resolveDependencies(session, request);
result.getArtifactResults().stream().map(a -> a.getArtifact())
.map(a -> a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion()).forEach(System.out::println);
}
}
It is throwing an exception saying "repository system session's local repository manager cannot be null." This is pretty self-explanatory; we all know what I need to do. However, DefaultRepositorySystemSession#setLocalRepositoryManager(LocalRepositoryManager) requires a LocalRepositoryManager parameter, which is what I am having trouble figuring out how to setup. The repository I want to use is the the local repo at: %userprofile%/.m2/repository.
I found a few similar questions pertaining to my issue, but they seem to have the wrong context or are using deprecated libraries to achieve their end. I am hoping for a reliable, up-to-date solution.
Hopefully I am least heading in the right direction. What more do I need to do to get my list of dependencies?
EDIT
My current approach is to use Maven Invoker to run mvn dependency:list, but this is only temporary and is not a real solution. It takes too long to spin up the Maven process, and parsing the log output to get file paths is not clean approach. I am working to make my application more efficient, and using Maven Invoker is not an efficient method.
Well, if you have access to target
pom.xmlyou can simply executemvn dependency:treewhich would print complete tree of all the dependencies along with their scopes (both explicitly specified and transitive). The output looks like this:Alternatively, if you need to parse the output, it'd be easier with
mvn dependency:listormvn dependency:build-classpath. Pay attention, that this just prints the model and doesn't tell anything about whether particular dependency is resolved and cached in.m2, but you can check this with the output ofmvn dependency:classpath: just split the output line looking likewith
;and then check whether each file exists: