- Use Case
- Usage
- Developing
- License
In case you want to create a new annotation processor, you can use the annotation @AnnotationProcessor, to automatically create the required javax.annotation.processing.Processor file in the META-INF/services/ directory at compile time.
❗ To use this properly, see how to import it into your Build System and integrate it into Java ❗
<dependency>
<groupId>io.github.nkaaf</groupId>
<artifactId>annotationprocessor</artifactId>
<version>1.0</version>
</dependency>
plugins {
id 'java-library'
}
dependencies {
compileOnly 'io.github.nkaaf:annotationprocessor:1.0'
annotationProcessor 'io.github.nkaaf:annotationprocessor:1.0'
}
Annotate your annotation processor with @AnnotationProcessor. The processor behind this annotation checks if your annotation processor is built conforming (JSP 269). You can either extend your processor with javax.annotation.processing.AbstractProcessor or directly implement it with javax.annotation.processing.Processor.
You only need to import the dependency with your build system.
You need to add the dependencies' module in the module-info.java of the desired module. In addition, you need to set it up in your build system (Maven/ Gradle).
Your module only needs to require the io.github.nkaaf.annotationprocessor module. It gets the module java.compiler automatically, so you don't need it additionally for your annotation processor.
module yourModule {
requires static io.github.nkaaf.annotationprocessor;
}
You need to add the dependency as an annotation path in maven-compiler-plugin.
<pluin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>JAVA_VERSION_GREATER_OR_EQUALS_9</release>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.github.nkaaf</groupId>
<artifactId>annotationprocessor</artifactId>
<version>1.0</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</pluin>
Your module must require the io.github.nkaaf.annotationprocessor module.
❗ It will NOT automatically get the java.compiler module (different behaviour than in Maven) ❗
module yourModule {
requires static io.github.nkaaf.annotationprocessor;
}
You also need to turn on module path inference and add the release flag.
❗ You cannot use JDK 9 because the release flag causes an error with Gradle. JDK 10 causes another error with Gradle, but the Java 10 release flag can be used with JDK 12 and later. See the error tickets linked in example. But you can use the release flag 11, and a JDK 11 ❗
java {
modularity.inferModulePath.set(true)
}
// < Gradle 6.6
compileJava {
options.compilerArgs.addAll(['--release', '11'])
}
// >= Gradle 6.6
compileJava {
options.release.set(11)
}
To ensure maximum compatibility, this project must be compiled with Java 9.
Most IDEs do not support multi-release jars properly. The problem is that the package and class names are identical. The IDEs cannot compile them, even though this mechanism is clearly defined in the Maven POM.
There is also a problem with the tests. There is no automatic way to change the JDKs, so that both versions of my annotation processor are tested. My solution is a bash script that compiles and tests the classes. It uses SDKMAN! and the Junit Jupiter Engine, which is imported by Maven. You can easily run the test script with bash from anywhere on your computer. If you do not have the required Java Libraries installed in the Maven folder, the script downloads them. This also applies to the JDKs with SDKMAN!. The mechanism for switching JDKs with SDKMAN! is not perfect for my purpose, because it depends on hardcoded Java versions. These can be deleted at any time in the lists of SDKMAN! without me notice it.
- 8.0.282-zulu
- 11.0.10-zulu
- 15.0.2-sapmchn
- 16.0.1-zulu
Also tested with following, at the moment not available, SDKMAN! JDKs:
- 12.0.2-sapmchn
- 13.0.2-sapmchn
- 14.0.2-sapmchn
- 15.0.2-zulu
- 3.8.1
Also tested with following SDKMAN! Maven Versions:
- 3.6.3
- JUnit Jupiter API 5.7.2
- JUnit Platform Console Standalone 1.7.2 (Used in testing script for command line support)
This Project is licensed under the GNU Lesser General Public License 2.1 or any later (LGPL 2.1).
This list includes only Libraries and tools that are explicit imported/used in this project.