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.HashMap;
29 import java.util.List;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
34 import org.apache.maven.archiva.consumers.ConsumerException;
35 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.factory.ArtifactFactory;
38 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
39 import org.apache.maven.artifact.repository.ArtifactRepository;
40 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
41 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
42 import org.apache.maven.artifact.resolver.ArtifactCollector;
43 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
44 import org.apache.maven.project.DefaultMavenProjectBuilder;
45 import org.apache.maven.project.MavenProject;
46 import org.apache.maven.project.MavenProjectBuilder;
47 import org.apache.maven.project.ProjectBuildingException;
48 import org.apache.maven.shared.dependency.tree.DependencyNode;
49 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
50 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
51 import org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter;
52 import org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter;
53 import org.apache.maven.shared.dependency.tree.filter.StateDependencyNodeFilter;
54 import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeVisitor;
55 import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
56 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
57 import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor;
58 import org.dom4j.Document;
59 import org.dom4j.DocumentHelper;
60 import org.dom4j.Element;
61 import org.dom4j.io.OutputFormat;
62 import org.dom4j.io.XMLWriter;
65 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
66 * role-hint="dependency-tree-generator" instantiation-strategy="per-lookup"
68 public class DependencyTreeGeneratorConsumer
69 extends AbstractMonitoredConsumer
70 implements KnownRepositoryContentConsumer
72 /** @plexus.configuration */
73 private File generatedRepositoryLocation;
75 /** @plexus.configuration */
76 private File localRepository;
78 /** @plexus.requirement */
79 private DependencyTreeBuilder dependencyTreeBuilder;
81 /** @plexus.requirement */
82 private ArtifactFactory artifactFactory;
84 /** @plexus.requirement role-hint="maven" */
85 private ArtifactMetadataSource artifactMetadataSource;
87 /** @plexus.requirement */
88 private ArtifactCollector artifactCollector;
90 /** @plexus.requirement */
91 private MavenProjectBuilder projectBuilder;
93 /** @plexus.requirement */
94 private ArtifactRepositoryFactory artifactRepositoryFactory;
96 private String repositoryLocation;
98 private final DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
100 private ArtifactRepository localArtifactRepository;
102 private Field rawProjectCacheField;
104 private Field processedProjectCacheField;
106 public String getDescription()
108 return "Generate dependency tree metadata for tracking changes across algorithms";
111 public String getId()
113 return "dependency-tree-generator";
116 public boolean isPermanent()
121 public void setGeneratedRepositoryLocation( File generatedRepositoryLocation )
123 this.generatedRepositoryLocation = generatedRepositoryLocation;
126 public void beginScan( ManagedRepositoryConfiguration repository )
127 throws ConsumerException
129 repositoryLocation = repository.getLocation();
131 if ( generatedRepositoryLocation == null )
133 generatedRepositoryLocation = new File( repositoryLocation );
136 if ( localRepository == null )
138 // This is a bit crappy, it would be better to operate entirely within
139 // the base repository, but would need to adjust maven-artifact
140 localRepository = new File( System.getProperty( "user.home" ), ".m2/repository" );
143 localArtifactRepository =
144 artifactRepositoryFactory.createArtifactRepository( "local",
145 localRepository.toURL().toExternalForm(),
146 layout, null, null );
148 catch ( MalformedURLException e )
150 throw new ConsumerException( e.getMessage(), e );
155 public void completeScan()
159 public List getExcludes()
164 public List getIncludes()
166 return Collections.singletonList( "**/*.pom" );
169 public void processFile( String path )
170 throws ConsumerException
172 MavenProject project;
175 project = projectBuilder.build( new File( repositoryLocation, path ), localArtifactRepository, null, false );
177 // manually flush out the cache for memory concerns and more accurate building
178 flushProjectCache( projectBuilder );
180 catch ( ProjectBuildingException e )
182 throw new ConsumerException( e.getMessage(), e );
185 DependencyNode rootNode;
188 // TODO: do this for different values of new ScopeArtifactFilter( scope )
189 ArtifactFilter artifactFilter = null;
192 dependencyTreeBuilder.buildDependencyTree( project, localArtifactRepository, artifactFactory,
193 artifactMetadataSource, artifactFilter, artifactCollector );
195 catch ( DependencyTreeBuilderException e )
197 throw new ConsumerException( e.getMessage(), e );
200 Document document = DocumentHelper.createDocument();
201 DependencyNodeVisitor visitor = new XmlSerializingDependencyNodeVisitor( document );
203 // TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
204 visitor = new BuildingDependencyNodeVisitor( visitor );
206 CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
207 DependencyNodeVisitor firstPassVisitor =
208 new FilteringDependencyNodeVisitor( collectingVisitor, StateDependencyNodeFilter.INCLUDED );
209 rootNode.accept( firstPassVisitor );
211 DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
212 visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
214 rootNode.accept( visitor );
216 FileWriter writer = null;
220 artifactFactory.createProjectArtifact( project.getGroupId(), project.getArtifactId(),
221 project.getVersion() );
223 File generatedFile = new File( generatedRepositoryLocation, layout.pathOf( artifact ) + ".xml" );
224 generatedFile.getParentFile().mkdirs();
225 writer = new FileWriter( generatedFile );
226 OutputFormat format = OutputFormat.createPrettyPrint();
227 XMLWriter w = new XMLWriter( writer, format );
230 catch ( IOException e )
232 throw new ConsumerException( e.getMessage(), e );
236 IOUtils.closeQuietly( writer );
240 private void flushProjectCache( MavenProjectBuilder projectBuilder )
244 if ( rawProjectCacheField == null )
246 rawProjectCacheField = DefaultMavenProjectBuilder.class.getDeclaredField( "rawProjectCache" );
247 rawProjectCacheField.setAccessible( true );
250 if ( processedProjectCacheField == null )
252 processedProjectCacheField =
253 DefaultMavenProjectBuilder.class.getDeclaredField( "processedProjectCache" );
254 processedProjectCacheField.setAccessible( true );
257 rawProjectCacheField.set( projectBuilder, new HashMap() );
259 processedProjectCacheField.set( projectBuilder, new HashMap() );
261 catch ( Exception e )
263 throw new RuntimeException( e );
267 private static class XmlSerializingDependencyNodeVisitor
268 implements DependencyNodeVisitor
270 private Element xmlNode;
272 public XmlSerializingDependencyNodeVisitor( Document document )
274 xmlNode = document.addElement( "tree" );
277 // DependencyNodeVisitor methods ------------------------------------------
280 * @see org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor#visit(org.apache.maven.shared.dependency.tree.DependencyNode)
282 public boolean visit( DependencyNode node )
284 Element dependency = xmlNode.addElement( "dependency" );
286 Artifact artifact = node.getArtifact();
287 dependency.addElement( "groupId" ).setText( artifact.getGroupId() );
288 dependency.addElement( "artifactId" ).setText( artifact.getArtifactId() );
289 dependency.addElement( "type" ).setText( artifact.getType() );
290 dependency.addElement( "version" ).setText( artifact.getVersion() );
291 if ( artifact.getScope() != null )
293 dependency.addElement( "scope" ).setText( artifact.getScope() );
296 xmlNode = dependency.addElement( "dependencies" );
302 * @see org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor#endVisit(org.apache.maven.shared.dependency.tree.DependencyNode)
304 public boolean endVisit( DependencyNode node )
306 Element e = xmlNode.getParent();
308 if ( !xmlNode.hasContent() )
313 xmlNode = e.getParent();