Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I organize source-generation in Maven?

I have a Maven plugin which generates sources for my java project and places them into 'target/generated-sources' folder. I need these sources at compile-time. I want to have them in my project while modifying it, already generated. And of course, I want to put them into the correct folder under 'src' folder, and not into 'target/generated-sources'. How can I organize this using Maven?

In other words, I want to be able:

  1. generate sources I need by running some goal of my special source-generating plugin (already done, the sources have the package I specified)

  2. move these generated sources to 'src/main/java/...' folder of standart Maven layout.

  3. remove them from 'target/generated-sources' folder, because otherwise mvn clean install command raises error which says that I have "duplicate class". Indeed, if I just copy generated sources from target to src - I have to classes with the same name and package, though one of them is located in target folder.

Which Maven plugins can hlp with this? I suppose this is a typical task.

like image 453
MiamiBeach Avatar asked Jan 24 '14 19:01

MiamiBeach


1 Answers

The standard solution in maven is to generate all source into target/generated-sources, all source code from target/generated-sources and from src is compiled into target/classes and merged into target jar.

Plugin should never touch files under src directory, because these files are managed by version control system (ex. git).

If plugin is badly written and source files from target/generated-sources is not automatically compilled into target, then use goal build-helper:add-source from Build Helper Maven Plugin as @James Kingsbery said.

In maven-com4j-plugin source code there are comments:

/**
   * Directory in which to create the Java COM wrapper files. This directory
   * will be added to the Maven project's compile source directory list and
   * will therfore be auto-compiled when the Maven compile phase is run.
   * 
   * @parameter expression="${outputDirectory}"
   *            default-value="${project.build.directory}/generated-sources/com4j/java"
   */

The more important part and solution to your problem is:

This directory will be added to the Maven project's compile source directory list and will therfore be auto-compiled when the Maven compile phase is run

So, generated source code should be automatically compiled and archived into builded jar.

like image 152
MariuszS Avatar answered Sep 19 '22 18:09

MariuszS