您的插件应该分阶段运行generate-sources
。然后您必须确保生成的源在正常compile
阶段可用。
您的插件应配置如下:
<build>
<plugins>
<plugin>
<groupId>your.group</groupId>
<artifactId>your-generator-plugin</artifactId>
<version>your-generator-version</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>your-generator-goal</goal>
</goals>
<configuration>
<!-- Here goes all the plugin configuration -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
你可以直接从你的插件(mojo)中添加编译源路径来使用。
以下是您必须添加到 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;
并且必须将这样的内容添加到您的execute()
方法中:
if (!settings.isInteractiveMode()) {
LOG.info("Adding " + outputDir.getAbsolutePath() + " to compile source root");
}
project.addCompileSourceRoot(outputDir.getAbsolutePath());
This will output the generated java source files into target/src-generated/
but that can either be changed to some other default value in your mojo OR by adding this to the configuration part of the plugin:
<outputDir>path/to/my/generated/source/</outputDir>
The generated java source files will automatically be included in the compile
phase.