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