Creating new sources through the Maven plugin after the compilation phase

I have a Maven project in which I need to complete two steps of code generation. One generates some types of Java, then the second depends on these types of Java to generate some more code. Is there a way that both of these steps happen during my build?

Currently my steps are:

  • execute the first code generation plugin (during generate-sources )
  • add a directory of generated types to create a path
  • execute the second code generation plugin (during compile )

However, my problem is that everything that is generated by the second-generation code plugin will not be compiled (because the compilation phase is completed). If I attach the second code generation plugin to an earlier phase, it fails because it needs classes from the first code generation module that will be present in the classpath.

I know that I could break it down into two modules with one dependent on the other , but I was wondering if this could be done in one pom. It seems like a way is needed to call the compilation again after the completion of the normal compilation phase.

Any ideas?

+4
source share
2 answers

You can always tweak two compiler plugin executions that are tied to the compilation phase. In one, you include additional material:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <executions> <execution> <id>one</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration></configuration> </execution> <execution> <id>two</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument> </configuration> </execution> </executions> <plugin> 

You also try <includes><include>path/</include></includes>

According to the official documentation:

When several executions that correspond to a certain phase are specified, they are executed in the order specified in the POM, with inherited starts running.

But I donโ€™t understand what you really want. http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

+4
source

The obvious solution (code generation after the compilation phase) does not work, since Maven does not allow changing the phase order.

The correct solution is to use modules . You need two: the first module contains a code generator. In the second module, you can use the generator from the first module to generate something in the generate-sources phase.

The big advantage of this approach: you can never get into any kind of loop (for example, โ€œAโ€ needs the generated code that needs โ€œAโ€). This way, your build will be simpler and you will spend less time looking for odd bugs.

[UPDATE] In my projects, I run the code generator from the tests. Without a special option, files are created in the temp folder and compared with sources. This allows me to see when I unexpectedly changed my generated code ( which I installed under version control ).

When the system property is set, the source files are overwritten, and I can commit the changes to my VCS.

+3
source

Source: https://habr.com/ru/post/1334229/