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