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 | 47 | String classifier = artifact.getClassifier(); |
47 | 47 | if ( classifier != null && classifier.length() == 0 ) |
48 | |
{ |
49 | 1 | classifier = null; |
50 | |
} |
51 | 47 | 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 | 13 | final Set<MavenProject> singleParentSet = Collections.singleton( project ); |
60 | |
|
61 | 13 | final Set<MavenProject> moduleCandidates = new LinkedHashSet<MavenProject>( reactorProjects ); |
62 | |
|
63 | 13 | final Set<MavenProject> modules = new LinkedHashSet<MavenProject>(); |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 13 | modules.add( project ); |
72 | |
|
73 | 13 | int changed = 0; |
74 | |
|
75 | |
do |
76 | |
{ |
77 | 22 | changed = 0; |
78 | |
|
79 | 22 | for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator(); candidateIterator.hasNext(); ) |
80 | |
{ |
81 | 47 | final MavenProject moduleCandidate = candidateIterator.next(); |
82 | |
|
83 | 47 | if ( moduleCandidate.getFile() == null ) |
84 | |
{ |
85 | 1 | 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 | 1 | continue; |
89 | |
} |
90 | |
|
91 | |
Set<MavenProject> currentPotentialParents; |
92 | 46 | if ( includeSubModules ) |
93 | |
{ |
94 | 31 | currentPotentialParents = new LinkedHashSet<MavenProject>( modules ); |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 15 | currentPotentialParents = singleParentSet; |
99 | |
} |
100 | |
|
101 | 46 | for ( final Iterator<MavenProject> parentIterator = currentPotentialParents.iterator(); parentIterator.hasNext(); ) |
102 | |
{ |
103 | 72 | final MavenProject potentialParent = parentIterator.next(); |
104 | |
|
105 | 72 | 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 | 72 | if ( projectContainsModule( potentialParent, moduleCandidate ) ) |
118 | |
{ |
119 | |
|
120 | |
|
121 | 17 | modules.add( moduleCandidate ); |
122 | |
|
123 | |
|
124 | |
|
125 | 17 | candidateIterator.remove(); |
126 | |
|
127 | |
|
128 | |
|
129 | 17 | changed++; |
130 | |
} |
131 | 72 | } |
132 | 46 | } |
133 | |
} |
134 | 22 | while ( changed != 0 ); |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 13 | modules.remove( project ); |
140 | |
|
141 | 13 | return modules; |
142 | |
} |
143 | |
|
144 | |
private static boolean projectContainsModule( final MavenProject mainProject, final MavenProject moduleProject ) |
145 | |
throws IOException |
146 | |
{ |
147 | |
@SuppressWarnings( "unchecked" ) |
148 | 72 | final List<String> modules = mainProject.getModules(); |
149 | 72 | final File basedir = mainProject.getBasedir(); |
150 | |
|
151 | 72 | final File moduleFile = moduleProject.getFile() |
152 | |
.getCanonicalFile(); |
153 | |
|
154 | 72 | File moduleBasedir = moduleProject.getBasedir(); |
155 | |
|
156 | 72 | if ( moduleBasedir == null ) |
157 | |
{ |
158 | 3 | if ( moduleFile != null ) |
159 | |
{ |
160 | 3 | moduleBasedir = moduleFile.getParentFile(); |
161 | |
} |
162 | |
|
163 | 3 | if ( moduleBasedir == null ) |
164 | |
{ |
165 | 0 | moduleBasedir = new File( "." ); |
166 | |
} |
167 | |
} |
168 | |
|
169 | 72 | moduleBasedir = moduleBasedir.getCanonicalFile(); |
170 | |
|
171 | 72 | for ( final Iterator<String> it = modules.iterator(); it.hasNext(); ) |
172 | |
{ |
173 | 73 | final String moduleSubpath = it.next(); |
174 | |
|
175 | 73 | final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile(); |
176 | |
|
177 | 73 | if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) ) |
178 | |
{ |
179 | 17 | return true; |
180 | |
} |
181 | 56 | } |
182 | |
|
183 | 55 | return false; |
184 | |
} |
185 | |
|
186 | |
} |