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  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo;
27  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
30  import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
31  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
32  import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
33  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
34  import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
35  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
36  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
37  import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
38  
39  /**
40   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
41   */
42  public abstract class AbstractResolveMojo extends AbstractDependencyFilterMojo {
43      /**
44       * If specified, this parameter causes the dependencies to be written to the path specified instead of
45       * the console.
46       *
47       * @since 2.0
48       */
49      @Parameter(property = "outputFile")
50      protected File outputFile;
51  
52      /**
53       * Whether to append outputs into the output file or overwrite it.
54       *
55       * @since 2.2
56       */
57      @Parameter(property = "appendOutput", defaultValue = "false")
58      protected boolean appendOutput;
59  
60      /**
61       * Don't resolve plugins that are in the current reactor.
62       *
63       * @since 2.7
64       */
65      @Parameter(property = "excludeReactor", defaultValue = "true")
66      protected boolean excludeReactor;
67  
68      /**
69       * <i>not used in this goal</i>
70       */
71      @Parameter
72      protected boolean useJvmChmod = true;
73  
74      /**
75       * <i>not used in this goal</i>
76       */
77      @Parameter
78      protected boolean ignorePermissions;
79  
80      /**
81       * @return {@link FilterArtifacts}
82       */
83      protected FilterArtifacts getArtifactsFilter() {
84          final FilterArtifacts filter = new FilterArtifacts();
85  
86          if (excludeReactor) {
87  
88              filter.addFilter(new ExcludeReactorProjectsArtifactFilter(reactorProjects, getLog()));
89          }
90  
91          filter.addFilter(new ScopeFilter(
92                  DependencyUtil.cleanToBeTokenizedString(this.includeScope),
93                  DependencyUtil.cleanToBeTokenizedString(this.excludeScope)));
94  
95          filter.addFilter(new TypeFilter(
96                  DependencyUtil.cleanToBeTokenizedString(this.includeTypes),
97                  DependencyUtil.cleanToBeTokenizedString(this.excludeTypes)));
98  
99          filter.addFilter(new ClassifierFilter(
100                 DependencyUtil.cleanToBeTokenizedString(this.includeClassifiers),
101                 DependencyUtil.cleanToBeTokenizedString(this.excludeClassifiers)));
102 
103         filter.addFilter(new GroupIdFilter(
104                 DependencyUtil.cleanToBeTokenizedString(this.includeGroupIds),
105                 DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds)));
106 
107         filter.addFilter(new ArtifactIdFilter(
108                 DependencyUtil.cleanToBeTokenizedString(this.includeArtifactIds),
109                 DependencyUtil.cleanToBeTokenizedString(this.excludeArtifactIds)));
110 
111         return filter;
112     }
113 
114     /**
115      * This method resolves all transitive dependencies of an artifact.
116      *
117      * @param artifact the artifact used to retrieve dependencies
118      * @return resolved set of dependencies
119      * @throws DependencyResolverException in case of error while resolving artifacts.
120      */
121     protected Set<Artifact> resolveArtifactDependencies(final DependableCoordinate artifact)
122             throws DependencyResolverException {
123         ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
124 
125         Iterable<ArtifactResult> artifactResults =
126                 getDependencyResolver().resolveDependencies(buildingRequest, artifact, null);
127 
128         Set<Artifact> artifacts = new LinkedHashSet<>();
129 
130         for (final ArtifactResult artifactResult : artifactResults) {
131             artifacts.add(artifactResult.getArtifact());
132         }
133 
134         return artifacts;
135     }
136 }