View Javadoc

    1   //========================================================================
    2   //$Id: RuntimeDependencyResolver.java 397 2006-03-23 18:44:41Z janb $
    3   //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16
    17  package org.mortbay.jetty.plugin;
    18
    19
    20  import java.net.MalformedURLException;
    21  import java.util.ArrayList;
    22  import java.util.Collections;
    23  import java.util.Iterator;
    24  import java.util.List;
    25  import java.util.Set;
    26
    27  import org.apache.maven.artifact.Artifact;
    28  import org.apache.maven.artifact.factory.ArtifactFactory;
    29  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
    30  import org.apache.maven.artifact.repository.ArtifactRepository;
    31  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
    32  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
    33  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
    34  import org.apache.maven.artifact.resolver.ArtifactResolver;
    35  import org.apache.maven.artifact.resolver.ResolutionListener;
    36  import org.apache.maven.artifact.versioning.VersionRange;
    37  import org.apache.maven.project.MavenProject;
    38  import org.apache.maven.project.MavenProjectBuilder;
    39  import org.apache.maven.project.ProjectBuildingException;
    40  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
    41  import org.apache.maven.project.artifact.MavenMetadataSource;
    42  import org.mortbay.jetty.plugin.util.PluginLog;
    43
    44  /**
    45   * RuntimeDependencyResolver
    46   *
    47   * This class is able to pull down a remote pom, find all of it's
    48   * dependencies and transitively resolve them.
    49   *
    50   *
    51   */
    52  public class RuntimeDependencyResolver
    53  {
    54      private ArtifactFactory artifactFactory;
    55      private ArtifactResolver artifactResolver;
    56      private ArtifactMetadataSource metadataSource;
    57      private ArtifactRepository localRepository;
    58      private List remoteRepositories;
    59
    60
    61      /**
    62       * RuntimeResolutionListener
    63       *
    64       * Just for debug printing of transitive resolution steps
    65       *
    66       */
    67      class RuntimeResolutionListener implements ResolutionListener
    68      {
    69          public void testArtifact(Artifact arg0) { PluginLog.getLog().debug ("TESTING ARTIFACT "+arg0);}
    70          public void startProcessChildren(Artifact arg0) {PluginLog.getLog().debug("STARTING CHILDREN "+arg0);}
    71          public void endProcessChildren(Artifact arg0) {PluginLog.getLog().debug("ENDING CHILDREN "+arg0);}
    72          public void includeArtifact(Artifact arg0) {PluginLog.getLog().debug("INCLUDE ARTIFACT "+arg0);}
    73          public void omitForNearer(Artifact arg0, Artifact arg1) {PluginLog.getLog().debug("OMITTING "+arg0+" for NEARER "+arg1);}
    74          public void updateScope(Artifact arg0, String arg1) {PluginLog.getLog().debug("UPDATE of SCOPE "+arg0+ "="+arg1);}
    75          public void manageArtifact(Artifact arg0, Artifact arg1) {PluginLog.getLog().debug("MANAGE ARTIFACT "+arg0+" and "+arg1); }
    76          public void omitForCycle(Artifact arg0) {PluginLog.getLog().debug("OMIT FOR CYCLE "+arg0);}
    77          public void updateScopeCurrentPom(Artifact arg0, String arg1) {PluginLog.getLog().debug("UPDATE SCOPE CURRENT POM "+arg0+"="+arg1);}
    78          public void selectVersionFromRange(Artifact arg0) {PluginLog.getLog().debug("SELECT VERSION FROM RANGE "+arg0);}
    79          public void restrictRange(Artifact arg0, Artifact arg1, VersionRange arg2) {PluginLog.getLog().debug("RESTRICT RANGE "+arg0+" "+arg1+" range="+arg2);}
    80
    81      }
    82
    83
    84      public RuntimeDependencyResolver (ArtifactFactory artifactFactory, ArtifactResolver artifactResolver,
    85              ArtifactMetadataSource metadataSource, ArtifactRepository localRepository, List remoteRepositories)
    86      {
    87          this.artifactFactory = artifactFactory;
    88          this.artifactResolver = artifactResolver;
    89          this.metadataSource = metadataSource;
    90          this.localRepository = localRepository;
    91          this.remoteRepositories = new ArrayList(remoteRepositories);
    92      }
    93
    94
    95      /**
    96       * Download (if necessary) a pom, and load it as a MavenProject, transitively resolving any
    97       * dependencies therein.
    98       *
    99       * @param projectBuilder
    100      * @param groupId
    101      * @param artifactId
    102      * @param versionId
    103      * @return a Set of Artifacts representing the transitively resolved dependencies.
    104      *
    105      * @throws MalformedURLException
    106      * @throws ProjectBuildingException
    107      * @throws InvalidDependencyVersionException
    108      * @throws ArtifactResolutionException
    109      * @throws ArtifactNotFoundException
    110      */
    111     public Set transitivelyResolvePomDependencies (MavenProjectBuilder projectBuilder, String groupId, String artifactId, String versionId, boolean resolveProjectArtifact)
    112     throws MalformedURLException, ProjectBuildingException, InvalidDependencyVersionException, ArtifactResolutionException, ArtifactNotFoundException
    113     {
    114
    115         Artifact pomArtifact = getPomArtifact(groupId, artifactId, versionId);
    116         MavenProject project = loadPomAsProject(projectBuilder, pomArtifact);
    117         List dependencies = project.getDependencies();
    118
    119
    120         Set dependencyArtifacts = MavenMetadataSource.createArtifacts( artifactFactory, dependencies, null, null, null );
    121         dependencyArtifacts.add(project.getArtifact());
    122
    123         List listeners = Collections.EMPTY_LIST;
    124
    125         if (PluginLog.getLog().isDebugEnabled())
    126         {
    127             listeners = new ArrayList();
    128             listeners.add(new RuntimeResolutionListener());
    129         }
    130
    131         ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, pomArtifact,
    132                 Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, null, listeners);
    133
    134         Set artifacts = result.getArtifacts();
    135
    136         if (PluginLog.getLog().isDebugEnabled())
    137         {
    138             PluginLog.getLog().debug("RESOLVED "+artifacts.size()+" ARTIFACTS");
    139             Iterator itor = artifacts.iterator();
    140             while (itor.hasNext())
    141             {
    142                 Artifact a = (Artifact)itor.next();
    143                 PluginLog.getLog().debug(a.getFile().toURL().toString());
    144             }
    145         }
    146         return artifacts;
    147     }
    148
    149
    150
    151     public MavenProject loadPomAsProject (MavenProjectBuilder projectBuilder, Artifact pomArtifact)
    152     throws ProjectBuildingException
    153     {
    154         return projectBuilder.buildFromRepository(pomArtifact, remoteRepositories,localRepository);
    155     }
    156
    157
    158     public Artifact getArtifact (String groupId, String artifactId, String versionId, String type)
    159     {
    160         return this.artifactFactory.createBuildArtifact(groupId, artifactId, versionId, type);
    161     }
    162
    163
    164     public Artifact getPomArtifact (String groupId, String artifactId, String versionId)
    165     {
    166         return this.artifactFactory.createBuildArtifact(groupId, artifactId, versionId, "pom");
    167     }
    168
    169     public void removeDependency (Set artifacts, String groupId, String artifactId, String versionId, String type)
    170     {
    171         if ((artifacts == null) || artifacts.isEmpty())
    172             return;
    173
    174         Iterator itor = artifacts.iterator();
    175         while (itor.hasNext())
    176         {
    177             Artifact a = (Artifact)itor.next();
    178             if (a.getGroupId().equals(groupId) && a.getArtifactId().equals(artifactId) && a.getType().equals(type))
    179             {
    180                 //remove if the versions match, or there was no version specified
    181                 if (versionId == null)
    182                     itor.remove();
    183                 else if (a.getVersion().equals(versionId))
    184                     itor.remove();
    185             }
    186         }
    187     }
    188
    189     public void addDependency (Set artifacts, String groupId, String artifactId, String versionId, String type)
    190     throws ArtifactResolutionException, ArtifactNotFoundException
    191     {
    192         Artifact a = getArtifact(groupId, artifactId, versionId, type);
    193         artifactResolver.resolve(a, remoteRepositories, localRepository);
    194         artifacts.add(a);
    195     }
    196
    197 }