2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.repo
.maven
;
18 import java
.io
.ByteArrayOutputStream
;
20 import java
.util
.Comparator
;
21 import java
.util
.HashSet
;
22 import java
.util
.Properties
;
24 import java
.util
.TreeSet
;
26 import javax
.jcr
.Node
;
27 import javax
.jcr
.NodeIterator
;
28 import javax
.jcr
.Repository
;
29 import javax
.jcr
.Session
;
30 import javax
.jcr
.nodetype
.NodeType
;
32 import org
.apache
.commons
.io
.IOUtils
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
35 import org
.argeo
.jcr
.JcrUtils
;
36 import org
.argeo
.slc
.NameVersion
;
37 import org
.argeo
.slc
.SlcException
;
38 import org
.argeo
.slc
.aether
.AetherTemplate
;
39 import org
.argeo
.slc
.aether
.ArtifactIdComparator
;
40 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
41 import org
.argeo
.slc
.repo
.JarFileIndexer
;
42 import org
.argeo
.slc
.repo
.RepoConstants
;
43 import org
.argeo
.slc
.repo
.RepoUtils
;
44 import org
.sonatype
.aether
.artifact
.Artifact
;
45 import org
.sonatype
.aether
.graph
.DependencyNode
;
46 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
49 * Import all the dependencies listed in a POM and their dependency graphs to a
52 public class ImportMavenDependencies
implements Runnable
{
53 private final static Log log
= LogFactory
54 .getLog(ImportMavenDependencies
.class);
56 private AetherTemplate aetherTemplate
;
57 private String rootCoordinates
= "org.argeo.dep:versions-all:pom:1.2.0";
58 private String distCoordinates
= "org.argeo.tp:dist:pom:1.3.0";
59 private Set
<String
> excludedArtifacts
= new HashSet
<String
>();
61 private Repository repository
;
62 private String workspace
;
64 private String artifactBasePath
= RepoConstants
.ARTIFACTS_BASE_PATH
;
66 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
67 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
68 private Comparator
<Artifact
> artifactComparator
= new ArtifactIdComparator();
72 Set
<Artifact
> artifacts
= resolveDistribution();
78 void sync(Set
<Artifact
> artifacts
) {
79 Session session
= null;
81 session
= JcrUtils
.loginOrCreateWorkspace(repository
, workspace
);
83 NodeIterator nit
= session
.getNode(artifactBasePath
).getNodes();
84 while (nit
.hasNext()) {
85 Node node
= nit
.nextNode();
86 if (node
.isNodeType(NodeType
.NT_FOLDER
)
87 || node
.isNodeType(NodeType
.NT_UNSTRUCTURED
))
93 syncDistribution(session
, artifacts
);
94 } catch (Exception e
) {
95 throw new SlcException("Cannot import distribution", e
);
97 JcrUtils
.logoutQuietly(session
);
102 * Generate a POM with all the artifacts declared in root coordinates as
103 * dependencies AND in dependency management.
105 void createDistPom() {
107 Artifact pomArtifact
= new DefaultArtifact(rootCoordinates
);
109 Set
<Artifact
> registeredArtifacts
= new TreeSet
<Artifact
>(
111 MavenConventionsUtils
.gatherPomDependencies(aetherTemplate
,
112 registeredArtifacts
, pomArtifact
);
113 Artifact sdkArtifact
= new DefaultArtifact(distCoordinates
);
114 String sdkPom
= MavenConventionsUtils
.artifactsAsDependencyPom(
115 sdkArtifact
, registeredArtifacts
);
116 if (log
.isDebugEnabled())
117 log
.debug("Gathered " + registeredArtifacts
.size()
118 + " artifacts:\n" + sdkPom
);
119 } catch (Exception e
) {
120 throw new SlcException("Cannot resolve distribution", e
);
124 /** Returns all transitive dependencies of dist POM */
125 private Set
<Artifact
> resolveDistribution() {
127 Artifact distArtifact
= new DefaultArtifact(distCoordinates
);
128 Set
<Artifact
> artifacts
= new TreeSet
<Artifact
>(artifactComparator
);
130 DependencyNode node
= aetherTemplate
131 .resolveDependencies(distArtifact
);
132 addDependencies(artifacts
, node
, null);
134 if (log
.isDebugEnabled()) {
135 log
.debug("Resolved " + artifacts
.size() + " artifacts");
137 // Properties distributionDescriptor =
138 // generateDistributionDescriptor(artifacts);
139 // ByteArrayOutputStream out = new ByteArrayOutputStream();
140 // distributionDescriptor.store(out, "");
141 // log.debug(new String(out.toByteArray()));
146 * for (Artifact artifact : registeredArtifacts) { try { Boolean
147 * wasAdded = addArtifact(artifacts, artifact); if (wasAdded) {
148 * DependencyNode node = aetherTemplate
149 * .resolveDependencies(artifact); addDependencies(artifacts, node,
150 * null); } } catch (Exception e) {
151 * log.error("Could not resolve dependencies of " + artifact + ": "
152 * + e.getCause().getMessage()); }
156 * if (log.isDebugEnabled()) log.debug("Resolved " +
157 * artifacts.size() + " artifacts");
159 * // distribution descriptor // Properties distributionDescriptor =
160 * // generateDistributionDescriptor(artifacts); //
161 * ByteArrayOutputStream out = new ByteArrayOutputStream(); //
162 * distributionDescriptor.store(out, ""); // log.debug(new
163 * String(out.toByteArray())); // out.close();
166 } catch (Exception e
) {
167 throw new SlcException("Cannot resolve distribution", e
);
171 protected Properties
generateDistributionDescriptor(Set
<Artifact
> artifacts
) {
172 Properties distributionDescriptor
= new Properties();
173 for (Artifact artifact
: artifacts
) {
174 log
.debug(artifact
.getArtifactId() + " [" + artifact
.getVersion()
175 + "]\t(" + artifact
+ ")");
176 distributionDescriptor
.setProperty(artifact
.getArtifactId() + ":"
177 + artifact
.getVersion(), artifact
.toString());
179 return distributionDescriptor
;
182 /** Write artifacts to the target workspace, skipping excluded ones */
183 protected void syncDistribution(Session jcrSession
, Set
<Artifact
> artifacts
) {
184 Set
<Artifact
> artifactsWithoutSources
= new TreeSet
<Artifact
>(
186 Long begin
= System
.currentTimeMillis();
188 JcrUtils
.mkfolders(jcrSession
, artifactBasePath
);
189 artifacts
: for (Artifact artifact
: artifacts
) {
191 if (excludedArtifacts
.contains(artifact
.getGroupId() + ":"
192 + artifact
.getArtifactId())) {
193 if (log
.isDebugEnabled())
194 log
.debug("Exclude " + artifact
);
198 File jarFile
= MavenConventionsUtils
.artifactToFile(artifact
);
199 if (!jarFile
.exists()) {
200 log
.warn("Generated file " + jarFile
+ " for " + artifact
201 + " does not exist");
204 artifact
.setFile(jarFile
);
207 String parentPath
= MavenConventionsUtils
208 .artifactParentPath(artifactBasePath
, artifact
);
210 if (!jcrSession
.itemExists(parentPath
))
211 parentNode
= JcrUtils
.mkfolders(jcrSession
, parentPath
);
213 parentNode
= jcrSession
.getNode(parentPath
);
216 if (!parentNode
.hasNode(jarFile
.getName())) {
217 fileNode
= createFileNode(parentNode
, jarFile
);
219 fileNode
= parentNode
.getNode(jarFile
.getName());
222 if (artifactIndexer
.support(fileNode
.getPath()))
223 artifactIndexer
.index(fileNode
);
224 if (jarFileIndexer
.support(fileNode
.getPath()))
225 jarFileIndexer
.index(fileNode
);
228 addPdeSource(jcrSession
, artifact
, jarFile
,
229 artifactsWithoutSources
);
232 if (log
.isDebugEnabled())
233 log
.debug("Synchronized " + fileNode
);
234 } catch (Exception e
) {
235 log
.error("Could not synchronize " + artifact
, e
);
236 jcrSession
.refresh(false);
241 Long duration
= (System
.currentTimeMillis() - begin
) / 1000;
242 if (log
.isDebugEnabled()) {
243 log
.debug("Synchronized distribution in " + duration
+ "s");
244 log
.debug("The following artifacts have no sources:");
245 for (Artifact artifact
: artifactsWithoutSources
) {
249 } catch (Exception e
) {
250 throw new SlcException("Cannot synchronize distribution", e
);
254 /** Try to add PDE sources */
255 private void addPdeSource(Session session
, Artifact artifact
,
256 File artifactFile
, Set
<Artifact
> artifactsWithoutSources
) {
257 ByteArrayOutputStream out
= new ByteArrayOutputStream();
259 File origSourceFile
= null;
260 Artifact origSourceArtifact
= new DefaultArtifact(
261 artifact
.getGroupId(), artifact
.getArtifactId(), "sources",
262 artifact
.getExtension(), artifact
.getVersion());
263 Artifact newSourceArtifact
= new DefaultArtifact(
264 artifact
.getGroupId(),
265 artifact
.getArtifactId() + ".source",
266 artifact
.getExtension(), artifact
.getVersion());
268 origSourceFile
= aetherTemplate
269 .getResolvedFile(origSourceArtifact
);
270 } catch (Exception e
) {
271 // also try artifact following the conventions
272 origSourceArtifact
= newSourceArtifact
;
273 origSourceFile
= aetherTemplate
274 .getResolvedFile(origSourceArtifact
);
277 String newSourceParentPath
= MavenConventionsUtils
278 .artifactParentPath(artifactBasePath
, newSourceArtifact
);
279 Node newSourceParentNode
= JcrUtils
.mkfolders(session
,
280 newSourceParentPath
);
281 NameVersion bundleNameVersion
= RepoUtils
282 .readNameVersion(artifactFile
);
283 RepoUtils
.packagesAsPdeSource(origSourceFile
, bundleNameVersion
,
285 String newSourceFileName
= MavenConventionsUtils
286 .artifactFileName(newSourceArtifact
);
287 JcrUtils
.copyBytesAsFile(newSourceParentNode
, newSourceFileName
,
289 } catch (Exception e
) {
290 log
.error("Cannot add PDE source for " + artifact
+ ": " + e
);
291 artifactsWithoutSources
.add(artifact
);
293 IOUtils
.closeQuietly(out
);
297 private Node
createFileNode(Node parentNode
, File file
) {
298 return JcrUtils
.copyFile(parentNode
, file
);
301 /** Recursively adds non optional dependencies */
302 private void addDependencies(Set
<Artifact
> artifacts
, DependencyNode node
,
304 // if (artifacts.contains(node.getDependency().getArtifact()))
306 String currentArtifactId
= node
.getDependency().getArtifact()
308 if (log
.isDebugEnabled()) {
309 log
.debug("# Add dependency for " + currentArtifactId
);
310 if (ancestors
!= null)
311 log
.debug(ancestors
);
313 for (DependencyNode child
: node
.getChildren()) {
314 if (!child
.getDependency().isOptional()) {
315 if (willAdd(child
.getDependency().getArtifact())) {
316 addArtifact(artifacts
, child
.getDependency().getArtifact());
317 addDependencies(artifacts
, child
, currentArtifactId
+ "\n"
318 + (ancestors
!= null ? ancestors
: ""));
324 /** @return whether it was added */
325 private Boolean
addArtifact(Set
<Artifact
> artifacts
, Artifact artifact
) {
326 Boolean willAdd
= willAdd(artifact
);
328 artifacts
.add(artifact
);
330 log
.info("Skip " + artifact
);
334 private Boolean
willAdd(Artifact artifact
) {
335 Boolean willAdd
= true;
336 if (excludedArtifacts
.contains(artifact
.getGroupId() + ":"
337 + artifact
.getArtifactId()))
339 else if (excludedArtifacts
.contains(artifact
.getGroupId() + ":*"))
344 public void setAetherTemplate(AetherTemplate aetherTemplate
) {
345 this.aetherTemplate
= aetherTemplate
;
348 public void setExcludedArtifacts(Set
<String
> excludedArtifactIds
) {
349 this.excludedArtifacts
= excludedArtifactIds
;
352 public void setRootCoordinates(String rootCoordinates
) {
353 this.rootCoordinates
= rootCoordinates
;
356 public void setRepository(Repository repository
) {
357 this.repository
= repository
;
360 public void setWorkspace(String workspace
) {
361 this.workspace
= workspace
;
364 public void setDistCoordinates(String distCoordinates
) {
365 this.distCoordinates
= distCoordinates
;
368 public void setArtifactBasePath(String artifactBasePath
) {
369 this.artifactBasePath
= artifactBasePath
;