4

I have authored a mojo that generates code and sticks it under {root}/target/generated-sources/foo. When I execute:

mvn clean install

I get errors indicating that the generated sources are not being included in the build path (the generated files are there, but not being picked up in the compile phase). I understand from this answer that I need to dynamically add {root}/target/generated-sources/foo as a source directory for the POM. Problem is, I haven't been able to track down any information on how to do this.

As a backup plan, I intend to use the Build Helper Maven Plugin, but I was hoping to do this automatically in my mojo if possible.

Community
  • 1
  • 1
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393

1 Answers1

2

I prefer to add this to my Mojo:

/**
  * The current project representation.
  * @parameter expression="${project}"
  * @required
  * @readonly
  */
 private MavenProject project;

/**
 * Directory wherein generated source will be put; main, test, site, ... will be added implictly.
 * @parameter expression="${outputDir}" default-value="${project.build.directory}/src-generated"
 * @required
 */
private File outputDir;

Obviously you can change the default-value to match your own pattern.

And then in the execute() method:

if (!settings.isInteractiveMode()) {
    LOG.info("Adding " + outputDir.getAbsolutePath() + " to compile source root");
}
project.addCompileSourceRoot(outputDir.getAbsolutePath());
maba
  • 47,113
  • 10
  • 108
  • 118
  • Thanks. I already have the first bit (ie. an output location property). It is the second bit I'm trying to get working - adding the compile source root. `project` is undefined for me. Looking into it to see if it's a maven version thing, or whatever. But if you have any ideas, please let me know. – Kent Boogaart Aug 13 '12 at 09:51
  • @KentBoogaart Added the `MavenProperty` property that will be automatically picked up and ready to be used. – maba Aug 13 '12 at 09:52