1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| 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.Dependency; 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 MavenUtils { private static final String CLSPTH_SEP = File.separator.equals("/") ? ":" : ";";
public static String getClasspathFromMavenProject(File projectPom, File localRepoFolder) throws DependencyResolutionException, IOException, XmlPullParserException { MavenProject proj = loadProject(projectPom);
proj.setRemoteArtifactRepositories( Arrays.asList((ArtifactRepository) new MavenArtifactRepository("central", "http://mvn.fdx.net:8080/artifactory/repo", new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy())));
String classpath = ""; Aether aether = new Aether(proj, localRepoFolder);
List<Dependency> dependencies = proj.getDependencies(); Iterator<Dependency> it = dependencies.iterator();
while (it.hasNext()) { org.apache.maven.model.Dependency depend = it.next();
final Collection<Artifact> deps = aether.resolve( new DefaultArtifact(depend.getGroupId(), depend.getArtifactId(), depend.getClassifier(), depend.getType(), depend.getVersion()), JavaScopes.RUNTIME);
Iterator<Artifact> artIt = deps.iterator(); while (artIt.hasNext()) { Artifact art = artIt.next(); classpath = classpath + CLSPTH_SEP + art.getFile().getAbsolutePath(); } }
return classpath; }
private 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; }
public static void main(String[] args) throws Exception { String classpath = getClasspathFromMavenProject( new File("/Users/fdx/pom.xml"), new File("/Users/fdx/.m2/repository")); System.out.println("classpath = " + classpath); } }
|