1 package org.apache.maven.plugins.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.slf4j.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 {
42 }
43
44 public static String getClassifier( Artifact artifact )
45 {
46 String classifier = artifact.getClassifier();
47 if ( classifier != null && classifier.length() == 0 )
48 {
49 classifier = null;
50 }
51 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 final Set<MavenProject> singleParentSet = Collections.singleton( project );
60
61 final Set<MavenProject> moduleCandidates = new LinkedHashSet<>( reactorProjects );
62
63 final Set<MavenProject> modules = new LinkedHashSet<>();
64
65
66
67
68
69
70
71 modules.add( project );
72
73 int changed;
74
75 do
76 {
77 changed = 0;
78
79 for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator();
80 candidateIterator.hasNext(); )
81 {
82 final MavenProject moduleCandidate = candidateIterator.next();
83
84 if ( moduleCandidate.getFile() == null )
85 {
86 logger.warn(
87 "Cannot compute whether " + moduleCandidate.getId() + " is a module of: " + project.getId()
88 + "; it does not have an associated POM file on the local filesystem." );
89 continue;
90 }
91
92 Set<MavenProject> currentPotentialParents;
93 if ( includeSubModules )
94 {
95 currentPotentialParents = new LinkedHashSet<>( modules );
96 }
97 else
98 {
99 currentPotentialParents = singleParentSet;
100 }
101
102 for ( final MavenProject potentialParent : currentPotentialParents )
103 {
104 if ( potentialParent.getFile() == null )
105 {
106 logger.warn( "Cannot use: " + moduleCandidate.getId()
107 + " as a potential module-parent while computing the module set for: "
108 + project.getId()
109 + "; it does not have an associated POM file on the local filesystem." );
110 continue;
111 }
112
113
114
115
116 if ( projectContainsModule( potentialParent, moduleCandidate ) )
117 {
118
119
120 modules.add( moduleCandidate );
121
122
123
124 candidateIterator.remove();
125
126
127
128 changed++;
129
130
131 break;
132 }
133 }
134 }
135 }
136 while ( changed != 0 );
137
138
139
140
141 modules.remove( project );
142
143 return modules;
144 }
145
146 private static boolean projectContainsModule( final MavenProject mainProject,
147 final MavenProject moduleProject )
148 throws IOException
149 {
150 final List<String> modules = mainProject.getModules();
151 final File basedir = mainProject.getBasedir();
152
153 final File moduleFile = moduleProject.getFile().getCanonicalFile();
154
155 File moduleBasedir = moduleProject.getBasedir();
156
157 if ( moduleBasedir == null )
158 {
159 moduleBasedir = moduleFile.getParentFile();
160
161 if ( moduleBasedir == null )
162 {
163 moduleBasedir = new File( "." );
164 }
165 }
166
167 moduleBasedir = moduleBasedir.getCanonicalFile();
168
169 for ( final String moduleSubpath : modules )
170 {
171 final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile();
172
173 if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) )
174 {
175 return true;
176 }
177 }
178
179 return false;
180 }
181
182 }