Skip to content

Incremental Compilation and Maven

Michael Karneim edited this page Apr 30, 2018 · 6 revisions

There is a known issue with Maven and annotation processors.

Repleatedly invoking mvn compile after small code changes is not always calling annotation processors correctly. Instead you are forced to do a mvn clean compile in order to make sure that the sources will be generated completely.

Since this is not what you want, you might want to disable the annotation processing in the maven-compiler-plugin and use the maven-processor-plugin instead:

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <compilerArgument>-proc:none</compilerArgument>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <version>2.2.4</version>
        <executions>
          <execution>
            <id>process</id>
            <goals>
              <goal>process</goal>
            </goals>
            <phase>process-sources</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

After this change it's sufficient to execute mvn compile in order to get incremental code generation.

EDIT: According to this post from elucash there is another option, which enables incremental compilation directly on the maven-compiler-plugin.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.3</version>
  <configuration>
    <compilerVersion>1.8</compilerVersion>
    <source>1.8</source>
    <target>1.8</target>
    <useIncrementalCompilation>true</useIncrementalCompilation>
  </configuration>
</plugin>