-package org.argeo.slc.maven;\r
+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
\r
-import java.io.File;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.Vector;\r
+package org.argeo.slc.maven;\r
\r
import org.apache.commons.logging.Log;\r
import org.apache.commons.logging.LogFactory;\r
-import org.apache.maven.artifact.Artifact;\r
-import org.apache.maven.artifact.factory.ArtifactFactory;\r
-import org.apache.maven.artifact.metadata.ArtifactMetadataSource;\r
-import org.apache.maven.artifact.repository.ArtifactRepository;\r
-import org.apache.maven.artifact.resolver.ArtifactNotFoundException;\r
-import org.apache.maven.artifact.resolver.ArtifactResolutionException;\r
-import org.apache.maven.artifact.resolver.ArtifactResolutionResult;\r
-import org.apache.maven.artifact.resolver.ArtifactResolver;\r
-import org.apache.maven.embedder.MavenEmbedderException;\r
-import org.apache.maven.project.MavenProject;\r
-import org.apache.maven.project.artifact.InvalidDependencyVersionException;\r
+import org.apache.maven.cli.MavenCli;\r
+import org.apache.maven.repository.internal.DefaultServiceLocator;\r
+import org.apache.maven.repository.internal.MavenRepositorySystemSession;\r
import org.argeo.slc.SlcException;\r
-import org.codehaus.plexus.component.repository.exception.ComponentLookupException;\r
-import org.codehaus.plexus.embed.Embedder;\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.artifact.Artifact;\r
+import org.sonatype.aether.collection.CollectRequest;\r
+import org.sonatype.aether.connector.wagon.WagonProvider;\r
+import org.sonatype.aether.connector.wagon.WagonRepositoryConnectorFactory;\r
+import org.sonatype.aether.graph.Dependency;\r
+import org.sonatype.aether.graph.DependencyNode;\r
+import org.sonatype.aether.repository.LocalRepository;\r
+import org.sonatype.aether.repository.RemoteRepository;\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
-\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;\r
- private List<ArtifactRepository> remoteRepositoriesInternal;\r
- private List<RemoteRepository> remoteRepositories = new Vector<RemoteRepository>();\r
-\r
- private SlcMavenEmbedder mavenEmbedder;\r
- private ClassLoader classLoader;\r
- private Boolean offline = false;\r
+ private final static Log log = LogFactory.getLog(MavenManager.class);\r
\r
public void init() {\r
try {\r
- mavenEmbedder = new SlcMavenEmbedder();\r
- mavenEmbedder.setOffline(offline);\r
- //mavenEmbedder.setAlignWithUserInstallation(true);\r
- if (classLoader != null)\r
- mavenEmbedder.setClassLoader(classLoader);\r
- else\r
- mavenEmbedder.setClassLoader(getClass().getClassLoader());\r
- // else\r
- // mavenEmbedder.setClassLoader(Thread.currentThread()\r
- // .getContextClassLoader());\r
- mavenEmbedder.start();\r
-\r
- mavenEmbedder.setLocalRepositoryDirectory(new File(\r
- localRepositoryPath));\r
-\r
- localRepository = mavenEmbedder.getLocalRepository();\r
-\r
- remoteRepositoriesInternal = new Vector<ArtifactRepository>();\r
- for (RemoteRepository remoteRepository : remoteRepositories) {\r
-\r
- ArtifactRepository repository = mavenEmbedder.createRepository(\r
- remoteRepository.getUrl(), remoteRepository.getId());\r
- remoteRepositoriesInternal.add(repository);\r
- }\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");\r
- mavenFile.setType("pom");\r
- Artifact artifact = resolve(mavenFile);\r
- log.debug("Location of " + artifact + " : " + artifact.getFile());\r
- // log.debug("Dependencies of " + artifact);\r
- // for (Object obj : artifact.getDependencyTrail()) {\r
- // log.debug(" " + obj);\r
+ testMaven();\r
+ testAether();\r
+ // List<ComponentDescriptor<?>> lst = plexusContainer\r
+ // .discoverComponents(plexusContainer.getContainerRealm());\r
+ // for (ComponentDescriptor<?> cd : lst) {\r
+ // log.debug(cd);\r
+ // }\r
+ // ArtifactHandler artifactHandler = plexusContainer\r
+ // .lookup(ArtifactHandler.class);\r
+ // ArtifactRepository localRepository = new\r
+ // DefaultArtifactRepositoryFactory().createArtifactRepository("local",\r
+ // , layoutId, snapshots, releases);\r
+ // Maven maven = mavenCli.getContainer().lookup(Maven.class);\r
+ // Artifact artifact = new DefaultArtifact("org.argeo.slc.dist",\r
+ // "org.argeo.slc.sdk", "0.13.1-SNAPSHOT", "compile", "pom",\r
+ // null, artifactHandler);\r
+ // ArtifactResolutionRequest req = new ArtifactResolutionRequest();\r
+ // req.setLocalRepository(localRepository);\r
+ // req.setResolveTransitively(true);\r
+ // req.setArtifact(artifact);\r
+ // ArtifactResolver artifactResolver = plexusContainer\r
+ // .lookup(ArtifactResolver.class);\r
+ // ArtifactResolutionResult res = artifactResolver.resolve(req);\r
+ // Set<Artifact> artifacts = res.getArtifacts();\r
+ // for (Artifact art : artifacts) {\r
+ // log.debug(art);\r
// }\r
-\r
- MavenProject project = mavenEmbedder\r
- .readProjectWithDependencies(new File(\r
- "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/pom.xml"));\r
- // MavenProject project = mavenEmbedder\r
- // .readProjectWithDependencies(artifact.getFile());\r
-\r
- log.debug("Dependencies of " + artifact);\r
- for (Object obj : getTransitiveProjectDependencies(project,\r
- remoteRepositoriesInternal, localRepository)) {\r
- log.debug(" " + obj);\r
- }\r
-\r
} catch (Exception e) {\r
- throw new SlcException("Cannot initialize Maven manager", e);\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
}\r
}\r
\r
- public Set getTransitiveProjectDependencies(MavenProject project,\r
- List remoteRepos, ArtifactRepository local) {\r
- Embedder embedder = mavenEmbedder.getEmbedder();\r
+ public void testMaven() {\r
+ Thread.currentThread().setContextClassLoader(\r
+ getClass().getClassLoader());\r
+ String[] goals = { "-o", "-e", "-f",\r
+ "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/pom.xml",\r
+ "dependency:tree" };\r
+\r
+ // String m2Home = "/opt/apache-maven-3.0.1";\r
+ // System.setProperty("classworlds.conf", m2Home + "/bin/m2.conf");\r
+ // System.setProperty("maven.home", m2Home);\r
+ //\r
+ // Launcher.main(goals);\r
+\r
+ CustomCli mavenCli = new CustomCli();\r
+ mavenCli.doMain(goals,\r
+ "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk",\r
+ System.out, System.err);\r
+\r
+ PlexusContainer plexusContainer = mavenCli.getContainer();\r
+ log.debug(plexusContainer.getContext().getContextData());\r
+ plexusContainer.dispose();\r
+ }\r
+\r
+ public void testAether() {\r
try {\r
- ArtifactFactory artifactFactory = (ArtifactFactory) embedder\r
- .lookup(ArtifactFactory.ROLE);\r
+ RepositorySystem repoSystem = createRepositorySystem();\r
\r
- ArtifactResolver artifactResolver = (ArtifactResolver) embedder\r
- .lookup(ArtifactResolver.ROLE);\r
+ RepositorySystemSession session = createRepositorySystemSession(repoSystem);\r
\r
- ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) embedder\r
- .lookup(ArtifactMetadataSource.ROLE);\r
+ Dependency dependency = new Dependency(new DefaultArtifact(\r
+ "org.argeo.slc.dep:org.argeo.slc.dep.sdk:0.13.1-SNAPSHOT"),\r
+ "compile");\r
+ RemoteRepository argeo = new RemoteRepository("argeo", "default",\r
+ "http://maven.argeo.org/argeo/");\r
+ RemoteRepository argeoSnapshots = new RemoteRepository(\r
+ "argeo-snapshots", "default",\r
+ "http://dev.argeo.org/maven/argeo-snapshots/");\r
\r
- Set artifacts = project\r
- .createArtifacts(artifactFactory, null, null);\r
+ CollectRequest collectRequest = new CollectRequest();\r
+ collectRequest.setRoot(dependency);\r
+ collectRequest.addRepository(argeo);\r
+ collectRequest.addRepository(argeoSnapshots);\r
+ DependencyNode node = repoSystem.collectDependencies(session,\r
+ collectRequest).getRoot();\r
\r
- ArtifactResolutionResult arr = artifactResolver\r
- .resolveTransitively(artifacts, project.getArtifact(),\r
- local, remoteRepos, artifactMetadataSource, null);\r
+ repoSystem.resolveDependencies(session, node, null);\r
\r
- return arr.getArtifacts();\r
- } catch (Exception e) {\r
- throw new SlcException("Cannot resolve dependency for " + project,\r
- e);\r
- }\r
- // Order, just for display\r
- // Set dependencies = new TreeSet(new ArtifactComparator());\r
- // dependencies.addAll(arr.getArtifacts());\r
- // return dependencies;\r
- }\r
+ PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();\r
+ node.accept(nlg);\r
\r
- private Artifact resolve(MavenFile mavenDistribution) {\r
- try {\r
- Artifact artifact;\r
- if (mavenDistribution.getClassifier() == null) {\r
- artifact = mavenEmbedder.createArtifact(mavenDistribution\r
- .getGroupId(), mavenDistribution.getArtifactId(),\r
- mavenDistribution.getVersion(),\r
- Artifact.SCOPE_PROVIDED, mavenDistribution.getType());\r
- } else {\r
- artifact = mavenEmbedder.createArtifactWithClassifier(\r
- mavenDistribution.getGroupId(), mavenDistribution\r
- .getArtifactId(), mavenDistribution\r
- .getVersion(), mavenDistribution.getType(),\r
- mavenDistribution.getClassifier());\r
+ for (Artifact artifact : nlg.getArtifacts(true)) {\r
+ log.debug(artifact);\r
}\r
\r
- mavenEmbedder.resolve(artifact, remoteRepositoriesInternal,\r
- localRepository);\r
+ AetherUtils.logDependencyNode(0, node);\r
+ // System.out.println(nlg.getClassPath());\r
\r
- return artifact;\r
} catch (Exception e) {\r
- throw new SlcException("Cannot resolve artifact.", e);\r
+ throw new SlcException("Cannot resolve", e);\r
}\r
- }\r
\r
- public File getPackageLocation(MavenFile mavenDistribution) {\r
- return resolve(mavenDistribution).getFile();\r
}\r
\r
- public void destroy() {\r
+ /** Creates a Maven {@link RepositorySystem}. */\r
+ public static RepositorySystem createRepositorySystem() {\r
try {\r
- if (mavenEmbedder != null) {\r
- mavenEmbedder.stop();\r
- }\r
- } catch (MavenEmbedderException e) {\r
- log.error("Cannot destroy Maven manager", e);\r
+ // return new\r
+ // DefaultPlexusContainer().lookup(RepositorySystem.class);\r
+\r
+ DefaultServiceLocator locator = new DefaultServiceLocator();\r
+\r
+ locator.setServices(WagonProvider.class, new ManualWagonProvider());\r
+ locator.addService(RepositoryConnectorFactory.class,\r
+ WagonRepositoryConnectorFactory.class);\r
+\r
+ return locator.getService(RepositorySystem.class);\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot lookup repository system", e);\r
}\r
}\r
\r
- public void setLocalRepositoryPath(String localRepositoryPath) {\r
- this.localRepositoryPath = localRepositoryPath;\r
+ public static RepositorySystemSession createRepositorySystemSession(\r
+ RepositorySystem system) {\r
+ MavenRepositorySystemSession session = new MavenRepositorySystemSession();\r
+\r
+ LocalRepository localRepo = new LocalRepository(\r
+ System.getProperty("user.home") + "/.m2/repository");\r
+ session.setLocalRepositoryManager(system\r
+ .newLocalRepositoryManager(localRepo));\r
+ session.setTransferListener(new ConsoleTransferListener(System.out));\r
+ session.setRepositoryListener(new ConsoleRepositoryListener());\r
+ return session;\r
}\r
\r
- public List<RemoteRepository> getRemoteRepositories() {\r
- return remoteRepositories;\r
+ public static void main(String[] args) {\r
+ new MavenManager().init();\r
}\r
\r
- public void setRemoteRepositories(List<RemoteRepository> remoteRepositories) {\r
- this.remoteRepositories = remoteRepositories;\r
- }\r
+ static class CustomCli extends MavenCli {\r
+ private PlexusContainer container;\r
\r
- public void setClassLoader(ClassLoader classLoader) {\r
- this.classLoader = classLoader;\r
- }\r
+ @Override\r
+ protected void customizeContainer(PlexusContainer container) {\r
+ this.container = container;\r
+ }\r
\r
- public void setOffline(Boolean offline) {\r
- this.offline = offline;\r
- }\r
+ public PlexusContainer getContainer() {\r
+ return container;\r
+ }\r
\r
+ }\r
}\r