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