General idea: I want to build a simple annotation processor on Gradle and Java 17 (Gradle 7.5, Java 17.0.6). And use it in another project with the same setup.
here is my code:
annotation-processor project build.gradle:
plugins {
id 'java'
id 'maven-publish'
}
group 'org.simple.processor'
version '1.0-SNAPSHOT'
repositories {
mavenLocal()
}
publishing {
publications {
maven(MavenPublication) {
}
}
repositories {
mavenLocal()
}
}
compileJava {
options.annotationProcessorPath = configurations.annotationProcessor
}
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
tasks.withType(JavaCompile) {
options.compilerArgs += ["--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED"]
options.compilerArgs += ["--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED"]
options.compilerArgs += ["--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"]
options.compilerArgs += ["--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"]
}
annotation-processor project LocalVariableTranslator.class:
public class LocalVariableTranslator extends TreeTranslator {
@Override
public void visitClassDef(JCTree.JCClassDecl tree) {
// simply log something
}
}
annotation-processor-demo project build.gradle:
plugins {
id 'java'
}
group 'org.simple.processor'
version '1.0-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
annotationProcessor 'org.simple.processor:simple-processor:1.0-SNAPSHOT'
implementation 'org.simple.processor:simple-processor:1.0-SNAPSHOT'
}
my processor code somehow depends on internal Java API which is 'com.sun.tools.javac' package and this causes an issue for me when I try to build a demo project:
Execution failed for task ':compileJava'. superclass access check failed: class org.simple.processor.LocalVariableTranslator (in unnamed module @0x6073770b) cannot access class com.sun.tools.javac.tree.TreeTranslat or (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.tree to unnamed module @0x6073770b