View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.internal;
20  
21  import java.util.Collection;
22  import java.util.IdentityHashMap;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.Map;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.ResolutionListener;
29  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  
32  /**
33   * Assists in detecting wagon providers brought into the plugin class path via legacy Maven core artifacts (e.g.
34   * maven-core:2.0.6) and excluding them. A plugin should be able to explicitly declare dependencies on specific wagons
35   * for its use. However, the (old) wagons pulled in transitively via legacy Maven core artifacts are usually not
36   * intended as dependencies and more importantly screw up artifact resolution because they would get preferred over the
37   * core wagon versions. This is a hack to provide backward-compat with Maven 2 (MNG-4528, MNG-4561).
38   *
39   * @since 3.0
40   * @author Benjamin Bentmann
41   */
42  class PluginDependencyResolutionListener implements ResolutionListener {
43  
44      private ArtifactFilter coreFilter;
45  
46      private LinkedList<Artifact> coreArtifacts = new LinkedList<>();
47  
48      private Artifact wagonProvider;
49  
50      private Map<Artifact, Object> bannedArtifacts = new IdentityHashMap<>();
51  
52      PluginDependencyResolutionListener(ArtifactFilter coreFilter) {
53          this.coreFilter = coreFilter;
54      }
55  
56      public void removeBannedDependencies(Collection<Artifact> artifacts) {
57          if (!bannedArtifacts.isEmpty() && artifacts != null) {
58              for (Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); ) {
59                  Artifact artifact = it.next();
60                  if (bannedArtifacts.containsKey(artifact)) {
61                      it.remove();
62                  }
63              }
64          }
65      }
66  
67      public void startProcessChildren(Artifact artifact) {
68          if (wagonProvider == null) {
69              if (isLegacyCoreArtifact(artifact)) {
70                  coreArtifacts.addFirst(artifact);
71              } else if (!coreArtifacts.isEmpty() && isWagonProvider(artifact)) {
72                  wagonProvider = artifact;
73                  bannedArtifacts.put(artifact, null);
74              }
75          }
76      }
77  
78      private boolean isLegacyCoreArtifact(Artifact artifact) {
79          String version = artifact.getVersion();
80          return version != null && version.startsWith("2.") && !coreFilter.include(artifact);
81      }
82  
83      public void endProcessChildren(Artifact artifact) {
84          if (wagonProvider == artifact) {
85              wagonProvider = null;
86          } else if (coreArtifacts.peek() == artifact) {
87              coreArtifacts.removeFirst();
88          }
89      }
90  
91      public void includeArtifact(Artifact artifact) {
92          if (wagonProvider != null) {
93              bannedArtifacts.put(artifact, null);
94          }
95      }
96  
97      private boolean isWagonProvider(Artifact artifact) {
98          if ("org.apache.maven.wagon".equals(artifact.getGroupId())) {
99              return artifact.getArtifactId().startsWith("wagon-");
100         }
101         return false;
102     }
103 
104     public void manageArtifact(Artifact artifact, Artifact replacement) {}
105 
106     public void omitForCycle(Artifact artifact) {}
107 
108     public void omitForNearer(Artifact omitted, Artifact kept) {}
109 
110     public void restrictRange(Artifact artifact, Artifact replacement, VersionRange newRange) {}
111 
112     public void selectVersionFromRange(Artifact artifact) {}
113 
114     public void testArtifact(Artifact node) {}
115 
116     public void updateScope(Artifact artifact, String scope) {}
117 
118     public void updateScopeCurrentPom(Artifact artifact, String ignoredScope) {}
119 }