The default config for JUnit tests in IntelliJ (v2016.3) is to run Build
before launch. Normally, this is a good thing as it ensures that all of the changes have been applied before running the tests. However, it seems to be deleting the compiled classes of generated code. As you might expect, this means that tests fail because the tests for the generated classes can no longer find the class they are trying to test!
How do I keep IntelliJ's default "Build before launch" behavior without having my compiled generated classes get blown away when running tests?
Now for some details.
I'm using the Immutables library (not Lombok), so it's code that's generated using the "normal" Java Annotations mechanisms. IntelliJ happily picks up the changes and generates the Java code via the annotations. It also will compile the updated generated Java files when they change. So, the general "build automatically" behavior seems to be working. To me, this seems to indicate that I have the Annotation processor stuff set up correctly.
When I manually run "Build module" or when "Build" runs before tests, however, the class files for the generated Java files are removed. The generated Java files are not changed in any way, but the class files that they compile into are removed. To me, this means that there's something with Build that isn't paying attention to Generated Source dependencies.
One thing I have noted is that if I don't mark the directory into which sources are being generated as a Generated Source Root, this "delete the class files of generated source files" behavior doesn't happen. The downside of this is that IntelliJ no longer recognizes the generated classes and marks them as unknown in code that uses them.
How I have IntelliJ configured:
Preferences -> Annotation Processors
is usingModule content root
,Obtain processors from project classpath
, and has the production and test sources directory fields specified (and seems to be respecting those when generating the source files)Preferences -> Compiler
is not usingClear output directory on rebuild
and is usingBuild project automatically
File -> Other Settings -> Annotation Processors
has the same configurations as indicated in the above two points- The directory the sources are being generated into is marked as a Generated Source directory
I've been bashing my head against this for far too long, trying everything I can think of and everything anyone even hints at, but to no avail. Any help for how to make this work (other than "remove the Build option from the test config") would be amazingly helpful and deeply appreciated.