Skip to content

Commit ec06c83

Browse files
committed
fixed gradleModuleRoot to dynamically determine the number of parent directories to traverse (variations caused by running the jar versus running the sources via your IDE)
1 parent d6b9aed commit ec06c83

File tree

1 file changed

+22
-5
lines changed
  • src/main/java/com/yourorganization/maven_sample

1 file changed

+22
-5
lines changed

src/main/java/com/yourorganization/maven_sample/Util.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,33 @@ public class Util {
1111
/**
1212
* Temporary addition until JavaParser itself contains a similar method.
1313
* This is almost identical to the maven equivalent at CodeGenerationUtils#mavenModuleRoot()
14-
*
14+
* <p>
1515
* Useful for locating source code within your Gradle project. Finds the classpath for the given class,
16-
* then backs up out of multiple directories, until reaching the directory containing build.gradle
17-
* "build/(test-)classes/java/main"
16+
* then backs up out of multiple directories, until reaching the directory containing build.gradle -
17+
* typically this is "build/(test-)classes/java/main" (four traversals)
18+
* or "build/libs/javaparser-gradle-sample-shadow-1.0-SNAPSHOT" (three)
1819
*/
1920
public static Path gradleModuleRoot(Class<?> c) {
20-
return CodeGenerationUtils
21+
String buildFileName = "build.gradle";
22+
23+
24+
Path other = Paths.get(".");
25+
Path normalize = CodeGenerationUtils
2126
.classLoaderRoot(c)
22-
.resolve(Paths.get("..", "..", "..", ".."))
27+
.resolve(other)
2328
.normalize();
29+
30+
// If it's not a directory, or if it is a directory and the build file isn't present, go up a level.
31+
while (!normalize.toFile().isDirectory() || !normalize.resolve(buildFileName).toFile().exists()) {
32+
System.out.println("source root not found at + " + normalize + ", trying next directory up");
33+
other = other.resolve("..");
34+
normalize = CodeGenerationUtils
35+
.classLoaderRoot(c)
36+
.resolve(other)
37+
.normalize();
38+
}
39+
40+
return normalize;
2441
}
2542

2643
}

0 commit comments

Comments
 (0)