1 package org.codehaus.mojo.apt;
2
3 /*
4 * The MIT License
5 *
6 * Copyright 2006-2008 The Codehaus.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is furnished to do
13 * so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.DependencyResolutionRequiredException;
33 import org.apache.maven.model.Resource;
34 import org.apache.maven.project.MavenProject;
35
36 /**
37 * Provides utilities for working with Maven projects.
38 *
39 * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
40 * @version $Id: MavenProjectUtils.java 12564 2010-09-15 16:09:11Z mark $
41 */
42 public final class MavenProjectUtils
43 {
44 // constructors -----------------------------------------------------------
45
46 private MavenProjectUtils()
47 {
48 throw new AssertionError();
49 }
50
51 // public methods ---------------------------------------------------------
52
53 /**
54 * Gets a list of paths for the specified artifacts.
55 *
56 * @param project
57 * the project
58 * @param artifacts
59 * the artifacts to obtain paths for
60 * @return a list of <code>String</code> paths to the specified artifacts
61 * @throws DependencyResolutionRequiredException
62 * if an artifact cannot be found
63 */
64 public static List<String> getClasspathElements( MavenProject project, List<Artifact> artifacts )
65 throws DependencyResolutionRequiredException
66 {
67 // based on MavenProject.getCompileClasspathElements
68
69 List<String> list = new ArrayList<String>( artifacts.size() );
70
71 for ( Artifact artifact : artifacts )
72 {
73 if ( artifact.getArtifactHandler().isAddedToClasspath() )
74 {
75 // TODO: let the scope handler deal with this
76 if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() )
77 || Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) )
78 {
79 addArtifactPath( project, artifact, list );
80 }
81 }
82 }
83
84 return list;
85 }
86
87 /**
88 * Gets whether the specified list of resources contains a resource with the specified directory.
89 *
90 * @param resources the list of resources to examine
91 * @param directory the resource directory to look for
92 * @return {@code true} if the list of resources contains a resource with the specified directory
93 */
94 public static boolean containsDirectory( List<Resource> resources, String directory )
95 {
96 for ( Resource resource : resources )
97 {
98 if ( directory.equals( resource.getDirectory() ) )
99 {
100 return true;
101 }
102 }
103
104 return false;
105 }
106
107 // private methods --------------------------------------------------------
108
109 // copied from MavenProject.addArtifactPath
110 private static void addArtifactPath( MavenProject project, Artifact artifact, List<String> list )
111 throws DependencyResolutionRequiredException
112 {
113 String refId = getProjectReferenceId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
114 MavenProject refProject = (MavenProject) project.getProjectReferences().get( refId );
115
116 boolean projectDirFound = false;
117 if ( refProject != null )
118 {
119 if ( artifact.getType().equals( "test-jar" ) )
120 {
121 File testOutputDir = new File( refProject.getBuild().getTestOutputDirectory() );
122 if ( testOutputDir.exists() )
123 {
124 list.add( testOutputDir.getAbsolutePath() );
125 projectDirFound = true;
126 }
127 }
128 else
129 {
130 list.add( refProject.getBuild().getOutputDirectory() );
131 projectDirFound = true;
132 }
133 }
134 if ( !projectDirFound )
135 {
136 File file = artifact.getFile();
137 if ( file == null )
138 {
139 throw new DependencyResolutionRequiredException( artifact );
140 }
141 list.add( file.getPath() );
142 }
143 }
144
145 // copied from MavenProject.getProjectReferenceId
146 private static String getProjectReferenceId( String groupId, String artifactId, String version )
147 {
148 return groupId + ":" + artifactId + ":" + version;
149 }
150 }