View Javadoc

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