How do I specify a dependency rule in Maven between files of certain suffixes?
-
18-09-2019 - |
Question
I have a Java program that generates Java classes for my application. Basically it takes in a simple spec for a class and generates a specialized form of Java bean. I want to integrate this into my Maven pom.xml so that if the input file is changed, Maven automatically generates the new .java file before the compile phase of Maven.
I know how to do this trivially in make but I didn't find anything in the Maven doc with this functionality.
Solution
You didn't provide much details on your code generation process but you could maybe simply invoke the code generator with the exec-maven-plugin (see the Examples section). The convention is to generate sources in ${project.build.directory}/generated-sources/<tool>
. Then add the generated sources with the build-helper-plugin and its add-sources
mojo. Bind every thing on the generate-sources
phase.
I'll just show the build-helper stuff below:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-mytool-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/mytool</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
You could also write a simple plugin to wrap your generator. In that case, have a look at the Guide to generating sources.
PS: I may have missed something, there is a kind of mismatch between my answer and the title of your question.
OTHER TIPS
Maven have phase "generate-sources" for this