import org.apache.maven.repository.internal.DefaultServiceLocator;\r
import org.apache.maven.repository.internal.MavenRepositorySystemSession;\r
import org.argeo.slc.SlcException;\r
+import org.argeo.slc.aether.AetherUtils;\r
+import org.argeo.slc.aether.ConsoleRepositoryListener;\r
+import org.argeo.slc.aether.ConsoleTransferListener;\r
import org.codehaus.plexus.PlexusContainer;\r
import org.sonatype.aether.RepositorySystem;\r
import org.sonatype.aether.RepositorySystemSession;\r
import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;\r
import org.sonatype.aether.util.artifact.DefaultArtifact;\r
import org.sonatype.aether.util.graph.PreorderNodeListGenerator;\r
+import org.sonatype.aether.util.graph.selector.OptionalDependencySelector;\r
\r
public class MavenManager {\r
private final static Log log = LogFactory.getLog(MavenManager.class);\r
\r
public void testAether() {\r
try {\r
- RepositorySystem repoSystem = newRepositorySystem();\r
+ RepositorySystem repoSystem = createRepositorySystem();\r
\r
- RepositorySystemSession session = newSession(repoSystem);\r
+ RepositorySystemSession session = createRepositorySystemSession(repoSystem);\r
\r
Dependency dependency = new Dependency(new DefaultArtifact(\r
"org.argeo.slc.dep:org.argeo.slc.dep.sdk:0.13.1-SNAPSHOT"),\r
log.debug(artifact);\r
}\r
\r
- logDependencyNode(0, node);\r
+ AetherUtils.logDependencyNode(0, node);\r
// System.out.println(nlg.getClassPath());\r
- \r
+\r
} catch (Exception e) {\r
throw new SlcException("Cannot resolve", e);\r
}\r
\r
}\r
\r
- private void logDependencyNode(int depth, DependencyNode dependencyNode) {\r
- StringBuffer prefix = new StringBuffer(depth * 2 + 2);\r
- // prefix.append("|-");\r
- for (int i = 0; i < depth * 2; i++) {\r
- prefix.append(' ');\r
- }\r
- Artifact artifact = dependencyNode.getDependency().getArtifact();\r
- log.debug(prefix + "|-> " + artifact.getArtifactId() + " ["\r
- + artifact.getVersion() + "]");\r
- for (DependencyNode child : dependencyNode.getChildren())\r
- logDependencyNode(depth + 1, child);\r
- }\r
-\r
- private RepositorySystem newRepositorySystem() {\r
+ /** Creates a Maven {@link RepositorySystem}. */\r
+ public static RepositorySystem createRepositorySystem() {\r
try {\r
// return new\r
// DefaultPlexusContainer().lookup(RepositorySystem.class);\r
}\r
}\r
\r
- private RepositorySystemSession newSession(RepositorySystem system) {\r
+ public static RepositorySystemSession createRepositorySystemSession(\r
+ RepositorySystem system) {\r
MavenRepositorySystemSession session = new MavenRepositorySystemSession();\r
\r
LocalRepository localRepo = new LocalRepository(\r
.newLocalRepositoryManager(localRepo));\r
session.setTransferListener(new ConsoleTransferListener(System.out));\r
session.setRepositoryListener(new ConsoleRepositoryListener());\r
-\r
return session;\r
}\r
\r
}\r
\r
}\r
- /*\r
- * private final Log log = LogFactory.getLog(getClass());\r
- * \r
- * private String localRepositoryPath = System.getProperty("user.home") +\r
- * File.separator + ".m2" + File.separator + "repository";\r
- * \r
- * private ArtifactRepository localRepository; private\r
- * List<ArtifactRepository> remoteRepositoriesInternal; private\r
- * List<RemoteRepository> remoteRepositories = new\r
- * Vector<RemoteRepository>();\r
- * \r
- * private MavenEmbedder mavenEmbedder; private ClassLoader classLoader;\r
- * private Boolean offline = false;\r
- * \r
- * public void init() { try { mavenEmbedder = new SlcMavenEmbedder();\r
- * mavenEmbedder.setOffline(offline); //\r
- * mavenEmbedder.setAlignWithUserInstallation(true); if (classLoader !=\r
- * null) mavenEmbedder.setClassLoader(classLoader); else\r
- * mavenEmbedder.setClassLoader(getClass().getClassLoader()); // else //\r
- * mavenEmbedder.setClassLoader(Thread.currentThread() //\r
- * .getContextClassLoader()); mavenEmbedder.start();\r
- * \r
- * mavenEmbedder.setLocalRepositoryDirectory(new File(\r
- * localRepositoryPath));\r
- * \r
- * localRepository = mavenEmbedder.getLocalRepository();\r
- * \r
- * remoteRepositoriesInternal = new Vector<ArtifactRepository>(); for\r
- * (RemoteRepository remoteRepository : remoteRepositories) {\r
- * \r
- * ArtifactRepository repository = mavenEmbedder.createRepository(\r
- * remoteRepository.getUrl(), remoteRepository.getId());\r
- * remoteRepositoriesInternal.add(repository); }\r
- * \r
- * MavenFile mavenFile = new MavenFile();\r
- * mavenFile.setGroupId("org.argeo.slc.dist");\r
- * mavenFile.setArtifactId("org.argeo.slc.sdk");\r
- * mavenFile.setVersion("0.12.2-SNAPSHOT"); mavenFile.setType("pom");\r
- * Artifact artifact = resolve(mavenFile); log.debug("Location of " +\r
- * artifact + " : " + artifact.getFile()); // log.debug("Dependencies of " +\r
- * artifact); // for (Object obj : artifact.getDependencyTrail()) { //\r
- * log.debug(" " + obj); // }\r
- * \r
- * File pomFile = new File(\r
- * "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/pom.xml");\r
- * MavenProject project = mavenEmbedder\r
- * .readProjectWithDependencies(pomFile); // MavenProject project =\r
- * mavenEmbedder // .readProjectWithDependencies(artifact.getFile());\r
- * \r
- * // EventMonitor eventMonitor = new EventMonitor() { // // public void\r
- * startEvent(String eventName, String target, // long timestamp) { //\r
- * log.debug(eventName + ", " + target + ", " + timestamp); // } // //\r
- * public void errorEvent(String eventName, String target, // long\r
- * timestamp, Throwable cause) { // log.debug(eventName + ", " + target +\r
- * ", " + timestamp); // } // // public void endEvent(String eventName,\r
- * String target, // long timestamp) { // log.debug(eventName + ", " +\r
- * target + ", " + timestamp); // } // }; // // String[] goals = { "clean",\r
- * "install" }; // mavenEmbedder.execute(project, Arrays.asList(goals), //\r
- * eventMonitor, // null, null, pomFile.getParentFile());\r
- * \r
- * Set<Artifact> transitDeps = getTransitiveProjectDependencies( project,\r
- * remoteRepositoriesInternal, localRepository);\r
- * log.debug(transitDeps.size() + " dependencies for " + artifact); for\r
- * (Object obj : transitDeps) { log.debug(" " + obj); }\r
- * \r
- * } catch (Exception e) { throw new\r
- * SlcException("Cannot initialize Maven manager", e); } }\r
- * \r
- * @SuppressWarnings("unchecked") public Set<Artifact>\r
- * getTransitiveProjectDependencies(MavenProject project,\r
- * List<ArtifactRepository> remoteRepos, ArtifactRepository local) {\r
- * Embedder embedder = mavenEmbedder.getEmbedder(); try { ArtifactFactory\r
- * artifactFactory = (ArtifactFactory) embedder\r
- * .lookup(ArtifactFactory.ROLE);\r
- * \r
- * ArtifactResolver artifactResolver = (ArtifactResolver) embedder\r
- * .lookup(ArtifactResolver.ROLE);\r
- * \r
- * ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource)\r
- * embedder .lookup(ArtifactMetadataSource.ROLE);\r
- * \r
- * Set<Artifact> artifacts = project.createArtifacts(artifactFactory, null,\r
- * null);\r
- * \r
- * ArtifactResolutionResult arr = artifactResolver\r
- * .resolveTransitively(artifacts, project.getArtifact(), local,\r
- * remoteRepos, artifactMetadataSource, null);\r
- * \r
- * return arr.getArtifacts(); } catch (Exception e) { throw new\r
- * SlcException("Cannot resolve dependency for " + project, e); } // Order,\r
- * just for display // Set dependencies = new TreeSet(new\r
- * ArtifactComparator()); // dependencies.addAll(arr.getArtifacts()); //\r
- * return dependencies; }\r
- * \r
- * private Artifact resolve(MavenFile mavenDistribution) { try { Artifact\r
- * artifact; if (mavenDistribution.getClassifier() == null) { artifact =\r
- * mavenEmbedder.createArtifact(mavenDistribution .getGroupId(),\r
- * mavenDistribution.getArtifactId(), mavenDistribution.getVersion(),\r
- * Artifact.SCOPE_PROVIDED, mavenDistribution.getType()); } else { artifact\r
- * = mavenEmbedder.createArtifactWithClassifier(\r
- * mavenDistribution.getGroupId(), mavenDistribution .getArtifactId(),\r
- * mavenDistribution .getVersion(), mavenDistribution.getType(),\r
- * mavenDistribution.getClassifier()); }\r
- * \r
- * mavenEmbedder.resolve(artifact, remoteRepositoriesInternal,\r
- * localRepository);\r
- * \r
- * return artifact; } catch (Exception e) { throw new\r
- * SlcException("Cannot resolve artifact.", e); } }\r
- * \r
- * public File getPackageLocation(MavenFile mavenDistribution) { return\r
- * resolve(mavenDistribution).getFile(); }\r
- * \r
- * public void destroy() { try { if (mavenEmbedder != null) {\r
- * mavenEmbedder.stop(); } } catch (MavenEmbedderException e) {\r
- * log.error("Cannot destroy Maven manager", e); } }\r
- * \r
- * public void setLocalRepositoryPath(String localRepositoryPath) {\r
- * this.localRepositoryPath = localRepositoryPath; }\r
- * \r
- * public List<RemoteRepository> getRemoteRepositories() { return\r
- * remoteRepositories; }\r
- * \r
- * public void setRemoteRepositories(List<RemoteRepository>\r
- * remoteRepositories) { this.remoteRepositories = remoteRepositories; }\r
- * \r
- * public void setClassLoader(ClassLoader classLoader) { this.classLoader =\r
- * classLoader; }\r
- * \r
- * public void setOffline(Boolean offline) { this.offline = offline; }\r
- */\r
}\r