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 depen
I just whipped up a solution to both your and my problem:
/*******************************************************************************
* Copyright (c) 2013 TerraFrame, Inc. All rights reserved.
*
* This file is part of Runway SDK(tm).
*
* Runway SDK(tm) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Runway SDK(tm) is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Runway SDK(tm). If not, see .
******************************************************************************/
package com.test.mavenaether;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.resolution.DependencyResolutionException;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.artifact.JavaScopes;
import com.jcabi.aether.Aether;
public class MavenAether
{
public static void main(String[] args) throws Exception
{
String classpath = getClasspathFromMavenProject(new File("/users/terraframe/documents/workspace/MavenSandbox/pom.xml"), new File("/users/terraframe/.m2/repository"));
System.out.println("classpath = " + classpath);
}
public static String getClasspathFromMavenProject(File projectPom, File localRepoFolder) throws DependencyResolutionException, IOException, XmlPullParserException
{
MavenProject proj = loadProject(projectPom);
proj.setRemoteArtifactRepositories(
Arrays.asList(
(ArtifactRepository) new MavenArtifactRepository(
"maven-central", "http://repo1.maven.org/maven2/", new DefaultRepositoryLayout(),
new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy()
)
)
);
String classpath = "";
Aether aether = new Aether(proj, localRepoFolder);
List dependencies = proj.getDependencies();
Iterator it = dependencies.iterator();
while (it.hasNext()) {
org.apache.maven.model.Dependency depend = it.next();
final Collection deps = aether.resolve(
new DefaultArtifact(depend.getGroupId(), depend.getArtifactId(), depend.getClassifier(), depend.getType(), depend.getVersion()),
JavaScopes.RUNTIME
);
Iterator artIt = deps.iterator();
while (artIt.hasNext()) {
Artifact art = artIt.next();
classpath = classpath + " " + art.getFile().getAbsolutePath();
}
}
return classpath;
}
public static MavenProject loadProject(File pomFile) throws IOException, XmlPullParserException
{
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
{
reader.close();
}
}
return ret;
}
}
pom.xml:
4.0.0
com.test
MavenSandbox
0.0.1-SNAPSHOT
com.jcabi
jcabi-aether
0.7.19
org.apache.maven
maven-core
3.0.3
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! :)