1 package org.apache.archiva.consumers.dependencytree;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.lang.reflect.Field;
26 import java.net.MalformedURLException;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
35 import org.apache.maven.archiva.consumers.ConsumerException;
36 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
37 import org.apache.maven.artifact.Artifact;
38 import org.apache.maven.artifact.factory.ArtifactFactory;
39 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
40 import org.apache.maven.artifact.repository.ArtifactRepository;
41 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
42 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
43 import org.apache.maven.artifact.resolver.ArtifactCollector;
44 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
45 import org.apache.maven.project.DefaultMavenProjectBuilder;
46 import org.apache.maven.project.MavenProject;
47 import org.apache.maven.project.MavenProjectBuilder;
48 import org.apache.maven.project.ProjectBuildingException;
49 import org.apache.maven.shared.dependency.tree.DependencyNode;
50 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
51 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
52 import org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter;
53 import org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter;
54 import org.apache.maven.shared.dependency.tree.filter.StateDependencyNodeFilter;
55 import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeVisitor;
56 import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
57 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
58 import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor;
59 import org.dom4j.Document;
60 import org.dom4j.DocumentHelper;
61 import org.dom4j.Element;
62 import org.dom4j.io.OutputFormat;
63 import org.dom4j.io.XMLWriter;
66 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
67 * role-hint="dependency-tree-generator" instantiation-strategy="per-lookup"
69 public class DependencyTreeGeneratorConsumer
70 extends AbstractMonitoredConsumer
71 implements KnownRepositoryContentConsumer
73 /** @plexus.configuration */
74 private File generatedRepositoryLocation;
76 /** @plexus.configuration */
77 private File localRepository;
79 /** @plexus.requirement */
80 private DependencyTreeBuilder dependencyTreeBuilder;
82 /** @plexus.requirement */
83 private ArtifactFactory artifactFactory;
85 /** @plexus.requirement role-hint="maven" */
86 private ArtifactMetadataSource artifactMetadataSource;
88 /** @plexus.requirement */
89 private ArtifactCollector artifactCollector;
91 /** @plexus.requirement */
92 private MavenProjectBuilder projectBuilder;
94 /** @plexus.requirement */
95 private ArtifactRepositoryFactory artifactRepositoryFactory;
97 private String repositoryLocation;
99 private final DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
101 private ArtifactRepository localArtifactRepository;
103 private Field rawProjectCacheField;
105 private Field processedProjectCacheField;
107 private List<String> includes = Collections.singletonList( "**/*.pom" );
109 public String getDescription()
111 return "Generate dependency tree metadata for tracking changes across algorithms";
114 public String getId()
116 return "dependency-tree-generator";
119 public boolean isPermanent()
124 public void setGeneratedRepositoryLocation( File generatedRepositoryLocation )
126 this.generatedRepositoryLocation = generatedRepositoryLocation;
129 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
130 throws ConsumerException
132 repositoryLocation = repository.getLocation();
134 if ( generatedRepositoryLocation == null )
136 generatedRepositoryLocation = new File( repositoryLocation );
139 if ( localRepository == null )
141 // This is a bit crappy, it would be better to operate entirely within
142 // the base repository, but would need to adjust maven-artifact
143 localRepository = new File( System.getProperty( "user.home" ), ".m2/repository" );
146 localArtifactRepository =
147 artifactRepositoryFactory.createArtifactRepository( "local",
148 localRepository.toURL().toExternalForm(),
149 layout, null, null );
151 catch ( MalformedURLException e )
153 throw new ConsumerException( e.getMessage(), e );
158 public void completeScan()
162 public List getExcludes()
167 public List getIncludes()
172 public void processFile( String path )
173 throws ConsumerException
175 MavenProject project;
178 project = projectBuilder.build( new File( repositoryLocation, path ), localArtifactRepository, null, false );
180 // manually flush out the cache for memory concerns and more accurate building
181 flushProjectCache( projectBuilder );
183 catch ( ProjectBuildingException e )
185 throw new ConsumerException( e.getMessage(), e );
188 DependencyNode rootNode;
191 // TODO: do this for different values of new ScopeArtifactFilter( scope )
192 ArtifactFilter artifactFilter = null;
195 dependencyTreeBuilder.buildDependencyTree( project, localArtifactRepository, artifactFactory,
196 artifactMetadataSource, artifactFilter, artifactCollector );
198 catch ( DependencyTreeBuilderException e )
200 throw new ConsumerException( e.getMessage(), e );
203 Document document = DocumentHelper.createDocument();
204 DependencyNodeVisitor visitor = new XmlSerializingDependencyNodeVisitor( document );
206 // TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
207 visitor = new BuildingDependencyNodeVisitor( visitor );
209 CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
210 DependencyNodeVisitor firstPassVisitor =
211 new FilteringDependencyNodeVisitor( collectingVisitor, StateDependencyNodeFilter.INCLUDED );
212 rootNode.accept( firstPassVisitor );
214 DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
215 visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
217 rootNode.accept( visitor );
219 FileWriter writer = null;
223 artifactFactory.createProjectArtifact( project.getGroupId(), project.getArtifactId(),
224 project.getVersion() );
226 String p = layout.pathOf( artifact );
227 if ( !p.equals( path ) )
229 throw new ConsumerException( "Bad path: " + p + "; should be: " + path );
232 File generatedFile = new File( generatedRepositoryLocation, p + ".xml" );
233 generatedFile.getParentFile().mkdirs();
234 writer = new FileWriter( generatedFile );
235 OutputFormat format = OutputFormat.createPrettyPrint();
236 XMLWriter w = new XMLWriter( writer, format );
239 catch ( IOException e )
241 throw new ConsumerException( e.getMessage(), e );
245 IOUtils.closeQuietly( writer );
249 private void flushProjectCache( MavenProjectBuilder projectBuilder )
253 if ( rawProjectCacheField == null )
255 rawProjectCacheField = DefaultMavenProjectBuilder.class.getDeclaredField( "rawProjectCache" );
256 rawProjectCacheField.setAccessible( true );
259 if ( processedProjectCacheField == null )
261 processedProjectCacheField =
262 DefaultMavenProjectBuilder.class.getDeclaredField( "processedProjectCache" );
263 processedProjectCacheField.setAccessible( true );
266 rawProjectCacheField.set( projectBuilder, new HashMap() );
268 processedProjectCacheField.set( projectBuilder, new HashMap() );
270 catch ( Exception e )
272 throw new RuntimeException( e );
276 private static class XmlSerializingDependencyNodeVisitor
277 implements DependencyNodeVisitor
279 private Element xmlNode;
281 public XmlSerializingDependencyNodeVisitor( Document document )
283 xmlNode = document.addElement( "tree" );
286 // DependencyNodeVisitor methods ------------------------------------------
289 * @see org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor#visit(org.apache.maven.shared.dependency.tree.DependencyNode)
291 public boolean visit( DependencyNode node )
293 Element dependency = xmlNode.addElement( "dependency" );
295 Artifact artifact = node.getArtifact();
296 dependency.addElement( "groupId" ).setText( artifact.getGroupId() );
297 dependency.addElement( "artifactId" ).setText( artifact.getArtifactId() );
298 dependency.addElement( "type" ).setText( artifact.getType() );
299 dependency.addElement( "version" ).setText( artifact.getVersion() );
300 if ( artifact.getScope() != null )
302 dependency.addElement( "scope" ).setText( artifact.getScope() );
304 if ( artifact.getClassifier() != null )
306 dependency.addElement( "classifier" ).setText( artifact.getClassifier() );
309 xmlNode = dependency.addElement( "dependencies" );
315 * @see org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor#endVisit(org.apache.maven.shared.dependency.tree.DependencyNode)
317 public boolean endVisit( DependencyNode node )
319 Element e = xmlNode.getParent();
321 if ( !xmlNode.hasContent() )
326 xmlNode = e.getParent();
332 public void setIncludes( List<String> includes )
334 this.includes = includes;