简体   繁体   中英

Generating new sources via Maven plugin after compile phase

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

At the moment my steps are:

  1. execute first code generation plugin (during generate-sources )
  2. add directory of generated types to build path
  3. execute second code generation plugin (during compile )

However my problem is that anything generated by the second code generation plugin will not be compiled (because the compile phase has finished). If I attach the second code generation plugin to an earlier phase, it fails because it needs the classes from the first code generation plugin to be present on the classpath.

I know I could split this into two modules with one dependent on the other , but I was wondering if this could be achieved in one pom. It seems like a need a way to invoke compile again after the normal compile phase is complete.

Any ideas?

You can always configure two executions of the compiler plugin, both tied to the compile phase. In one you include the extra stuff:

<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 may try as well <includes><include>path/</include></includes>

According the official documentation :

When multiple executions are given that match a particular phase, they are executed in the order specified in the POM, with inherited executions running first.

But I quite not get what you exactly want. http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

The obvious solution (generate code after the compile phase) doesn't work since Maven doesn't allow to reorder the phases.

The correct solution is to use modules . You need two: The first module contains the 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 caught in some kind of loop (like "A" needs generated code which needs "A"). So your build will be more simple and you will spend less time hunting odd bugs.

[UPDATE] In my projects, I run the code generator from tests. Without a special option, the files are generated into the temp folder and compared to the sources. That allows me to see when I have unexpected changes in my generated code ( which I put under version control ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM