maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From car...@apache.org
Subject svn commit: r513904 - in /maven/sandbox/trunk/shared/grafo/grafo-maven-plugin: pom.xml src/main/java/org/apache/maven/plugins/grafo/Grafo.java src/main/java/org/apache/maven/plugins/grafo/GrafoMojo.java
Date Fri, 02 Mar 2007 19:34:51 GMT
Author: carlos
Date: Fri Mar  2 11:34:50 2007
New Revision: 513904

URL: http://svn.apache.org/viewvc?view=rev&rev=513904
Log:
Use maven-dependency-tree for dependency resolution

Modified:
    maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/pom.xml
    maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/Grafo.java
    maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/GrafoMojo.java

Modified: maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/pom.xml?view=diff&rev=513904&r1=513903&r2=513904
==============================================================================
--- maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/pom.xml (original)
+++ maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/pom.xml Fri Mar  2 11:34:50 2007
@@ -3,9 +3,9 @@
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
-    <artifactId>maven-plugins</artifactId>
     <groupId>org.apache.maven.plugins</groupId>
-    <version>1</version>
+    <artifactId>maven-plugins</artifactId>
+    <version>8</version>
   </parent>
   <artifactId>maven-grafo-plugin</artifactId>
   <packaging>maven-plugin</packaging>
@@ -15,22 +15,27 @@
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-plugin-api</artifactId>
-      <version>2.0</version>
+      <version>2.0.5</version>
     </dependency>
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-project</artifactId>
-      <version>2.0.3</version>
+      <version>2.0.5</version>
     </dependency>
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-artifact-manager</artifactId>
-      <version>2.0.3</version>
+      <version>2.0.5</version>
     </dependency>
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-artifact</artifactId>
-      <version>2.0.3</version>
+      <version>2.0.5</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-dependency-tree</artifactId>
+      <version>1.0-alpha-3-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>org.prefuse</groupId>

Modified: maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/Grafo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/Grafo.java?view=diff&rev=513904&r1=513903&r2=513904
==============================================================================
--- maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/Grafo.java
(original)
+++ maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/Grafo.java
Fri Mar  2 11:34:50 2007
@@ -22,21 +22,26 @@
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
-import java.util.Collections;
-import java.util.HashMap;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactCollector;
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
-import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.project.ProjectBuildingException;
+import org.apache.maven.shared.dependency.tree.DependencyNode;
+import org.apache.maven.shared.dependency.tree.DependencyTree;
+import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
+import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
@@ -61,41 +66,47 @@
 
     private ArtifactMetadataSource artifactMetadataSource;
 
+    private DependencyTreeBuilder dependencyTreeBuilder;
+
+    private ArtifactCollector collector;
+
+    private MavenProjectBuilder mavenProjectBuilder;
+
     private Xpp3Dom report;
 
     public Grafo( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory,
-                 ArtifactMetadataSource artifactMetadataSource )
+                  ArtifactMetadataSource artifactMetadataSource, DependencyTreeBuilder dependencyTreeBuilder,
+                  ArtifactCollector collector, MavenProjectBuilder mavenProjectBuilder )
     {
         this.artifactResolver = artifactResolver;
         this.artifactFactory = artifactFactory;
         this.artifactMetadataSource = artifactMetadataSource;
+        this.dependencyTreeBuilder = dependencyTreeBuilder;
+        this.collector = collector;
+        this.mavenProjectBuilder = mavenProjectBuilder;
     }
 
     public void execute( String groupId, String artifactId, String version, ArtifactRepository
localRepository,
-                        List remoteRepositories )
+                         List remoteRepositories )
         throws ArtifactResolutionException, ArtifactNotFoundException
     {
-        // ----------------------------------------------------------------------
-        // Download the archetype
-        // ----------------------------------------------------------------------
-
-        Artifact dummyArtifact = artifactFactory.createArtifact( "dummy", "dummy", "dummy",
Artifact.SCOPE_RUNTIME,
-                                                                 "jar" );
-
-        Artifact artifact = artifactFactory
-            .createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar"
);
-
-        artifactResolver.resolve( artifact, remoteRepositories, localRepository );
-
-        GrafoResolutionListener grafoResolutionListener = new GrafoResolutionListener();
-        List listeners = Collections.singletonList( grafoResolutionListener );
-        Set artifacts = Collections.singleton( artifact );
+        // TODO
+    }
 
-        //System.out.println( artifact.getDependencyTrail() );
+    public void execute( MavenProject project, ArtifactRepository localRepository, List remoteRepositories
)
+        throws MojoExecutionException
+    {
+        DependencyTree dependencyTree;
 
-        ArtifactResolutionResult result = artifactResolver
-            .resolveTransitively( artifacts, dummyArtifact, new HashMap(), localRepository,
remoteRepositories,
-                                  artifactMetadataSource, null, listeners );
+        try
+        {
+            dependencyTree = dependencyTreeBuilder.buildDependencyTree( project, localRepository,
artifactFactory,
+                                                                        artifactMetadataSource,
collector );
+        }
+        catch ( DependencyTreeBuilderException e )
+        {
+            throw new MojoExecutionException( "Unable to build dependency tree", e );
+        }
 
         report = new Xpp3Dom( "graphml" );
         report.setAttribute( "xmlns", "http://graphml.graphdrawing.org/xmlns" );
@@ -105,9 +116,29 @@
 
         addKeys( graph );
 
-        processNodes( graph, grafoResolutionListener.getNodes() );
-        System.out.println();
-        processEdges( graph, grafoResolutionListener.getEdges() );
+        for ( Iterator it = dependencyTree.iterator(); it.hasNext(); )
+        {
+            DependencyNode node = (DependencyNode) it.next();
+
+            Artifact artifact = resolveArtifact( node.getArtifact(), remoteRepositories,
localRepository );
+
+            MavenProject childProject;
+            try
+            {
+                childProject = mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories,
localRepository,
+                                                                        true );
+            }
+            catch ( ProjectBuildingException e )
+            {
+                throw new MojoExecutionException( "Unable to build project object for artifact
" + artifact, e );
+            }
+
+            childProject.setArtifact( artifact );
+
+            processNode( report, artifact );
+            processEdges( report, node, node.getChildren() );
+        }
+
         System.out.println( report );
 
         PrintWriter writer = null;
@@ -134,64 +165,57 @@
         }
 
     }
-    
+
     private String getId( Artifact artifact )
     {
         return artifact.getId();
     }
 
-    private void processNodes( Xpp3Dom dom, Map nodes )
+    private void processNode( Xpp3Dom dom, Artifact artifact )
     {
-        Iterator it = nodes.values().iterator();
-        while ( it.hasNext() )
-        {
-            Artifact artifact = (Artifact) it.next();
-            System.out.println( artifact.getArtifactId() );
+        System.out.println( artifact.getArtifactId() );
 
-            Xpp3Dom node = new Xpp3Dom( "node" );
-            node.setAttribute( "id", getId( artifact ) );
+        Xpp3Dom node = new Xpp3Dom( "node" );
+        node.setAttribute( "id", getId( artifact ) );
 
-            Xpp3Dom data = new Xpp3Dom( "data" );
-            data.setAttribute( "key", "artifactId" );
-            data.setValue( artifact.getArtifactId() );
-            node.addChild( data );
+        Xpp3Dom data = new Xpp3Dom( "data" );
+        data.setAttribute( "key", "artifactId" );
+        data.setValue( artifact.getArtifactId() );
+        node.addChild( data );
+
+        data = new Xpp3Dom( "data" );
+        data.setAttribute( "key", "groupId" );
+        data.setValue( artifact.getGroupId() );
+        node.addChild( data );
+
+        data = new Xpp3Dom( "data" );
+        data.setAttribute( "key", "version" );
+        data.setValue( artifact.getVersion() );
+        node.addChild( data );
+
+        data = new Xpp3Dom( "data" );
+        data.setAttribute( "key", "scope" );
+        data.setValue( artifact.getScope() );
+        node.addChild( data );
+
+        data = new Xpp3Dom( "data" );
+        data.setAttribute( "key", "label" );
+        data.setValue( artifact.getArtifactId() + ":" + artifact.getVersion() );
+        node.addChild( data );
 
-            data = new Xpp3Dom( "data" );
-            data.setAttribute( "key", "groupId" );
-            data.setValue( artifact.getGroupId() );
-            node.addChild( data );
-
-            data = new Xpp3Dom( "data" );
-            data.setAttribute( "key", "version" );
-            data.setValue( artifact.getVersion() );
-            node.addChild( data );
-
-            data = new Xpp3Dom( "data" );
-            data.setAttribute( "key", "scope" );
-            data.setValue( artifact.getScope() );
-            node.addChild( data );
-
-            data = new Xpp3Dom( "data" );
-            data.setAttribute( "key", "label" );
-            data.setValue( artifact.getArtifactId() + ":" + artifact.getVersion() );
-            node.addChild( data );
-
-            dom.addChild( node );
-        }
+        dom.addChild( node );
     }
 
-    private void processEdges( Xpp3Dom dom, Set links )
+    private void processEdges( Xpp3Dom dom, DependencyNode parent, Collection children )
     {
-        Iterator it = links.iterator();
+        Iterator it = children.iterator();
         while ( it.hasNext() )
         {
-            ArtifactDependency artifactDependency = (ArtifactDependency) it.next();
-            System.out.println( artifactDependency.getOrigin().getArtifactId() + " ->
"
-                + artifactDependency.getDestination().getArtifactId() );
+            DependencyNode child = (DependencyNode) it.next();
 
             Xpp3Dom edge = new Xpp3Dom( "edge" );
-            edge.setAttribute( "source", getId( artifactDependency.getOrigin() ) );
-            edge.setAttribute( "target", getId( artifactDependency.getDestination() ) );
+            edge.setAttribute( "source", getId( parent.getArtifact() ) );
+            edge.setAttribute( "target", getId( child.getArtifact() ) );
             dom.addChild( edge );
         }
     }
@@ -234,4 +258,26 @@
         graph.addChild( key );
     }
 
+    private Artifact resolveArtifact( Artifact artifact, List remoteRepositories, ArtifactRepository
localRepository )
+        throws MojoExecutionException
+    {
+        Artifact resolvedArtifact = artifactFactory.createArtifact( artifact.getGroupId(),
artifact.getArtifactId(),
+                                                                    artifact.getVersion(),
artifact.getScope(),
+                                                                    artifact.getType() );
+
+        try
+        {
+            artifactResolver.resolve( resolvedArtifact, remoteRepositories, localRepository
);
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            throw new MojoExecutionException( "Artifact was not found in the repo" + resolvedArtifact,
e );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            throw new MojoExecutionException( "Error resolving artifact " + resolvedArtifact,
e );
+        }
+
+        return resolvedArtifact;
+    }
 }

Modified: maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/GrafoMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/GrafoMojo.java?view=diff&rev=513904&r1=513903&r2=513904
==============================================================================
--- maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/GrafoMojo.java
(original)
+++ maven/sandbox/trunk/shared/grafo/grafo-maven-plugin/src/main/java/org/apache/maven/plugins/grafo/GrafoMojo.java
Fri Mar  2 11:34:50 2007
@@ -16,22 +16,20 @@
  * limitations under the License.
  */
 
-import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
-import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
-import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
-import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactCollector;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.StringUtils;
+import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
 
 /**
  * <p>
@@ -44,7 +42,7 @@
  * @version $Id$
  */
 public class GrafoMojo
-  extends AbstractMojo
+    extends AbstractMojo
 {
 
     /**
@@ -56,7 +54,7 @@
      * @component
      */
     private ArtifactFactory artifactFactory;
-    
+
     /**
      * @component
      */
@@ -72,7 +70,6 @@
      */
     private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
 
-
     /**
      * @parameter expression="${localRepository}"
      * @required
@@ -102,59 +99,28 @@
     private List pomRemoteRepositories;
 
     /**
-     * @parameter expression="${remoteRepositories}"
-     */
-    private String remoteRepositories;
-
-    /**
      * @parameter expression="${project}"
      */
     private MavenProject project;
-    
-    public void execute()
-        throws MojoExecutionException
-    {
-        // TODO: prompt for missing values
-        // TODO: configurable license
-
-        // ----------------------------------------------------------------------
-        // archetypeGroupId
-        // archetypeArtifactId
-        // archetypeVersion
-        //
-        // localRepository
-        // remoteRepository
-        // parameters
-        // ----------------------------------------------------------------------
-
-        if ( project.getFile() != null && groupId == null )
-        {
-            groupId = project.getGroupId();
-        }
 
-        if ( project.getFile() != null && artifactId == null )
-        {
-            artifactId = project.getArtifactId();
-        }
-
-        String basedir = System.getProperty( "user.dir" );
-
-        List archetypeRemoteRepositories = new ArrayList( pomRemoteRepositories );
-
-        if ( remoteRepositories != null )
-        {
-            getLog().info( "We are using command line specified remote repositories: " +
remoteRepositories );
-
-            archetypeRemoteRepositories = new ArrayList();
+    /**
+     * @component
+     */
+    private DependencyTreeBuilder dependencyTreeBuilder;
 
-            String[] s = StringUtils.split( remoteRepositories, "," );
+    /**
+     * @component
+     */
+    private ArtifactCollector collector;
 
-            for ( int i = 0; i < s.length; i++ )
-            {
-                archetypeRemoteRepositories.add( createRepository( s[i], "id" + i ) );
-            }
-        }
+    /**
+     * @component
+     */
+    private MavenProjectBuilder mavenProjectBuilder;
 
+    public void execute()
+        throws MojoExecutionException
+    {
         if ( artifactResolver == null )
         {
             throw new NullPointerException( "artifactResolver can not be null" );
@@ -163,45 +129,9 @@
         {
             throw new NullPointerException( "artifactFactory can not be null" );
         }
-        
-        Grafo grafo = new Grafo( artifactResolver, artifactFactory, artifactMetadataSource
);
-        try
-        {
-            grafo.execute( groupId, artifactId, version,
-                           localRepository, archetypeRemoteRepositories );
-        }
-        catch ( ArtifactResolutionException e )
-        {
-            throw new MojoExecutionException( "Error during artifact resolution", e );
-        }
-        catch ( ArtifactNotFoundException e )
-        {
-            throw new MojoExecutionException( "Artifact not found", e );
-        }
-    }
-
-    //TODO: this should be put in John's artifact utils and used from there instead of being
repeated here. Creating
-    // artifact repositories is someowhat cumbersome atm.
-    public ArtifactRepository createRepository( String url, String repositoryId )
-    {
-        // snapshots vs releases
-        // offline = to turning the update policy off
-
-        //TODO: we'll need to allow finer grained creation of repositories but this will
do for now
 
-        String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
-
-        String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
-
-        ArtifactRepositoryPolicy snapshotsPolicy =
-            new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
-
-        ArtifactRepositoryPolicy releasesPolicy =
-            new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
-
-        return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout,
-                                                                   snapshotsPolicy, releasesPolicy
);
+        Grafo grafo = new Grafo( artifactResolver, artifactFactory, artifactMetadataSource,
dependencyTreeBuilder,
+                                 collector, mavenProjectBuilder );
+        grafo.execute( project, localRepository, pomRemoteRepositories );
     }
-    
 }
-



Mime
View raw message