2 * Copyright (C) 2007-2012 Argeo GmbH
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 String parentPomCoordinates
= "org.argeo:parent:1.2.0";
60 private Set
<String
> excludedArtifacts
= new HashSet
<String
>();
62 private Repository repository
;
63 private String workspace
;
65 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
67 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
68 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
69 private Comparator
<Artifact
> artifactComparator
= new ArtifactIdComparator();
73 Set
<Artifact
> artifacts
= resolveDistribution();
79 void sync(Set
<Artifact
> artifacts
) {
80 Session session
= null;
82 session
= JcrUtils
.loginOrCreateWorkspace(repository
, workspace
);
84 NodeIterator nit
= session
.getNode(artifactBasePath
).getNodes();
85 while (nit
.hasNext()) {
86 Node node
= nit
.nextNode();
87 if (node
.isNodeType(NodeType
.NT_FOLDER
)
88 || node
.isNodeType(NodeType
.NT_UNSTRUCTURED
))
94 syncDistribution(session
, artifacts
);
95 } catch (Exception e
) {
96 throw new SlcException("Cannot import distribution", e
);
98 JcrUtils
.logoutQuietly(session
);
103 * Generate a POM with all the artifacts declared in root coordinates as
104 * dependencies AND in dependency management.
106 void createDistPom() {
108 Artifact pomArtifact
= new DefaultArtifact(rootCoordinates
);
110 Set
<Artifact
> registeredArtifacts
= new TreeSet
<Artifact
>(
112 MavenConventionsUtils
.gatherPomDependencies(aetherTemplate
,
113 registeredArtifacts
, pomArtifact
);
114 Artifact sdkArtifact
= new DefaultArtifact(distCoordinates
);
115 String sdkPom
= MavenConventionsUtils
.artifactsAsDependencyPom(
116 sdkArtifact
, registeredArtifacts
, new DefaultArtifact(
117 parentPomCoordinates
));
118 if (log
.isDebugEnabled())
119 log
.debug("Gathered " + registeredArtifacts
.size()
120 + " artifacts:\n" + sdkPom
);
121 } catch (Exception e
) {
122 throw new SlcException("Cannot resolve distribution", e
);
126 /** Returns all transitive dependencies of dist POM */
127 private Set
<Artifact
> resolveDistribution() {
129 Artifact distArtifact
= new DefaultArtifact(distCoordinates
);
130 Set
<Artifact
> artifacts
= new TreeSet
<Artifact
>(artifactComparator
);
132 DependencyNode node
= aetherTemplate
133 .resolveDependencies(distArtifact
);
134 addDependencies(artifacts
, node
, null);
136 if (log
.isDebugEnabled()) {
137 log
.debug("Resolved " + artifacts
.size() + " artifacts");
139 // Properties distributionDescriptor =
140 // generateDistributionDescriptor(artifacts);
141 // ByteArrayOutputStream out = new ByteArrayOutputStream();
142 // distributionDescriptor.store(out, "");
143 // log.debug(new String(out.toByteArray()));
148 * for (Artifact artifact : registeredArtifacts) { try { Boolean
149 * wasAdded = addArtifact(artifacts, artifact); if (wasAdded) {
150 * DependencyNode node = aetherTemplate
151 * .resolveDependencies(artifact); addDependencies(artifacts, node,
152 * null); } } catch (Exception e) {
153 * log.error("Could not resolve dependencies of " + artifact + ": "
154 * + e.getCause().getMessage()); }
158 * if (log.isDebugEnabled()) log.debug("Resolved " +
159 * artifacts.size() + " artifacts");
161 * // distribution descriptor // Properties distributionDescriptor =
162 * // generateDistributionDescriptor(artifacts); //
163 * ByteArrayOutputStream out = new ByteArrayOutputStream(); //
164 * distributionDescriptor.store(out, ""); // log.debug(new
165 * String(out.toByteArray())); // out.close();
168 } catch (Exception e
) {
169 throw new SlcException("Cannot resolve distribution", e
);
173 protected Properties
generateDistributionDescriptor(Set
<Artifact
> artifacts
) {
174 Properties distributionDescriptor
= new Properties();
175 for (Artifact artifact
: artifacts
) {
176 log
.debug(artifact
.getArtifactId() + " [" + artifact
.getVersion()
177 + "]\t(" + artifact
+ ")");
178 distributionDescriptor
.setProperty(artifact
.getArtifactId() + ":"
179 + artifact
.getVersion(), artifact
.toString());
181 return distributionDescriptor
;
184 /** Write artifacts to the target workspace, skipping excluded ones */
185 protected void syncDistribution(Session jcrSession
, Set
<Artifact
> artifacts
) {
186 Set
<Artifact
> artifactsWithoutSources
= new TreeSet
<Artifact
>(
188 Long begin
= System
.currentTimeMillis();
190 JcrUtils
.mkfolders(jcrSession
, artifactBasePath
);
191 artifacts
: for (Artifact artifact
: artifacts
) {
193 if (excludedArtifacts
.contains(artifact
.getGroupId() + ":"
194 + artifact
.getArtifactId())) {
195 if (log
.isDebugEnabled())
196 log
.debug("Exclude " + artifact
);
200 File jarFile
= MavenConventionsUtils
.artifactToFile(artifact
);
201 if (!jarFile
.exists()) {
202 log
.warn("Generated file " + jarFile
+ " for " + artifact
203 + " does not exist");
206 artifact
.setFile(jarFile
);
209 String parentPath
= MavenConventionsUtils
210 .artifactParentPath(artifactBasePath
, artifact
);
212 if (!jcrSession
.itemExists(parentPath
))
213 parentNode
= JcrUtils
.mkfolders(jcrSession
, parentPath
);
215 parentNode
= jcrSession
.getNode(parentPath
);
218 if (!parentNode
.hasNode(jarFile
.getName())) {
219 fileNode
= createFileNode(parentNode
, jarFile
);
221 fileNode
= parentNode
.getNode(jarFile
.getName());
224 if (artifactIndexer
.support(fileNode
.getPath()))
225 artifactIndexer
.index(fileNode
);
226 if (jarFileIndexer
.support(fileNode
.getPath()))
227 jarFileIndexer
.index(fileNode
);
230 addPdeSource(jcrSession
, artifact
, jarFile
,
231 artifactsWithoutSources
);
234 if (log
.isDebugEnabled())
235 log
.debug("Synchronized " + fileNode
);
236 } catch (Exception e
) {
237 log
.error("Could not synchronize " + artifact
, e
);
238 jcrSession
.refresh(false);
243 Long duration
= (System
.currentTimeMillis() - begin
) / 1000;
244 if (log
.isDebugEnabled()) {
245 log
.debug("Synchronized distribution in " + duration
+ "s");
246 log
.debug("The following artifacts have no sources:");
247 for (Artifact artifact
: artifactsWithoutSources
) {
251 } catch (Exception e
) {
252 throw new SlcException("Cannot synchronize distribution", e
);
256 /** Try to add PDE sources */
257 private void addPdeSource(Session session
, Artifact artifact
,
258 File artifactFile
, Set
<Artifact
> artifactsWithoutSources
) {
259 ByteArrayOutputStream out
= new ByteArrayOutputStream();
261 File origSourceFile
= null;
262 Artifact origSourceArtifact
= new DefaultArtifact(
263 artifact
.getGroupId(), artifact
.getArtifactId(), "sources",
264 artifact
.getExtension(), artifact
.getVersion());
265 Artifact newSourceArtifact
= new DefaultArtifact(
266 artifact
.getGroupId(),
267 artifact
.getArtifactId() + ".source",
268 artifact
.getExtension(), artifact
.getVersion());
270 origSourceFile
= aetherTemplate
271 .getResolvedFile(origSourceArtifact
);
272 } catch (Exception e
) {
273 // also try artifact following the conventions
274 origSourceArtifact
= newSourceArtifact
;
275 origSourceFile
= aetherTemplate
276 .getResolvedFile(origSourceArtifact
);
279 String newSourceParentPath
= MavenConventionsUtils
280 .artifactParentPath(artifactBasePath
, newSourceArtifact
);
281 Node newSourceParentNode
= JcrUtils
.mkfolders(session
,
282 newSourceParentPath
);
283 NameVersion bundleNameVersion
= RepoUtils
284 .readNameVersion(artifactFile
);
285 RepoUtils
.packagesAsPdeSource(origSourceFile
, bundleNameVersion
,
287 String newSourceFileName
= MavenConventionsUtils
288 .artifactFileName(newSourceArtifact
);
289 JcrUtils
.copyBytesAsFile(newSourceParentNode
, newSourceFileName
,
291 } catch (Exception e
) {
292 log
.error("Cannot add PDE source for " + artifact
+ ": " + e
);
293 artifactsWithoutSources
.add(artifact
);
295 IOUtils
.closeQuietly(out
);
299 private Node
createFileNode(Node parentNode
, File file
) {
300 return JcrUtils
.copyFile(parentNode
, file
);
303 /** Recursively adds non optional dependencies */
304 private void addDependencies(Set
<Artifact
> artifacts
, DependencyNode node
,
306 // if (artifacts.contains(node.getDependency().getArtifact()))
308 String currentArtifactId
= node
.getDependency().getArtifact()
310 if (log
.isDebugEnabled()) {
311 log
.debug("# Add dependency for " + currentArtifactId
);
312 if (ancestors
!= null)
313 log
.debug(ancestors
);
315 for (DependencyNode child
: node
.getChildren()) {
316 if (!child
.getDependency().isOptional()) {
317 if (willAdd(child
.getDependency().getArtifact())) {
318 addArtifact(artifacts
, child
.getDependency().getArtifact());
319 addDependencies(artifacts
, child
, currentArtifactId
+ "\n"
320 + (ancestors
!= null ? ancestors
: ""));
326 /** @return whether it was added */
327 private Boolean
addArtifact(Set
<Artifact
> artifacts
, Artifact artifact
) {
328 Boolean willAdd
= willAdd(artifact
);
330 artifacts
.add(artifact
);
332 log
.info("Skip " + artifact
);
336 private Boolean
willAdd(Artifact artifact
) {
337 Boolean willAdd
= true;
338 if (excludedArtifacts
.contains(artifact
.getGroupId() + ":"
339 + artifact
.getArtifactId()))
341 else if (excludedArtifacts
.contains(artifact
.getGroupId() + ":*"))
346 public void setAetherTemplate(AetherTemplate aetherTemplate
) {
347 this.aetherTemplate
= aetherTemplate
;
350 public void setExcludedArtifacts(Set
<String
> excludedArtifactIds
) {
351 this.excludedArtifacts
= excludedArtifactIds
;
354 public void setRootCoordinates(String rootCoordinates
) {
355 this.rootCoordinates
= rootCoordinates
;
358 public void setRepository(Repository repository
) {
359 this.repository
= repository
;
362 public void setWorkspace(String workspace
) {
363 this.workspace
= workspace
;
366 public void setDistCoordinates(String distCoordinates
) {
367 this.distCoordinates
= distCoordinates
;
370 public void setArtifactBasePath(String artifactBasePath
) {
371 this.artifactBasePath
= artifactBasePath
;