Ram C Ram C - 1 year ago 85
Java Question

java standalone code that generates set of java files - how to do it in maven

I have a standalone java project that uses a struct definition file to generate a set of java files to output directory. All java source files have the same package name. Now I want to do that in Maven. Is there an option in maven plugin to generate the source and then build the jar eliminating all source files that was used to generate this source?

Answer Source

The idiomatic way would be to create a Maven plugin that runs the Java code that generates the Java source files.

It doesn't take too much work to create a maven plugin. Create a new maven project with a packaging of maven-plugin add the code generator as a dependency of the plugin. Implement a mojo that finds your struct definition file and runs your code generator. Bind the plugin goal to the generate-source phase and output the generated code to target/generated-sources/a-name. The plugin can add this directory as a source root to compile on the MavenProject object and the compiler plugin with compile the code.

You can then add your plugin to the Maven project that you want to generate the code for. Maven will run it in the generate-sources phase, compile the output in the compile phase and put the class files in a jar in the package phase.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download