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?
-
What you are trying to achieve is unclear, what do you mean by eliminating all source files? – Jun 23 '16 at 16:28
-
i want to build a jar file with java files that are generated by the standalone program. Currently in eclipse I run the main java file it generates source files in output directory then I use eclipse export option to create a jar file for generated java files. Now I want to do this in Maven instead of using eclipse command options. – Ram C Jun 23 '16 at 16:33
-
then you should google for yourself and not ask this question https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes – Acewin Jun 23 '16 at 16:41
-
Possible duplicate of [How can I create an executable JAR with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Acewin Jun 23 '16 at 16:41
-
Create a build with maven-exec plugin to run your generator, maven compiler then maven jar. Something like that – Jun 23 '16 at 17:08
-
used maven-exec plugin to generate the code in prepare-package phase. then built the jar with include phrase to specifically include the generated source. thanks @RC – Ram C Jun 23 '16 at 19:44
-
@RamC glad you did sort it out ;) – Jun 23 '16 at 20:38
1 Answers
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.
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

- 3,015
- 1
- 13
- 13