Skip to content

Commit d6b9aed

Browse files
committed
tweaked the sample to have a gradle-specific "root directory" finder (almost identical to the existing maven equivalent)
1 parent 02401dd commit d6b9aed

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void main(String[] args) {
2424
// SourceRoot is a tool that read and writes Java files from packages on a certain root directory.
2525
// In this case the root directory is found by taking the root from the current Maven module,
2626
// with src/main/resources appended.
27-
SourceRoot sourceRoot = new SourceRoot(CodeGenerationUtils.mavenModuleRoot(LogicPositivizer.class).resolve("src/main/resources"));
27+
SourceRoot sourceRoot = new SourceRoot(Util.gradleModuleRoot(LogicPositivizer.class).resolve("src/main/resources"));
2828

2929
// Our sample is in the root of this directory, so no package name.
3030
CompilationUnit cu = sourceRoot.parse("", "Blabla.java");
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.yourorganization.maven_sample;
2+
3+
import com.github.javaparser.utils.CodeGenerationUtils;
4+
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
public class Util {
9+
10+
11+
/**
12+
* Temporary addition until JavaParser itself contains a similar method.
13+
* This is almost identical to the maven equivalent at CodeGenerationUtils#mavenModuleRoot()
14+
*
15+
* 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"
18+
*/
19+
public static Path gradleModuleRoot(Class<?> c) {
20+
return CodeGenerationUtils
21+
.classLoaderRoot(c)
22+
.resolve(Paths.get("..", "..", "..", ".."))
23+
.normalize();
24+
}
25+
26+
}

0 commit comments

Comments
 (0)