View Javadoc
1   package org.apache.maven.plugin.internal;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  import java.util.IdentityHashMap;
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  import java.util.Map;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.resolver.ResolutionListener;
30  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  
33  /**
34   * Assists in detecting wagon providers brought into the plugin class path via legacy Maven core artifacts (e.g.
35   * maven-core:2.0.6) and excluding them. A plugin should be able to explicitly declare dependencies on specific wagons
36   * for its use. However, the (old) wagons pulled in transitively via legacy Maven core artifacts are usually not
37   * intended as dependencies and more importantly screw up artifact resolution because they would get preferred over the
38   * core wagon versions. This is a hack to provide backward-compat with Maven 2 (MNG-4528, MNG-4561).
39   * 
40   * @since 3.0
41   * @author Benjamin Bentmann
42   */
43  class PluginDependencyResolutionListener
44      implements ResolutionListener
45  {
46  
47      private ArtifactFilter coreFilter;
48  
49      private LinkedList<Artifact> coreArtifacts = new LinkedList<Artifact>();
50  
51      private Artifact wagonProvider;
52  
53      private Map<Artifact, Object> bannedArtifacts = new IdentityHashMap<Artifact, Object>();
54  
55      public PluginDependencyResolutionListener( ArtifactFilter coreFilter )
56      {
57          this.coreFilter = coreFilter;
58      }
59  
60      public void removeBannedDependencies( Collection<Artifact> artifacts )
61      {
62          if ( !bannedArtifacts.isEmpty() && artifacts != null )
63          {
64              for ( Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); )
65              {
66                  Artifact artifact = it.next();
67                  if ( bannedArtifacts.containsKey( artifact ) )
68                  {
69                      it.remove();
70                  }
71              }
72          }
73      }
74  
75      public void startProcessChildren( Artifact artifact )
76      {
77          if ( wagonProvider == null )
78          {
79              if ( isLegacyCoreArtifact( artifact ) )
80              {
81                  coreArtifacts.addFirst( artifact );
82              }
83              else if ( !coreArtifacts.isEmpty() && isWagonProvider( artifact ) )
84              {
85                  wagonProvider = artifact;
86                  bannedArtifacts.put( artifact, null );
87              }
88          }
89      }
90  
91      private boolean isLegacyCoreArtifact( Artifact artifact )
92      {
93          String version = artifact.getVersion();
94          return version != null && version.startsWith( "2." ) && !coreFilter.include( artifact );
95      }
96  
97      public void endProcessChildren( Artifact artifact )
98      {
99          if ( wagonProvider == artifact )
100         {
101             wagonProvider = null;
102         }
103         else if ( coreArtifacts.peek() == artifact )
104         {
105             coreArtifacts.removeFirst();
106         }
107     }
108 
109     public void includeArtifact( Artifact artifact )
110     {
111         if ( wagonProvider != null )
112         {
113             bannedArtifacts.put( artifact, null );
114         }
115     }
116 
117     private boolean isWagonProvider( Artifact artifact )
118     {
119         if ( "org.apache.maven.wagon".equals( artifact.getGroupId() ) )
120         {
121             return artifact.getArtifactId().startsWith( "wagon-" );
122         }
123         return false;
124     }
125 
126     public void manageArtifact( Artifact artifact, Artifact replacement )
127     {
128     }
129 
130     public void omitForCycle( Artifact artifact )
131     {
132     }
133 
134     public void omitForNearer( Artifact omitted, Artifact kept )
135     {
136     }
137 
138     public void restrictRange( Artifact artifact, Artifact replacement, VersionRange newRange )
139     {
140     }
141 
142     public void selectVersionFromRange( Artifact artifact )
143     {
144     }
145 
146     public void testArtifact( Artifact node )
147     {
148     }
149 
150     public void updateScope( Artifact artifact, String scope )
151     {
152     }
153 
154     public void updateScopeCurrentPom( Artifact artifact, String ignoredScope )
155     {
156     }
157 
158 }