Reputation: 845
I wrote a plugin which should execute at compile phase. It generates some source code which is being used by other Java classes.
When I normally add this plugin in my POM, I face a compilation error because Maven first execute the compiler plugin and then my plugin. So while compiling, it fails because it need the source code which is generated by my own plugin.
How do I resolve this problem?
Upvotes: 3
Views: 3947
Reputation: 137084
The fix is to invoke your plugin before the compilation of the sources. Compilation, as done by maven-compiler-plugin:compile
, happens by default in the compile
phase of the default lifecycle.
Before that compile
phase, the default lifecycle also invokes the generate-sources
, which purpose is to:
generate any source code for inclusion in compilation.
Therefore, you should bind your plugin to the phase generate-sources
instead of the compile
phase. This can either be done using the defaultPhase
attribute of your MOJO with
@Mojo(name = "example", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
or declaring it explicitely in the POM in its executions:
<execution>
<phase>generate-sources</phase>
<!-- id, goal and configuration -->
</execution>
You'll need to make sure the classes you generated in that phase are correctly added to the buildpath. If the plugin doesn't do it already (by calling MavenProject.addCompileSourceRoot(directory)
), you can make use of the build-helper-maven-plugin:add-source
goal to add the directory where the sources have been generated to the buildpath.
Upvotes: 2