View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.resolvers;
20  
21  import java.io.File;
22  import java.util.LinkedHashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo;
28  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
31  import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
32  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
33  import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
34  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
35  import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
36  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
37  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
38  import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
39  
40  /**
41   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
42   */
43  public abstract class AbstractResolveMojo extends AbstractDependencyFilterMojo {
44      /**
45       * If specified, this parameter causes the dependencies to be written to the path specified instead of
46       * the console.
47       *
48       * @since 2.0
49       */
50      @Parameter(property = "outputFile")
51      protected File outputFile;
52  
53      /**
54       * Whether to append outputs into the output file or overwrite it.
55       *
56       * @since 2.2
57       */
58      @Parameter(property = "appendOutput", defaultValue = "false")
59      protected boolean appendOutput;
60  
61      /**
62       * Don't resolve plugins that are in the current reactor.
63       *
64       * @since 2.7
65       */
66      @Parameter(property = "excludeReactor", defaultValue = "true")
67      protected boolean excludeReactor;
68  
69      /**
70       * <i>not used in this goal</i>
71       */
72      @Parameter
73      protected boolean ignorePermissions;
74  
75      /**
76       * @return {@link FilterArtifacts}
77       */
78      protected FilterArtifacts getArtifactsFilter() {
79          final FilterArtifacts filter = new FilterArtifacts();
80  
81          if (excludeReactor) {
82  
83              filter.addFilter(new ExcludeReactorProjectsArtifactFilter(reactorProjects, getLog()));
84          }
85  
86          filter.addFilter(new ScopeFilter(
87                  DependencyUtil.cleanToBeTokenizedString(this.includeScope),
88                  DependencyUtil.cleanToBeTokenizedString(this.excludeScope)));
89  
90          filter.addFilter(new TypeFilter(
91                  DependencyUtil.cleanToBeTokenizedString(this.includeTypes),
92                  DependencyUtil.cleanToBeTokenizedString(this.excludeTypes)));
93  
94          filter.addFilter(new ClassifierFilter(
95                  DependencyUtil.cleanToBeTokenizedString(this.includeClassifiers),
96                  DependencyUtil.cleanToBeTokenizedString(this.excludeClassifiers)));
97  
98          filter.addFilter(new GroupIdFilter(
99                  DependencyUtil.cleanToBeTokenizedString(this.includeGroupIds),
100                 DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds)));
101 
102         filter.addFilter(new ArtifactIdFilter(
103                 DependencyUtil.cleanToBeTokenizedString(this.includeArtifactIds),
104                 DependencyUtil.cleanToBeTokenizedString(this.excludeArtifactIds)));
105 
106         return filter;
107     }
108 
109     /**
110      * This method resolves all transitive dependencies of an artifact.
111      *
112      * @param artifact the artifact used to retrieve dependencies
113      * @return resolved set of dependencies
114      * @throws DependencyResolverException in case of error while resolving artifacts.
115      */
116     protected Set<Artifact> resolveArtifactDependencies(final DependableCoordinate artifact)
117             throws DependencyResolverException {
118         ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
119 
120         Iterable<ArtifactResult> artifactResults =
121                 getDependencyResolver().resolveDependencies(buildingRequest, artifact, null);
122 
123         Set<Artifact> artifacts = new LinkedHashSet<>();
124 
125         for (final ArtifactResult artifactResult : artifactResults) {
126             artifacts.add(artifactResult.getArtifact());
127         }
128 
129         return artifacts;
130     }
131 }