Skip to content

Migration

Martin Desruisseaux edited this page Mar 22, 2025 · 12 revisions

Compiler plugin migration from Maven 3 to Maven 4

This page lists some usage patterns of Maven 3 which have changed in Maven 4.

Declaration of source directories

The Maven 3 elements are deprecated and should be replaced by the new <sources> element introduced in Maven 4.

Maven 3

<build>
  <sourceDirectory>src/main/java</sourceDirectory>
  <testSourceDirectory>src/test/java</testSourceDirectory>
</build>

Maven 4

<build>
  <sources>
    <source>
      <directory>src/main/java</directory>
    </source>
    <source>
      <scope>test</scope>
      <directory>src/test/java</directory>
    </source>
  </sources>
<build>

Note that the declaration of a <sources> element replaces the default values, hence the need to declare the source and test directories together. The new <source> element allows more flexibility such as specifying many directories, the include/exclude filters, the targeted Java release and more.

Declaration of many source directories

The external plugin used in Maven 3 is no longer needed and should be replaced by the build-in <sources> elements.

Maven 3

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>add-directory</id>
          <goals>
            <goal>add-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>src/extension/java</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Maven 4

<build>
  <sources>
    <source>
      <directory>src/main/java</directory>
    </source>
    <source>
      <directory>src/extension/java</directory>
    </source>
    <source>
      <scope>test</scope>
      <directory>src/test/java</directory>
    </source>
  </sources>
<build>

Reminder: because <sources> replaces the default values, these defaults need to be explicitly specified.

Include/exclude filters

The Maven 3 way to declare include/exclude filters is still supported, but should be replaced by the <sources> element when applicable. Those two ways are not strictly equivalent:

  • The Maven 4 way specifies filters independently for each source directory. These filters will be applied by all plugins that migrated to the Maven 4 API, not only the compiler plugin.
  • Conversely, the Maven 3 way specifies filters which will be applied only by the compiler plugin. However, these filters apply to all source directories.

Maven 3

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>**/Foo*.java</exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

Maven 4

<build>
  <sources>
    <source>
      <directory>src/main/java</directory>
      <excludes>
        <exclude>**/Foo*.java</exclude>
      </excludes>
    </source>
    <source>
      <scope>test</scope>
      <directory>src/test/java</directory>
    </source>
  </sources>
</build>
Clone this wiki locally