Home » Java » java – maven-compiler-plugin: add extra dependency for incremental build-Exceptionshub

java – maven-compiler-plugin: add extra dependency for incremental build-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to construct a java class file with a class with a long, multi-line string constant. (The usual way of putting the string into a separate file and load it from the same JAR as the class is not an option. There won’t be a JAR, only that class file.) Originally, I was using the multiline-string annotation-processor, it allows to place the string content in a doc comment:

/**
 * This will be the actual
 * content of the string
**/
@Multiline static final String foo = "this literal is ignored";

Here I mistakenly thought: “oh, comments are inconvenient, why not change multiline-string to load the content from an external file?” It turns out that this works, but maven (which I’m using for building), does of course not understand that the class file needs to be recompiled if the file read by the annotation processor is changed.

Approaches:

  • set useIncrementalCompilation to false: Does not at all do what I would expect, instead disables dependency checking for incremental builds
  • add .py to fileExtensions (didn’t expect that to work)
  • add the maven-clean-plugin as part of the initialization phase: This works, but it is a bit overkill…

What I’d really like to do is to add the path of the embedded file to maven’s list of “if this file changes, recompile that file”. Is that possible, or is there another, elegant way of solving this?

How to&Answers:

Maybe this is not a full answer, but just my thoughts on the above:

Annotation processors in java usually are not intended for changing the existing classes.

Yes, there is Lombok, that kind of breaks this assumption, but the price is using some internal compiler internal APIs and “Manually” compiling the modified AST:

This is also written in this tutorial for example.

Usually annotation processors can create some descriptors, like JSON or files, or, alternatively new Java sources, but they never (again with the exception of lombok) modify the existing sources.

If I’ve got you correctly, you wrote your own annotation processor for multi-line handling like the one that you’ve provided in the first link, but this kind of incompatible with the idea of annotation processors, so I doubt maven can help you out here.