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.ProjectBuildingRequest;
31  import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
32  import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
33  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
34  import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
35  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
36  import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
37  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
38  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
39  import org.apache.maven.shared.transfer.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.
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 getArtifactsFilter()
98      {
99          final FilterArtifacts filter = new FilterArtifacts();
100 
101         if ( excludeReactor )
102         {
103 
104             filter.addFilter( new ExcludeReactorProjectsArtifactFilter( reactorProjects, getLog() ) );
105 
106         }
107 
108         filter.addFilter( new ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
109                                            DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
110 
111         filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
112                                           DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
113 
114         filter.addFilter( new ClassifierFilter( DependencyUtil.cleanToBeTokenizedString( this.includeClassifiers ),
115                                                 DependencyUtil.cleanToBeTokenizedString( this.excludeClassifiers ) ) );
116 
117         filter.addFilter( new GroupIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeGroupIds ),
118                                              DependencyUtil.cleanToBeTokenizedString( this.excludeGroupIds ) ) );
119 
120         filter.addFilter( new ArtifactIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeArtifactIds ),
121                                                 DependencyUtil.cleanToBeTokenizedString( this.excludeArtifactIds ) ) );
122 
123         return filter;
124     }
125 
126     /**
127      * This method resolves all transitive dependencies of an artifact.
128      *
129      * @param artifact the artifact used to retrieve dependencies
130      * @return resolved set of dependencies
131      * @throws DependencyResolverException in case of error while resolving artifacts.
132      */
133     protected Set<Artifact> resolveArtifactDependencies( final DependableCoordinate artifact )
134         throws DependencyResolverException
135     {
136         ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
137 
138         Iterable<ArtifactResult> artifactResults =
139             getDependencyResolver().resolveDependencies( buildingRequest, artifact, null );
140 
141         Set<Artifact> artifacts = new LinkedHashSet<>();
142 
143         for ( final ArtifactResult artifactResult : artifactResults )
144         {
145             artifacts.add( artifactResult.getArtifact() );
146         }
147 
148         return artifacts;
149 
150     }
151 }