View Javadoc
1   package org.apache.maven.plugin.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.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
29  import org.apache.maven.plugin.dependency.fromDependencies.AbstractDependencyFilterMojo;
30  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.project.MavenProjectBuilder;
35  import org.apache.maven.project.ProjectBuildingException;
36  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
37  import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
38  import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
39  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
40  import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
41  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
42  import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
43  
44  /**
45   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
46   * @version $Id: AbstractResolveMojo.html 937155 2015-01-21 21:53:50Z khmarbaise $
47   */
48  public abstract class AbstractResolveMojo
49      extends AbstractDependencyFilterMojo
50  {
51      /**
52       * Project builder -- builds a model from a pom.xml
53       */
54      @Component
55      protected MavenProjectBuilder mavenProjectBuilder;
56  
57      /**
58       * If specified, this parameter will cause the dependencies to be written to the path specified, instead of writing
59       * to the console.
60       *
61       * @since 2.0
62       */
63      @Parameter( property = "outputFile" )
64      protected File outputFile;
65  
66      /**
67       * This method resolves the dependency artifacts from the project.
68       *
69       * @param theProject
70       *            The POM.
71       * @return resolved set of dependency artifacts.
72       *
73       * @throws ArtifactResolutionException
74       * @throws ArtifactNotFoundException
75       * @throws InvalidDependencyVersionException
76       */
77  
78      /**
79       * Whether to append outputs into the output file or overwrite it.
80       *
81       * @since 2.2
82       */
83      @Parameter( property = "appendOutput", defaultValue = "false" )
84      protected boolean appendOutput;
85  
86      /**
87       * Don't resolve plugins that are in the current reactor. 
88       * Only works for plugins at the moment.
89       * 
90       * @since 2.7
91       */
92      @Parameter( property = "excludeReactor", defaultValue = "true" )
93      protected boolean excludeReactor;
94  
95      /**
96       * <i>not used in this goal</i>
97       */
98      @Parameter
99      protected boolean useJvmChmod = true;
100 
101     /**
102      * <i>not used in this goal</i>
103      */
104     @Parameter
105     protected boolean ignorePermissions;
106 
107     protected FilterArtifacts getPluginArtifactsFilter()
108     {
109         if ( excludeReactor )
110         {
111             final StringBuilder exAids = new StringBuilder();
112             if ( this.excludeArtifactIds != null )
113             {
114                 exAids.append( this.excludeArtifactIds );
115             }
116 
117             for ( final MavenProject rp : reactorProjects )
118             {
119                 if ( !"maven-plugin".equals( rp.getPackaging() ) )
120                 {
121                     continue;
122                 }
123 
124                 if ( exAids.length() > 0 )
125                 {
126                     exAids.append( "," );
127                 }
128 
129                 exAids.append( rp.getArtifactId() );
130             }
131 
132             this.excludeArtifactIds = exAids.toString();
133         }
134 
135         final FilterArtifacts filter = new FilterArtifacts();
136 
137         filter.addFilter( new ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
138                                            DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
139 
140         filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
141                                           DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
142 
143         filter.addFilter( new ClassifierFilter( DependencyUtil.cleanToBeTokenizedString( this.includeClassifiers ),
144                                                 DependencyUtil.cleanToBeTokenizedString( this.excludeClassifiers ) ) );
145 
146         filter.addFilter( new GroupIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeGroupIds ),
147                                              DependencyUtil.cleanToBeTokenizedString( this.excludeGroupIds ) ) );
148 
149         filter.addFilter( new ArtifactIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeArtifactIds ),
150                                                 DependencyUtil.cleanToBeTokenizedString( this.excludeArtifactIds ) ) );
151 
152         return filter;
153     }
154 
155     protected Set<Artifact> resolveDependencyArtifacts( final MavenProject theProject )
156         throws ArtifactResolutionException, ArtifactNotFoundException, InvalidDependencyVersionException
157     {
158         final Set<Artifact> artifacts =
159             theProject.createArtifacts( this.factory, Artifact.SCOPE_TEST,
160                                         new ScopeArtifactFilter( Artifact.SCOPE_TEST ) );
161 
162         for ( final Artifact artifact : artifacts )
163         {
164             // resolve the new artifact
165             this.resolver.resolve( artifact, this.remoteRepos, this.getLocal() );
166         }
167         return artifacts;
168     }
169 
170     /**
171      * This method resolves all transitive dependencies of an artifact.
172      *
173      * @param artifact the artifact used to retrieve dependencies
174      * @return resolved set of dependencies
175      * @throws ArtifactResolutionException
176      * @throws ArtifactNotFoundException
177      * @throws ProjectBuildingException
178      * @throws InvalidDependencyVersionException
179      *
180      */
181     protected Set<Artifact> resolveArtifactDependencies( final Artifact artifact )
182         throws ArtifactResolutionException, ArtifactNotFoundException, ProjectBuildingException,
183         InvalidDependencyVersionException
184     {
185         final Artifact pomArtifact =
186             this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "",
187                                          "pom" );
188 
189         final MavenProject pomProject =
190             mavenProjectBuilder.buildFromRepository( pomArtifact, this.remoteRepos, this.getLocal() );
191 
192         return resolveDependencyArtifacts( pomProject );
193     }
194 }