You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/06/22 02:26:07 UTC

svn commit: r416183 - in /incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi: GenerateInstallerMojo.java JbiResolutionListener.java

Author: gnodet
Date: Wed Jun 21 17:26:06 2006
New Revision: 416183

URL: http://svn.apache.org/viewvc?rev=416183&view=rev
Log:
Remove dependencies provided by shared libraries

Added:
    incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/JbiResolutionListener.java
Modified:
    incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/GenerateInstallerMojo.java

Modified: incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/GenerateInstallerMojo.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/GenerateInstallerMojo.java?rev=416183&r1=416182&r2=416183&view=diff
==============================================================================
--- incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/GenerateInstallerMojo.java (original)
+++ incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/GenerateInstallerMojo.java Wed Jun 21 17:26:06 2006
@@ -17,16 +17,35 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 import org.apache.maven.archiver.MavenArchiver;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
+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.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.DependencyManagement;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.project.ProjectBuildingException;
+import org.apache.servicemix.maven.plugin.jbi.JbiResolutionListener.Node;
 import org.codehaus.plexus.archiver.ArchiverException;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.jar.ManifestException;
@@ -84,6 +103,42 @@
      */
     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
 
+    /**
+     * @component
+     */
+    private MavenProjectBuilder pb;
+    
+
+    /**
+     * @parameter default-value="${localRepository}"
+     */
+    private ArtifactRepository localRepo;
+
+    /**
+     * @parameter default-value="${project.remoteArtifactRepositories}"
+     */
+    private List remoteRepos;
+    
+    /**
+     * @component
+     */
+    protected ArtifactMetadataSource artifactMetadataSource;
+
+    /**
+     * @component
+     */
+    protected ArtifactResolver resolver;
+
+    /**
+     * @component
+     */
+    private ArtifactCollector collector;
+
+    /**
+     * @component
+     */
+    protected ArtifactFactory factory;
+
     public void execute() throws MojoExecutionException, MojoFailureException {
 
         getLog().debug(" ======= GenerateInstallerMojo settings =======");
@@ -147,23 +202,121 @@
             throw new JbiPluginException("Unable to copy file " + projectArtifact, e);
         }
 
-        Set artifacts = project.getArtifacts();
-        for (Iterator iter = artifacts.iterator(); iter.hasNext();) {
-            Artifact artifact = (Artifact) iter.next();
+        ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
 
-            // TODO: utilise appropriate methods from project builder
-            ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
+        JbiResolutionListener listener = resolveProject();
+        //print(listener.getRootNode(), "");
+        
+        Set sharedLibraries = new HashSet();
+        Set includes = new HashSet();
+        for (Iterator iter = project.getArtifacts().iterator(); iter.hasNext();) {
+            Artifact artifact = (Artifact) iter.next();
             if (!artifact.isOptional() && filter.include(artifact)) {
-                String type = artifact.getType();
-                if ("jar".equals(type)) {
-                    try {
-                        FileUtils.copyFileToDirectory(artifact.getFile(), new File(workDirectory, LIB_DIRECTORY));
-                    } catch (IOException e) {
-                        throw new JbiPluginException("Unable to copy file " + artifact.getFile(), e);
-                    }
+                MavenProject project = null;
+                try {
+                    project = pb.buildFromRepository(artifact, remoteRepos, localRepo);
+                } catch (ProjectBuildingException e) {
+                    getLog().warn(
+                            "Unable to determine packaging for dependency : "
+                                    + artifact.getArtifactId()
+                                    + " assuming jar");
+                }
+                String type = project != null ? project.getPackaging() : artifact.getType();
+                if ("jbi-shared-library".equals(type)) {
+                    removeBranch(listener, artifact);
+                } else if ("jar".equals(type)) {
+                    includes.add(artifact);
+                }
+            }
+        }
+        //print(listener.getRootNode(), "");
+        includes.retainAll(getArtifacts(listener.getRootNode(), new HashSet()));
+        
+        for (Iterator iter = includes.iterator(); iter.hasNext();) {
+            Artifact artifact = (Artifact) iter.next();
+            try {
+                getLog().info("Including: " + artifact);
+                FileUtils.copyFileToDirectory(artifact.getFile(), new File(workDirectory, LIB_DIRECTORY));
+            } catch (IOException e) {
+                throw new JbiPluginException("Unable to copy file " + artifact.getFile(), e);
+            }
+        }
+    }
+
+    private void removeBranch(JbiResolutionListener listener, Artifact artifact) {
+        Node n = listener.getNode(artifact);
+        if (n != null && n.getParent() != null) {
+            n.getParent().getChildren().remove(n);
+        }
+    }
+    
+    private Set getArtifacts(Node n, Set s) {
+        s.add(n.getArtifact());
+        for (Iterator iter = n.getChildren().iterator(); iter.hasNext();) {
+            Node c = (Node) iter.next();
+            getArtifacts(c, s);
+        }
+        return s;
+    }
+
+    private void excludeBranch(Node n, Set excludes) {
+        excludes.add(n);
+        for (Iterator iter = n.getChildren().iterator(); iter.hasNext();) {
+            Node c = (Node) iter.next();
+            excludeBranch(c, excludes);
+        }
+    }
+
+    private void print(Node rootNode, String string) {
+        getLog().info(string + rootNode.getArtifact());
+        for (Iterator iter = rootNode.getChildren().iterator(); iter.hasNext();) {
+            Node n = (Node) iter.next();
+            print(n, string + "  ");
+        }
+    }
+    
+    private JbiResolutionListener resolveProject() {
+        Map managedVersions = null;
+        try {
+            managedVersions = createManagedVersionMap( project.getId(), project.getDependencyManagement() );
+        }
+        catch ( ProjectBuildingException e ) {
+            getLog().error( "An error occurred while resolving project dependencies.", e );
+        }
+        JbiResolutionListener listener = new JbiResolutionListener();
+        try {
+            collector.collect( project.getDependencyArtifacts(), project.getArtifact(), managedVersions,
+                               localRepo, remoteRepos, artifactMetadataSource, null,
+                               Collections.singletonList( listener ) );
+        }
+        catch ( ArtifactResolutionException e ) {
+            getLog().error( "An error occurred while resolving project dependencies.", e );
+        }
+        return listener;
+    }
+
+    private Map createManagedVersionMap(String projectId, DependencyManagement dependencyManagement)
+                    throws ProjectBuildingException {
+        Map map;
+        if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
+            map = new HashMap();
+            for (Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext();) {
+                Dependency d = (Dependency) i.next();
+
+                try {
+                    VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
+                    Artifact artifact = factory.createDependencyArtifact(d.getGroupId(), d.getArtifactId(),
+                                    versionRange, d.getType(), d.getClassifier(), d.getScope());
+                    map.put(d.getManagementKey(), artifact);
+                } catch (InvalidVersionSpecificationException e) {
+                    throw new ProjectBuildingException(projectId, "Unable to parse version '" + d.getVersion()
+                                    + "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e);
                 }
             }
+        } else {
+            map = Collections.EMPTY_MAP;
         }
+        return map;
     }
 
 }

Added: incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/JbiResolutionListener.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/JbiResolutionListener.java?rev=416183&view=auto
==============================================================================
--- incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/JbiResolutionListener.java (added)
+++ incubator/servicemix/trunk/tooling/jbi-maven-plugin/src/main/java/org/apache/servicemix/maven/plugin/jbi/JbiResolutionListener.java Wed Jun 21 17:26:06 2006
@@ -0,0 +1,183 @@
+package org.apache.servicemix.maven.plugin.jbi;
+
+/*
+ * Copyright 2004-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.resolver.ResolutionListener;
+import org.apache.maven.artifact.versioning.VersionRange;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+/**
+ * @author Edwin Punzalan
+ */
+public class JbiResolutionListener
+    implements ResolutionListener
+{
+    private Stack parents = new Stack();
+
+    private Map artifacts = new HashMap();
+
+    private Node rootNode;
+
+    public void testArtifact( Artifact artifact )
+    {
+        // intentionally blank
+    }
+
+    public void startProcessChildren( Artifact artifact )
+    {
+        Node node = (Node) artifacts.get( artifact.getDependencyConflictId() );
+        if ( parents.isEmpty() )
+        {
+            rootNode = node;
+        }
+
+        parents.push( node );
+    }
+
+    public void endProcessChildren( Artifact artifact )
+    {
+        Node check = (Node) parents.pop();
+        assert artifact.equals( check.artifact );
+    }
+
+    public void omitForNearer( Artifact omitted, Artifact kept )
+    {
+        assert omitted.getDependencyConflictId().equals( kept.getDependencyConflictId() );
+
+        Node prev = (Node) artifacts.get( omitted.getDependencyConflictId() );
+        if ( prev != null )
+        {
+            if ( prev.parent != null )
+            {
+                prev.parent.children.remove( prev );
+            }
+            artifacts.remove( omitted.getDependencyConflictId() );
+        }
+
+        includeArtifact( kept );
+    }
+
+    public void omitForCycle( Artifact artifact )
+    {
+        // intentionally blank
+    }
+
+    public void includeArtifact( Artifact artifact )
+    {
+        if ( artifacts.containsKey( artifact.getDependencyConflictId() ) )
+        {
+            Node prev = (Node) artifacts.get( artifact.getDependencyConflictId() );
+            if ( prev.parent != null )
+            {
+                prev.parent.children.remove( prev );
+            }
+            artifacts.remove( artifact.getDependencyConflictId() );
+        }
+
+        Node node = new Node();
+        node.artifact = artifact;
+        if ( !parents.isEmpty() )
+        {
+            node.parent = (Node) parents.peek();
+            node.parent.children.add( node );
+        }
+        artifacts.put( artifact.getDependencyConflictId(), node );
+    }
+
+    public void updateScope( Artifact artifact, String scope )
+    {
+        Node node = (Node) artifacts.get( artifact.getDependencyConflictId() );
+
+        node.artifact.setScope( scope );
+    }
+
+    public void manageArtifact( Artifact artifact, Artifact replacement )
+    {
+        Node node = (Node) artifacts.get( artifact.getDependencyConflictId() );
+
+        if ( node != null )
+        {
+            if ( replacement.getVersion() != null )
+            {
+                node.artifact.setVersion( replacement.getVersion() );
+            }
+            if ( replacement.getScope() != null )
+            {
+                node.artifact.setScope( replacement.getScope() );
+            }
+        }
+    }
+
+    public void updateScopeCurrentPom( Artifact artifact, String key )
+    {
+        // intentionally blank
+    }
+
+    public void selectVersionFromRange( Artifact artifact )
+    {
+        // intentionally blank
+    }
+
+    public void restrictRange( Artifact artifact, Artifact artifact1, VersionRange versionRange )
+    {
+        // intentionally blank
+    }
+
+    public Node getNode(Artifact artifact) {
+        return (Node) artifacts.get(artifact.getDependencyConflictId());
+    }
+    
+    public Collection getArtifacts()
+    {
+        return artifacts.values();
+    }
+
+    static class Node
+    {
+        private Node parent;
+
+        private List children = new ArrayList();
+
+        private Artifact artifact;
+
+        public List getChildren()
+        {
+            return children;
+        }
+
+        public Artifact getArtifact()
+        {
+            return artifact;
+        }
+        
+        public Node getParent() {
+            return parent;
+        }
+    }
+
+    public Node getRootNode()
+    {
+        return rootNode;
+    }
+}