7

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.

MiamiBeach
  • 3,261
  • 6
  • 28
  • 54
  • It would be helpful to see relevant configuration of your pom.xml. – Behe Jan 24 '14 at 19:10
  • This plugin is developed by you? – MariuszS Jan 24 '14 at 19:12
  • 1
    It is not typical, because all generated stuff should go into the target folder. It is easy to clean generated files and also to separate what gets checked in to a source control system. – Henry Jan 24 '14 at 19:13
  • The plugin is com4j-maven-plugin. Its places generated files into target/generated-sources folder. – MiamiBeach Jan 24 '14 at 19:20
  • @Dymytry And this source code from `target/generated-sources` isn't compiled into `target/classes` automatically? – MariuszS Jan 24 '14 at 19:21

3 Answers3

8

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.

MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • Thank you! I had a similar question w.r.t whether `target/generated-sources` is the standard mechanism not just for annotation processors but for any generated source code. I was wanting for avro plugin generated sources & was able to look at the plugin code as you did to confirm that whatever dir specified via `outputDirectory` param of the plugin is added to the classpath & compiled and packaged. I was also able to see that the default value for this is `${project.build.directory}/generated-sources/avro`. Would you mind adding a link to the source for this in where you read this? – lmk Jun 27 '23 at 19:57
  • Can you please help share a link to the official maven documentation that mentions this aspect, i.e. that `target/generated-sources` is the standard mechanism not just for annotation processors but for any generated source code? I couldn't see that mentioned both [here](https://maven.apache.org/guides/mini/guide-generating-sources.html) & [here](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html). – lmk Jun 27 '23 at 21:58
0

If your plugin works correctly it will add the generated sources to the internal project and other plugins like the maven-compiler-plugin will pick it up and compile the generatd code.

Within your plugin code you can accomplish this by using something similar:

mavenProject.addCompileSourceRoot( getOutputDirectory().getAbsolutePath() );
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Can you please help share a link to the official maven documentation that mentions this aspect, i.e. that `target/generated-sources` is the standard mechanism not just for annotation processors but for any generated source code? I couldn't see that mentioned both [here](https://maven.apache.org/guides/mini/guide-generating-sources.html) & [here](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html). – lmk Jun 27 '23 at 20:16
0

You should have a look at the build helper plugin. It allows you to specify additional source directories (such as target/generated-sources). See also Usage of maven Build Helper Maven Plugin.

Community
  • 1
  • 1
James Kingsbery
  • 7,298
  • 2
  • 38
  • 67