35
35
import java .io .FileReader ;
36
36
import java .io .IOException ;
37
37
import java .io .InputStreamReader ;
38
+ import java .nio .file .Path ;
38
39
import java .nio .file .Paths ;
39
40
import java .util .ArrayList ;
40
41
import java .util .Collections ;
@@ -165,9 +166,20 @@ public List<File> getSourceDirectories() {
165
166
sourcePath = build .getSourceDirectory ();
166
167
}
167
168
if (sourcePath == null ) {
168
- sourcePath = Paths .get ("src/main/java" ).toString ();
169
+ sourcePath = getSourceDirectoryFromParent (getParentPom ());
170
+ if (sourcePath == null ) {
171
+ sourcePath = Paths .get ("src/main/java" ).toString ();
172
+ }
173
+ }
174
+ sourcePath = extractVariable (sourcePath );
175
+ Path path = Paths .get (sourcePath );
176
+
177
+ String absoluteSourcePath ;
178
+ if (path .isAbsolute ()) {
179
+ absoluteSourcePath = path .toString ();
180
+ } else {
181
+ absoluteSourcePath = Paths .get (directory .getAbsolutePath (), sourcePath ).toString ();
169
182
}
170
- String absoluteSourcePath = Paths .get (directory .getAbsolutePath (), sourcePath ).toString ();
171
183
File source = new File (absoluteSourcePath );
172
184
if (source .exists ()) {
173
185
output .add (source );
@@ -182,6 +194,28 @@ public List<File> getSourceDirectories() {
182
194
return output ;
183
195
}
184
196
197
+ /**
198
+ * Climbs the pom.xml hierarchy until a model is found in which
199
+ * a source directory is declared.
200
+ * @return the uninterpolated source directory declared in the nearest ancestor
201
+ */
202
+ private String getSourceDirectoryFromParent (SpoonPom parent ) {
203
+ if (parent == null ) {
204
+ return null ;
205
+ }
206
+ String sourcePath = null ;
207
+ Build build = parent .model .getBuild ();
208
+ if (build != null ) {
209
+ sourcePath = build .getSourceDirectory ();
210
+ if (sourcePath == null && parent .getParentPom () != null ) {
211
+ return getSourceDirectoryFromParent (parent .getParentPom ());
212
+ }
213
+ } else if (parent .getParentPom () != null ) {
214
+ return getSourceDirectoryFromParent (parent .getParentPom ());
215
+ }
216
+ return sourcePath ;
217
+ }
218
+
185
219
/**
186
220
* Get the list of test directories of the project
187
221
* @return the list of test directories
@@ -195,9 +229,20 @@ public List<File> getTestDirectories() {
195
229
sourcePath = build .getTestSourceDirectory ();
196
230
}
197
231
if (sourcePath == null ) {
198
- sourcePath = Paths .get ("src/test/java" ).toString ();
232
+ sourcePath = getTestSourceDirectoryFromParent (getParentPom ());
233
+ if (sourcePath == null ) {
234
+ sourcePath = Paths .get ("src/test/java" ).toString ();
235
+ }
236
+ }
237
+ sourcePath = extractVariable (sourcePath );
238
+ Path path = Paths .get (sourcePath );
239
+
240
+ String absoluteSourcePath ;
241
+ if (path .isAbsolute ()) {
242
+ absoluteSourcePath = path .toString ();
243
+ } else {
244
+ absoluteSourcePath = Paths .get (directory .getAbsolutePath (), sourcePath ).toString ();
199
245
}
200
- String absoluteSourcePath = Paths .get (directory .getAbsolutePath (), sourcePath ).toString ();
201
246
File source = new File (absoluteSourcePath );
202
247
if (source .exists ()) {
203
248
output .add (source );
@@ -212,6 +257,28 @@ public List<File> getTestDirectories() {
212
257
return output ;
213
258
}
214
259
260
+ /**
261
+ * Climbs the pom.xml hierarchy until a model is found in which
262
+ * a test source directory is declared.
263
+ * @return the uninterpolated test source directory declared in the nearest ancestor
264
+ */
265
+ private String getTestSourceDirectoryFromParent (SpoonPom parent ) {
266
+ if (parent == null ) {
267
+ return null ;
268
+ }
269
+ String sourcePath = null ;
270
+ Build build = parent .model .getBuild ();
271
+ if (build != null ) {
272
+ sourcePath = build .getTestSourceDirectory ();
273
+ if (sourcePath == null && parent .getParentPom () != null ) {
274
+ return getTestSourceDirectoryFromParent (parent .getParentPom ());
275
+ }
276
+ } else if (parent .getParentPom () != null ) {
277
+ return getTestSourceDirectoryFromParent (parent .getParentPom ());
278
+ }
279
+ return sourcePath ;
280
+ }
281
+
215
282
/**
216
283
* Get the list of classpath files generated by maven
217
284
* @return the list of classpath files
@@ -247,29 +314,31 @@ private String extractVariable(String value) {
247
314
}
248
315
249
316
/**
250
- * Get the value of a property
317
+ * Get the value of a property. Reference: https://maven.apache.org/ref/3.6.3/maven-model-builder/#Model_Interpolation
251
318
* @param key the key of the property
252
319
* @return the property value if key exists or null
253
320
*/
254
321
private String getProperty (String key ) {
255
- if ("project.version" .equals (key ) || "pom.version" .equals (key )) {
322
+ if ("project.version" .equals (key ) || "pom.version" . equals ( key ) || " version" .equals (key )) {
256
323
if (model .getVersion () != null ) {
257
324
return model .getVersion ();
258
325
} else if (model .getParent () != null ) {
259
326
return model .getParent ().getVersion ();
260
327
}
261
- } else if ("project.groupId" .equals (key ) || "pom.groupId" .equals (key )) {
328
+ } else if ("project.groupId" .equals (key ) || "pom.groupId" .equals (key ) || "groupId" . equals ( key ) ) {
262
329
if (model .getGroupId () != null ) {
263
330
return model .getGroupId ();
264
331
} else if (model .getParent () != null ) {
265
332
return model .getParent ().getGroupId ();
266
333
}
267
- } else if ("project.artifactId" .equals (key ) || "pom.artifactId" .equals (key )) {
334
+ } else if ("project.artifactId" .equals (key ) || "pom.artifactId" . equals ( key ) || " artifactId" .equals (key )) {
268
335
if (model .getArtifactId () != null ) {
269
336
return model .getArtifactId ();
270
337
} else if (model .getParent () != null ) {
271
338
return model .getParent ().getArtifactId ();
272
339
}
340
+ } else if ("project.basedir" .equals (key ) || "pom.basedir" .equals (key ) || "basedir" .equals (key )) {
341
+ return pomFile .getParent ();
273
342
}
274
343
String value = extractVariable (model .getProperties ().getProperty (key ));
275
344
if (value == null ) {
0 commit comments