X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.maven%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fmaven%2FMavenManager.java;h=2c6fddd4b42b50d93e0edc9d06d29493b04a2049;hb=701d50c2a2308672129ca761036500f7492a84b0;hp=28287b227a9c98ceadab701fb50767e3af8d130e;hpb=79c3c7ee7692ea7fb3bfa2031771f27f67727603;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenManager.java b/runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenManager.java index 28287b227..2c6fddd4b 100644 --- a/runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenManager.java +++ b/runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenManager.java @@ -1,20 +1,51 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.slc.maven; -import java.io.File; -import java.util.List; -import java.util.Vector; +import org.apache.maven.cli.MavenCli; +import org.codehaus.plexus.PlexusContainer; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -//import org.apache.maven.artifact.Artifact; -//import org.apache.maven.artifact.repository.ArtifactRepository; -//import org.apache.maven.embedder.MavenEmbedder; -//import org.apache.maven.embedder.MavenEmbedderException; -import org.argeo.slc.SlcException; public class MavenManager { - /* + + public static void main(String[] args){ + //CustomCli mavenCli = new CustomCli(); + MavenCli mavenCli = new MavenCli(); + String[] goals = { "-e","dependency:tree" }; + mavenCli.doMain(goals, "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk", System.out, System.err); + } + + static class CustomCli extends MavenCli{ + private PlexusContainer container; + + @Override + protected void customizeContainer(PlexusContainer container) { + this.container = container; + } + + public PlexusContainer getContainer() { + return container; + } + + + + } +/* private final Log log = LogFactory.getLog(getClass()); private String localRepositoryPath = System.getProperty("user.home") @@ -25,13 +56,21 @@ public class MavenManager { private List remoteRepositories = new Vector(); private MavenEmbedder mavenEmbedder; + private ClassLoader classLoader; + private Boolean offline = false; public void init() { try { - mavenEmbedder = new MavenEmbedder(); - mavenEmbedder.setOffline(true); - mavenEmbedder.setClassLoader(Thread.currentThread() - .getContextClassLoader()); + mavenEmbedder = new SlcMavenEmbedder(); + mavenEmbedder.setOffline(offline); + // mavenEmbedder.setAlignWithUserInstallation(true); + if (classLoader != null) + mavenEmbedder.setClassLoader(classLoader); + else + mavenEmbedder.setClassLoader(getClass().getClassLoader()); + // else + // mavenEmbedder.setClassLoader(Thread.currentThread() + // .getContextClassLoader()); mavenEmbedder.start(); mavenEmbedder.setLocalRepositoryDirectory(new File( @@ -46,15 +85,54 @@ public class MavenManager { remoteRepository.getUrl(), remoteRepository.getId()); remoteRepositoriesInternal.add(repository); } - + MavenFile mavenFile = new MavenFile(); - mavenFile.setGroupId("org.argeo.slc.runtime"); - mavenFile.setArtifactId("org.argeo.slc.specs"); - mavenFile.setVersion("0.11.3-SNAPSHOT"); + mavenFile.setGroupId("org.argeo.slc.dist"); + mavenFile.setArtifactId("org.argeo.slc.sdk"); + mavenFile.setVersion("0.12.2-SNAPSHOT"); + mavenFile.setType("pom"); Artifact artifact = resolve(mavenFile); - log.debug("Dependencies of "+artifact); - for(Object obj : artifact.getDependencyTrail()){ - log.debug(" "+obj); + log.debug("Location of " + artifact + " : " + artifact.getFile()); + // log.debug("Dependencies of " + artifact); + // for (Object obj : artifact.getDependencyTrail()) { + // log.debug(" " + obj); + // } + + File pomFile = new File( + "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/pom.xml"); + MavenProject project = mavenEmbedder + .readProjectWithDependencies(pomFile); + // MavenProject project = mavenEmbedder + // .readProjectWithDependencies(artifact.getFile()); + + // EventMonitor eventMonitor = new EventMonitor() { + // + // public void startEvent(String eventName, String target, + // long timestamp) { + // log.debug(eventName + ", " + target + ", " + timestamp); + // } + // + // public void errorEvent(String eventName, String target, + // long timestamp, Throwable cause) { + // log.debug(eventName + ", " + target + ", " + timestamp); + // } + // + // public void endEvent(String eventName, String target, + // long timestamp) { + // log.debug(eventName + ", " + target + ", " + timestamp); + // } + // }; + // + // String[] goals = { "clean", "install" }; + // mavenEmbedder.execute(project, Arrays.asList(goals), + // eventMonitor, + // null, null, pomFile.getParentFile()); + + Set transitDeps = getTransitiveProjectDependencies( + project, remoteRepositoriesInternal, localRepository); + log.debug(transitDeps.size() + " dependencies for " + artifact); + for (Object obj : transitDeps) { + log.debug(" " + obj); } } catch (Exception e) { @@ -62,6 +140,38 @@ public class MavenManager { } } + @SuppressWarnings("unchecked") + public Set getTransitiveProjectDependencies(MavenProject project, + List remoteRepos, ArtifactRepository local) { + Embedder embedder = mavenEmbedder.getEmbedder(); + try { + ArtifactFactory artifactFactory = (ArtifactFactory) embedder + .lookup(ArtifactFactory.ROLE); + + ArtifactResolver artifactResolver = (ArtifactResolver) embedder + .lookup(ArtifactResolver.ROLE); + + ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) embedder + .lookup(ArtifactMetadataSource.ROLE); + + Set artifacts = project.createArtifacts(artifactFactory, + null, null); + + ArtifactResolutionResult arr = artifactResolver + .resolveTransitively(artifacts, project.getArtifact(), + local, remoteRepos, artifactMetadataSource, null); + + return arr.getArtifacts(); + } catch (Exception e) { + throw new SlcException("Cannot resolve dependency for " + project, + e); + } + // Order, just for display + // Set dependencies = new TreeSet(new ArtifactComparator()); + // dependencies.addAll(arr.getArtifacts()); + // return dependencies; + } + private Artifact resolve(MavenFile mavenDistribution) { try { Artifact artifact; @@ -112,5 +222,13 @@ public class MavenManager { public void setRemoteRepositories(List remoteRepositories) { this.remoteRepositories = remoteRepositories; } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + public void setOffline(Boolean offline) { + this.offline = offline; + } */ }