1 | |
package org.apache.maven.plugin.assembly.utils; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.project.MavenProject; |
24 | |
import org.codehaus.plexus.logging.Logger; |
25 | |
|
26 | |
import java.io.File; |
27 | |
import java.io.IOException; |
28 | |
import java.util.Collections; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.LinkedHashSet; |
31 | |
import java.util.List; |
32 | |
import java.util.Set; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public final class ProjectUtils |
38 | |
{ |
39 | |
|
40 | |
private ProjectUtils() |
41 | 0 | { |
42 | 0 | } |
43 | |
|
44 | |
public static String getClassifier( Artifact artifact ) |
45 | |
{ |
46 | 141 | String classifier = artifact.getClassifier(); |
47 | 141 | if ( classifier != null && classifier.length() == 0 ) |
48 | |
{ |
49 | 3 | classifier = null; |
50 | |
} |
51 | 141 | return classifier; |
52 | |
} |
53 | |
|
54 | |
public static Set<MavenProject> getProjectModules( final MavenProject project, |
55 | |
final List<MavenProject> reactorProjects, |
56 | |
final boolean includeSubModules, final Logger logger ) |
57 | |
throws IOException |
58 | |
{ |
59 | 39 | final Set<MavenProject> singleParentSet = Collections.singleton( project ); |
60 | |
|
61 | 39 | final Set<MavenProject> moduleCandidates = new LinkedHashSet<MavenProject>( reactorProjects ); |
62 | |
|
63 | 39 | final Set<MavenProject> modules = new LinkedHashSet<MavenProject>(); |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 39 | modules.add( project ); |
72 | |
|
73 | 39 | int changed = 0; |
74 | |
|
75 | |
do |
76 | |
{ |
77 | 66 | changed = 0; |
78 | |
|
79 | 66 | for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator(); candidateIterator.hasNext(); ) |
80 | |
{ |
81 | 141 | final MavenProject moduleCandidate = candidateIterator.next(); |
82 | |
|
83 | 141 | if ( moduleCandidate.getFile() == null ) |
84 | |
{ |
85 | 3 | logger.warn( "Cannot compute whether " + moduleCandidate.getId() + " is a module of: " |
86 | |
+ project.getId() |
87 | |
+ "; it does not have an associated POM file on the local filesystem." ); |
88 | 3 | continue; |
89 | |
} |
90 | |
|
91 | |
Set<MavenProject> currentPotentialParents; |
92 | 138 | if ( includeSubModules ) |
93 | |
{ |
94 | 93 | currentPotentialParents = new LinkedHashSet<MavenProject>( modules ); |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 45 | currentPotentialParents = singleParentSet; |
99 | |
} |
100 | |
|
101 | 138 | for ( final Iterator<MavenProject> parentIterator = currentPotentialParents.iterator(); parentIterator.hasNext(); ) |
102 | |
{ |
103 | 216 | final MavenProject potentialParent = parentIterator.next(); |
104 | |
|
105 | 216 | if ( potentialParent.getFile() == null ) |
106 | |
{ |
107 | 0 | logger.warn( "Cannot use: " + moduleCandidate.getId() |
108 | |
+ " as a potential module-parent while computing the module set for: " |
109 | |
+ project.getId() |
110 | |
+ "; it does not have an associated POM file on the local filesystem." ); |
111 | 0 | continue; |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | 216 | if ( projectContainsModule( potentialParent, moduleCandidate ) ) |
118 | |
{ |
119 | |
|
120 | |
|
121 | 51 | modules.add( moduleCandidate ); |
122 | |
|
123 | |
|
124 | |
|
125 | 51 | candidateIterator.remove(); |
126 | |
|
127 | |
|
128 | |
|
129 | 51 | changed++; |
130 | |
} |
131 | 216 | } |
132 | 138 | } |
133 | |
} |
134 | 66 | while ( changed != 0 ); |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 39 | modules.remove( project ); |
140 | |
|
141 | 39 | return modules; |
142 | |
} |
143 | |
|
144 | |
private static boolean projectContainsModule( final MavenProject mainProject, final MavenProject moduleProject ) |
145 | |
throws IOException |
146 | |
{ |
147 | |
@SuppressWarnings( "unchecked" ) |
148 | 216 | final List<String> modules = mainProject.getModules(); |
149 | 216 | final File basedir = mainProject.getBasedir(); |
150 | |
|
151 | 216 | final File moduleFile = moduleProject.getFile() |
152 | |
.getCanonicalFile(); |
153 | |
|
154 | 216 | File moduleBasedir = moduleProject.getBasedir(); |
155 | |
|
156 | 216 | if ( moduleBasedir == null ) |
157 | |
{ |
158 | 9 | if ( moduleFile != null ) |
159 | |
{ |
160 | 9 | moduleBasedir = moduleFile.getParentFile(); |
161 | |
} |
162 | |
|
163 | 9 | if ( moduleBasedir == null ) |
164 | |
{ |
165 | 0 | moduleBasedir = new File( "." ); |
166 | |
} |
167 | |
} |
168 | |
|
169 | 216 | moduleBasedir = moduleBasedir.getCanonicalFile(); |
170 | |
|
171 | 216 | for ( final Iterator<String> it = modules.iterator(); it.hasNext(); ) |
172 | |
{ |
173 | 219 | final String moduleSubpath = it.next(); |
174 | |
|
175 | 219 | final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile(); |
176 | |
|
177 | 219 | if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) ) |
178 | |
{ |
179 | 51 | return true; |
180 | |
} |
181 | 168 | } |
182 | |
|
183 | 165 | return false; |
184 | |
} |
185 | |
|
186 | |
} |