1 package org.apache.maven.plugin.dependency;
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 org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
24 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
25 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
26 import org.apache.maven.plugins.annotations.Component;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.project.MavenProjectBuilder;
30 import org.apache.maven.project.ProjectBuildingException;
31 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
32
33 import java.io.File;
34 import java.util.Set;
35
36 /**
37 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
38 * @version $Id: AbstractResolveMojo.java 1357251 2012-07-04 13:28:33Z olamy $
39 */
40 public abstract class AbstractResolveMojo
41 extends AbstractDependencyFilterMojo
42 {
43 /**
44 * Project builder -- builds a model from a pom.xml
45 */
46 @Component
47 protected MavenProjectBuilder mavenProjectBuilder;
48
49 /**
50 * If specified, this parameter will cause the dependencies to be written to the path specified, instead of writing
51 * to the console.
52 *
53 * @since 2.0
54 */
55 @Parameter( property = "outputFile" )
56 protected File outputFile;
57
58 /**
59 * This method resolves the dependency artifacts from the project.
60 *
61 * @param theProject
62 * The POM.
63 * @return resolved set of dependency artifacts.
64 *
65 * @throws ArtifactResolutionException
66 * @throws ArtifactNotFoundException
67 * @throws InvalidDependencyVersionException
68 */
69
70 /**
71 * Whether to append outputs into the output file or overwrite it.
72 *
73 * @since 2.2
74 */
75 @Parameter( property = "appendOutput", defaultValue = "false" )
76 protected boolean appendOutput;
77
78 protected Set<Artifact> resolveDependencyArtifacts( MavenProject theProject )
79 throws ArtifactResolutionException, ArtifactNotFoundException, InvalidDependencyVersionException
80 {
81 Set<Artifact> artifacts = theProject.createArtifacts( this.factory, Artifact.SCOPE_TEST,
82 new ScopeArtifactFilter( Artifact.SCOPE_TEST ) );
83
84 for ( Artifact artifact : artifacts )
85 {
86 // resolve the new artifact
87 this.resolver.resolve( artifact, this.remoteRepos, this.getLocal() );
88 }
89 return artifacts;
90 }
91
92 /**
93 * This method resolves all transitive dependencies of an artifact.
94 *
95 * @param artifact the artifact used to retrieve dependencies
96 * @return resolved set of dependencies
97 * @throws ArtifactResolutionException
98 * @throws ArtifactNotFoundException
99 * @throws ProjectBuildingException
100 * @throws InvalidDependencyVersionException
101 *
102 */
103 protected Set<Artifact> resolveArtifactDependencies( Artifact artifact )
104 throws ArtifactResolutionException, ArtifactNotFoundException, ProjectBuildingException,
105 InvalidDependencyVersionException
106 {
107 Artifact pomArtifact =
108 this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "",
109 "pom" );
110
111 MavenProject pomProject =
112 mavenProjectBuilder.buildFromRepository( pomArtifact, this.remoteRepos, this.getLocal() );
113
114 return resolveDependencyArtifacts( pomProject );
115 }
116 }