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 org.eclipse.aether.artifact.Artifact;
22  import org.eclipse.aether.collection.DependencyCollectionContext;
23  import org.eclipse.aether.collection.DependencySelector;
24  import org.eclipse.aether.graph.Dependency;
25  
26  /**
27   * Assists in detecting wagon providers brought into the plugin class path via legacy Maven core artifacts (e.g.
28   * maven-core:2.0.6) and excluding them. A plugin should be able to explicitly declare dependencies on specific wagons
29   * for its use. However, the (old) wagons pulled in transitively via legacy Maven core artifacts are usually not
30   * intended as dependencies and more importantly screw up artifact resolution because they would get preferred over the
31   * core wagon versions. This is a hack to provide backward-compat with Maven 2 (MNG-4528, MNG-4561).
32   *
33   * @author Benjamin Bentmann
34   */
35  class WagonExcluder implements DependencySelector {
36  
37      private final boolean coreArtifact;
38  
39      WagonExcluder() {
40          this(false);
41      }
42  
43      private WagonExcluder(boolean coreArtifact) {
44          this.coreArtifact = coreArtifact;
45      }
46  
47      public boolean selectDependency(Dependency dependency) {
48          return !coreArtifact || !isWagonProvider(dependency.getArtifact());
49      }
50  
51      public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
52          if (coreArtifact || !isLegacyCoreArtifact(context.getDependency().getArtifact())) {
53              return this;
54          } else {
55              return new WagonExcluder(true);
56          }
57      }
58  
59      private boolean isLegacyCoreArtifact(Artifact artifact) {
60          String version = artifact.getVersion();
61          return version != null
62                  && version.startsWith("2.")
63                  && artifact.getArtifactId().startsWith("maven-")
64                  && artifact.getGroupId().equals("org.apache.maven");
65      }
66  
67      private boolean isWagonProvider(Artifact artifact) {
68          if ("org.apache.maven.wagon".equals(artifact.getGroupId())) {
69              return artifact.getArtifactId().startsWith("wagon-");
70          }
71          return false;
72      }
73  
74      @Override
75      public boolean equals(Object obj) {
76          if (obj == this) {
77              return true;
78          } else if (obj == null || !getClass().equals(obj.getClass())) {
79              return false;
80          }
81  
82          WagonExcluder that = (WagonExcluder) obj;
83          return coreArtifact == that.coreArtifact;
84      }
85  
86      @Override
87      public int hashCode() {
88          int hash = getClass().hashCode();
89          hash = hash * 31 + (coreArtifact ? 1 : 0);
90          return hash;
91      }
92  }