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