There is one very convenient solution - use the Eclipse Java Compiler (EJC) instead of the standard Oracle javac! One of the advantages of ECJ over javac is that it is error-prone, it tries to compile as much as possible and saves the already generated class files. The EJC was designed for use in the IDE for highly interactive work, where partial compilation is required, but it can also be used as a CLI or Maven plugin. Plexus guys provide EJC as a convenient dependency on Maven.
Compilers connect to Maven. You can have several compilations (compilation) defined in one POM, and you can use different compilers for each, giving the developer a wide range of options.
POM code example:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>pre-compilation</id> <phase>generate-sources</phase> <goals> <goal>compile</goal> </goals> <configuration> <compilerId>eclipse</compilerId> <failOnError>false</failOnError> </configuration> </execution> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <useIncrementalCompilation>false</useIncrementalCompilation> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-compiler-eclipse</artifactId> <version>2.3</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.eclipse.xtext</groupId> <artifactId>xtext-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <id>generate-the-stuff</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
Credits to Gabriel Axel for his article http://www.gabiaxel.com/2011/10/replacing-javac-with-eclipse-compiler.html
This approach can solve some complex “sources for the generated sources into the original” circular dependencies, potentially unsolvable by separation into separate modules.
In addition, I wanted to integrate source generation in the most transparent way. The need to rearrange the code based on dependencies on the generated sources will ultimately violate it. I want to group my code based on logical design, not because of technical features.
If the code generator works with xtext, as in my case, and you use the xtext-maven-plugin, say, the recently released 2.5.0, you do not need to configure anything, as in the above example, the plugin is exactly under the hood.
source share