]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.maven/src/main/java/org/argeo/slc/maven/MavenManager.java
fe23082f649c1b2d826e07dfcb972bf20d74d107
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.maven
;
20 public class MavenManager
{
22 // public static void main(String[] args){
23 // //CustomCli mavenCli = new CustomCli();
24 // MavenCli mavenCli = new MavenCli();
25 // String[] goals = { "-e","dependency:tree" };
26 // mavenCli.doMain(goals, "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk", System.out, System.err);
29 // static class CustomCli extends MavenCli{
30 // private PlexusContainer container;
33 // protected void customizeContainer(PlexusContainer container) {
34 // this.container = container;
37 // public PlexusContainer getContainer() {
45 private final Log log = LogFactory.getLog(getClass());
47 private String localRepositoryPath = System.getProperty("user.home")
48 + File.separator + ".m2" + File.separator + "repository";
50 private ArtifactRepository localRepository;
51 private List<ArtifactRepository> remoteRepositoriesInternal;
52 private List<RemoteRepository> remoteRepositories = new Vector<RemoteRepository>();
54 private MavenEmbedder mavenEmbedder;
55 private ClassLoader classLoader;
56 private Boolean offline = false;
60 mavenEmbedder = new SlcMavenEmbedder();
61 mavenEmbedder.setOffline(offline);
62 // mavenEmbedder.setAlignWithUserInstallation(true);
63 if (classLoader != null)
64 mavenEmbedder.setClassLoader(classLoader);
66 mavenEmbedder.setClassLoader(getClass().getClassLoader());
68 // mavenEmbedder.setClassLoader(Thread.currentThread()
69 // .getContextClassLoader());
70 mavenEmbedder.start();
72 mavenEmbedder.setLocalRepositoryDirectory(new File(
73 localRepositoryPath));
75 localRepository = mavenEmbedder.getLocalRepository();
77 remoteRepositoriesInternal = new Vector<ArtifactRepository>();
78 for (RemoteRepository remoteRepository : remoteRepositories) {
80 ArtifactRepository repository = mavenEmbedder.createRepository(
81 remoteRepository.getUrl(), remoteRepository.getId());
82 remoteRepositoriesInternal.add(repository);
85 MavenFile mavenFile = new MavenFile();
86 mavenFile.setGroupId("org.argeo.slc.dist");
87 mavenFile.setArtifactId("org.argeo.slc.sdk");
88 mavenFile.setVersion("0.12.2-SNAPSHOT");
89 mavenFile.setType("pom");
90 Artifact artifact = resolve(mavenFile);
91 log.debug("Location of " + artifact + " : " + artifact.getFile());
92 // log.debug("Dependencies of " + artifact);
93 // for (Object obj : artifact.getDependencyTrail()) {
94 // log.debug(" " + obj);
97 File pomFile = new File(
98 "/home/mbaudier/dev/src/slc/dist/org.argeo.slc.sdk/pom.xml");
99 MavenProject project = mavenEmbedder
100 .readProjectWithDependencies(pomFile);
101 // MavenProject project = mavenEmbedder
102 // .readProjectWithDependencies(artifact.getFile());
104 // EventMonitor eventMonitor = new EventMonitor() {
106 // public void startEvent(String eventName, String target,
108 // log.debug(eventName + ", " + target + ", " + timestamp);
111 // public void errorEvent(String eventName, String target,
112 // long timestamp, Throwable cause) {
113 // log.debug(eventName + ", " + target + ", " + timestamp);
116 // public void endEvent(String eventName, String target,
118 // log.debug(eventName + ", " + target + ", " + timestamp);
122 // String[] goals = { "clean", "install" };
123 // mavenEmbedder.execute(project, Arrays.asList(goals),
125 // null, null, pomFile.getParentFile());
127 Set<Artifact> transitDeps = getTransitiveProjectDependencies(
128 project, remoteRepositoriesInternal, localRepository);
129 log.debug(transitDeps.size() + " dependencies for " + artifact);
130 for (Object obj : transitDeps) {
131 log.debug(" " + obj);
134 } catch (Exception e) {
135 throw new SlcException("Cannot initialize Maven manager", e);
139 @SuppressWarnings("unchecked")
140 public Set<Artifact> getTransitiveProjectDependencies(MavenProject project,
141 List<ArtifactRepository> remoteRepos, ArtifactRepository local) {
142 Embedder embedder = mavenEmbedder.getEmbedder();
144 ArtifactFactory artifactFactory = (ArtifactFactory) embedder
145 .lookup(ArtifactFactory.ROLE);
147 ArtifactResolver artifactResolver = (ArtifactResolver) embedder
148 .lookup(ArtifactResolver.ROLE);
150 ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) embedder
151 .lookup(ArtifactMetadataSource.ROLE);
153 Set<Artifact> artifacts = project.createArtifacts(artifactFactory,
156 ArtifactResolutionResult arr = artifactResolver
157 .resolveTransitively(artifacts, project.getArtifact(),
158 local, remoteRepos, artifactMetadataSource, null);
160 return arr.getArtifacts();
161 } catch (Exception e) {
162 throw new SlcException("Cannot resolve dependency for " + project,
165 // Order, just for display
166 // Set dependencies = new TreeSet(new ArtifactComparator());
167 // dependencies.addAll(arr.getArtifacts());
168 // return dependencies;
171 private Artifact resolve(MavenFile mavenDistribution) {
174 if (mavenDistribution.getClassifier() == null) {
175 artifact = mavenEmbedder.createArtifact(mavenDistribution
176 .getGroupId(), mavenDistribution.getArtifactId(),
177 mavenDistribution.getVersion(),
178 Artifact.SCOPE_PROVIDED, mavenDistribution.getType());
180 artifact = mavenEmbedder.createArtifactWithClassifier(
181 mavenDistribution.getGroupId(), mavenDistribution
182 .getArtifactId(), mavenDistribution
183 .getVersion(), mavenDistribution.getType(),
184 mavenDistribution.getClassifier());
187 mavenEmbedder.resolve(artifact, remoteRepositoriesInternal,
191 } catch (Exception e) {
192 throw new SlcException("Cannot resolve artifact.", e);
196 public File getPackageLocation(MavenFile mavenDistribution) {
197 return resolve(mavenDistribution).getFile();
200 public void destroy() {
202 if (mavenEmbedder != null) {
203 mavenEmbedder.stop();
205 } catch (MavenEmbedderException e) {
206 log.error("Cannot destroy Maven manager", e);
210 public void setLocalRepositoryPath(String localRepositoryPath) {
211 this.localRepositoryPath = localRepositoryPath;
214 public List<RemoteRepository> getRemoteRepositories() {
215 return remoteRepositories;
218 public void setRemoteRepositories(List<RemoteRepository> remoteRepositories) {
219 this.remoteRepositories = remoteRepositories;
222 public void setClassLoader(ClassLoader classLoader) {
223 this.classLoader = classLoader;
226 public void setOffline(Boolean offline) {
227 this.offline = offline;