Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 727
joelittlejohn Avatar asked Jan 04 '11 15:01

joelittlejohn


People also ask

What does the Maven compiler plugin do?

The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle: compile – compile main source files. testCompile – compile test source files.

What is the difference between plugin and pluginManagement tags?

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.

Are Maven plugins reusable?

Plugins are the central feature of Maven that allow for the reuse of common build logic across multiple projects.

How does Maven compile work?

Given the lifecycle phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the integration tests, install the verified ...


1 Answers

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

like image 60
ssedano Avatar answered Sep 30 '22 03:09

ssedano