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