Question

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.

Was it helpful?

Solution

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top