View Javadoc
1   package org.apache.maven.plugins.dependency.resolvers;
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.io.File;
23  import java.util.LinkedHashSet;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo;
29  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.ProjectBuildingRequest;
32  import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
33  import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
34  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
35  import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
36  import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
37  import org.apache.maven.shared.artifact.resolve.ArtifactResult;
38  import org.apache.maven.shared.dependencies.DependableCoordinate;
39  import org.apache.maven.shared.dependencies.resolve.DependencyResolverException;
40  
41  /**
42   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
43   */
44  public abstract class AbstractResolveMojo
45      extends AbstractDependencyFilterMojo
46  {
47      /**
48       * If specified, this parameter will cause the dependencies to be written to the path specified, instead of writing
49       * to the console.
50       *
51       * @since 2.0
52       */
53      @Parameter( property = "outputFile" )
54      protected File outputFile;
55  
56      /**
57       * This method resolves the dependency artifacts from the project.
58       *
59       * @param theProject The POM.
60       * @return resolved set of dependency artifacts.
61       * @throws ArtifactResolutionException
62       * @throws ArtifactNotFoundException
63       * @throws InvalidDependencyVersionException
64       */
65  
66      /**
67       * Whether to append outputs into the output file or overwrite it.
68       *
69       * @since 2.2
70       */
71      @Parameter( property = "appendOutput", defaultValue = "false" )
72      protected boolean appendOutput;
73  
74      /**
75       * Don't resolve plugins that are in the current reactor. Only works for plugins at the moment.
76       *
77       * @since 2.7
78       */
79      @Parameter( property = "excludeReactor", defaultValue = "true" )
80      protected boolean excludeReactor;
81  
82      /**
83       * <i>not used in this goal</i>
84       */
85      @Parameter
86      protected boolean useJvmChmod = true;
87  
88      /**
89       * <i>not used in this goal</i>
90       */
91      @Parameter
92      protected boolean ignorePermissions;
93  
94      /**
95       * @return {@link FilterArtifacts}
96       */
97      protected FilterArtifacts getPluginArtifactsFilter()
98      {
99          if ( excludeReactor )
100         {
101             final StringBuilder exAids = new StringBuilder();
102             if ( this.excludeArtifactIds != null )
103             {
104                 exAids.append( this.excludeArtifactIds );
105             }
106 
107             for ( final MavenProject rp : reactorProjects )
108             {
109                 if ( !"maven-plugin".equals( rp.getPackaging() ) )
110                 {
111                     continue;
112                 }
113 
114                 if ( exAids.length() > 0 )
115                 {
116                     exAids.append( "," );
117                 }
118 
119                 exAids.append( rp.getArtifactId() );
120             }
121 
122             this.excludeArtifactIds = exAids.toString();
123         }
124 
125         final FilterArtifacts filter = new FilterArtifacts();
126 
127         //CHECKSTYLE_OFF: LineLength
128         filter.addFilter( new org.apache.maven.shared.artifact.filter.collection.ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
129                                                                                               DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
130         //CHECKSTYLE_ON: LineLength
131 
132         filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
133                                           DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
134 
135         filter.addFilter( new ClassifierFilter( DependencyUtil.cleanToBeTokenizedString( this.includeClassifiers ),
136                                                 DependencyUtil.cleanToBeTokenizedString( this.excludeClassifiers ) ) );
137 
138         filter.addFilter( new GroupIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeGroupIds ),
139                                              DependencyUtil.cleanToBeTokenizedString( this.excludeGroupIds ) ) );
140 
141         filter.addFilter( new ArtifactIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeArtifactIds ),
142                                                 DependencyUtil.cleanToBeTokenizedString( this.excludeArtifactIds ) ) );
143 
144         return filter;
145     }
146 
147     /**
148      * This method resolves all transitive dependencies of an artifact.
149      *
150      * @param artifact the artifact used to retrieve dependencies
151      * @return resolved set of dependencies
152      * @throws DependencyResolverException in case of error while resolving artifacts.
153      */
154     protected Set<Artifact> resolveArtifactDependencies( final DependableCoordinate artifact )
155         throws DependencyResolverException
156     {
157         ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
158 
159         Iterable<ArtifactResult> artifactResults =
160             getDependencyResolver().resolveDependencies( buildingRequest, artifact, null );
161 
162         Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
163 
164         for ( final ArtifactResult artifactResult : artifactResults )
165         {
166             artifacts.add( artifactResult.getArtifact() );
167         }
168 
169         return artifacts;
170 
171     }
172 }