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