Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

This quick tutorial introduces the compiler plugin, one of the core plugins of the Maven build tool.

For an overview of the other core plugins, refer to this article.

2. Plugin Goals

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

Here’s the compiler plugin in the POM:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.12.1</version>
    <configuration>
        ...
    </configuration>
</plugin>

We can find the latest version of this plugin here.

3. Configuration

By default, the compiler plugin compiles source code compatible with Java 5, and the generated classes also work with Java 5 regardless of the JDK in use. We can modify these settings in the configuration element:

<configuration>
    <source>1.8</source>
    <target>1.8</target>
    <-- other customizations -->
</configuration>

For convenience, we can set the Java version as properties of the POM:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Sometimes we want to pass arguments to the javac compiler. This is where the compilerArgs parameter comes in handy.

For instance, we can specify the following configuration for the compiler to warn about unchecked operations:

<configuration>
    <!-- other configuration -->
    <compilerArgs>
        <arg>-Xlint:unchecked</arg>
    </compilerArgs>
</configuration>

When compiling this class:

public class Data {
    List<String> textList = new ArrayList();

    public void addText(String text) {
        textList.add(text);
    }

    public List getTextList() {
        return this.textList;
    }
}

we’ll see an unchecked warning on the console:

[WARNING] ... Data.java:[7,29] unchecked conversion
  required: java.util.List<java.lang.String>
  found:    java.util.ArrayList

As both goals of the compiler plugin are automatically bound to phases in the Maven default lifecycle, we can execute these goals with the commands mvn compile and mvn test-compile.

4. Java 9 Updates

4.1. Configuration

Until Java 8, we used the version number as 1.x where x represents Java’s version, like 1.8 for Java 8.

For Java 9 and above, we can just use the version number directly:

<configuration>
    <source>9</source>
    <target>9</target>
</configuration>

Similarly, we can define the version using properties as:

<properties>
    <maven.compiler.source>9</maven.compiler.source>
    <maven.compiler.target>9</maven.compiler.target>
</properties>

Maven added its support for Java 9 in 3.5.0, so we’ll need at least that version. We’ll also need at least 3.8.0 of the maven-compiler-plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.12.1</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

4.2. Build

Now it’s time to test our configuration.

First, let’s create a MavenCompilerPlugin class in which we’re importing a package from another module.

A simple one is javax.xml.XMLConstants.XML_NS_PREFIX:

public class MavenCompilerPlugin {
    public static void main(String[] args) {
        System.out.println("The XML namespace prefix is: "
          + XML_NS_PREFIX);
    }
}

Next, let’s compile it:

mvn -q clean compile exec:java
  -Dexec.mainClass="com.baeldung.maven.java9.MavenCompilerPlugin"

When using Java 9 defaults, though, we’ll get an error:

[ERROR] COMPILATION ERROR :
[ERROR] .../MavenCompilerPlugin.java:[3,20]
  package javax.xml is not visible
  (package javax.xml is declared in module java.xml,
  but module com.baeldung.maven.java9 does not read it)
[ERROR] .../MavenCompilerPlugin.java:[3,1]
  static import only from classes and interfaces
[ERROR] .../MavenCompilerPlugin.java:[7,62]
  cannot find symbol
symbol:   variable XML_NS_PREFIX
location: class com.baeldung.maven.java9.MavenCompilerPlugin

The error comes from the fact that this package is in a separate module that we haven’t included yet in our build.

The simplest way to solve this is by creating a module-info.java class and indicating that we require the java.xml module:

module com.baeldung.maven.java9 {
    requires java.xml;
}

Now we can try again:

mvn -q clean compile exec:java
  -Dexec.mainClass="com.baeldung.maven.java9.MavenCompilerPlugin"

And our output will be:

The XML namespace prefix is: xml

5. Conclusion

In this article, we went over the compiler plugin and described how to use it. We also learned about Maven’s support for Java 9.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Next »
Quick Guide to the Maven Install Plugin
« Previous
Maven Resources Plugin
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW