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