How to add maven compiler plugin in java project

This post talks about adding maven compiler plugin dependency to projects with pom.xml, Maven is an open-source Apache tool designed to automate the build process for Java-based projects

The maven-compiler-plugin is a fundamental plugin that every developer uses to compile the source code of a Maven project. By default, this plugin is not configured, and the compile goal is called as part of the Maven lifecycle process without explicit definition in the pom.xml.

If you wish to make specific configurations, add this plugin to the pom.xml.

The JDK provides a javac tool for compiling source code. Internally, this plugin utilizes the JDK javac tool to compile the code.

What is the Maven compiler plugin?

The Maven Compiler Plugin is a Maven plugin used to compile Java source code and generate classes. It is commonly referred to as the Maven Compiler.

This plugin includes two goals:

  • compile: Compiles the source files of a Maven application.
  • testCompile: Compiles the test files of a Maven app. You can also specify the version of Java used to compile the project.

What is the Maven compiler version?

The current latest Maven Compiler Plugin version is 3.12.1. You can specify the version using the version property within the dependency tag in the pom.xml. If the version tag is omitted, it defaults to the latest version.

How do I download a Maven compiler plugin?

The Maven Compiler Plugin can be downloaded either manually or automatically.

Manually, you can download either the source or binary file and copy it to the repository folder.

Another way is automatic. Once the plugin is configured in the pom.xml, it downloads the Maven Compiler for the first time to the local repository. Subsequently, it doesn’t download from the remote repository but uses the local cache repository.

What is the use of Maven compiler source and target?

The Maven Compiler compiles source files and generates class files for a Maven project. The source and target are configuration options that indicate the compatibility of the generated class files with Java versions

What is the latest version of the Maven compiler plugin?

maven compiler plugin’s latest version is 3.12.1. Add the dependency in pom.xml with the below values

  • groupId: org.apache.maven.plugins
  • artifactId:maven-compiler-plugin
  • version: 3.12.1
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.12.1</version>
    <configuration>
        <source>1.17</source>
        <target>1.17</target>
    </configuration>
</dependency>

How to change compiler options in pom.xml

In pom.xml, under the plugin tag, There is a configuration tag, It can be used to configure different configuration options.

<project>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <!-- put your configurations here -->
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

How to get the maven compiler Plugin help

The below maven commands provide helpful information about the compiler plugin

mvn compiler:help -Ddetail=true

maven compiler compile goal example

The Maven Compiler Plugin provides two goals:

  • compile goal: This executes the compiler:compile option to compile Java files as part of the compilation phase during the build process. It processes Java files in the src folder of the Maven project and generates the target/classes folder with .class files.
mvn compile
  • test-compile goal: This executes the compiler:testCompile option to compile test Java files as part of the test compilation phase during the testing process. It processes Java files in the test folder of the Maven project and generates the target/test/classes folder with .class files.
mvn test-compile

maven compiler Plugin Configuration Options

There are numerous configurations that we can change or override default values by adding different options to the tag. Below are the configurations a parameters you can configure

<build>
   <pluginManagement>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
            <!-- add Configuration options here
            - verbose
- fork
- executable
- compilerVersion -->
            </configuration>
         </plugin>
      </plugins>
   </pluginManagement>
</build>

Plugin configuration usage examples

we will see different configurations and examples

How to configure different Java paths and compiler versions for the plugin?

Modify the executable and compilerVersion tags in the configuration options according to your Java settings. Refer to the example below for more information

<configuration>
   <verbose>true</verbose>
   <fork>true</fork>
   <executable>${JAVA_HOME}/bin/javac</executable>
   <compilerVersion>1.8</compilerVersion>
</configuration>

Configure the Java compiler version in the plugin

The JDK javac tool includes source and target options to set JDK versions during compilation. This plugin also provides configurations for source and target options. By using these options, we can change the default compiler version to a new version in the pom.xml.

<configuration>
   <source>1.8</source>
   <target>1.8</target>
</configuration>

Passing arguments to the Maven compiler

Sometimes, it’s necessary to pass memory-related and garbage configurations to the compilation phase. This plugin provides a CompilerArgs tag to facilitate this.

<compilerArgs>
   <arg>-verbose</arg>
   <arg>-Xlint:all,-options,-path</arg>
</compilerArgs>

Enable encoding for source files in the compiler

We often receive a warning during the compilation process if the encoding is not set. This can be configured at either the compiler level or the project level.

Compiler encoding configuration

<configuration>
   <encoding>UTF-8</encoding>
</configuration>

maven project encoding : This is the root-level configuration that impacts all the modules of the project

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Disable this compiler plugin in the Maven application

By default, the 1 plugin runs without requiring configuration in the pom.xml. To disable it, make the following changes in the configuration section of the pom.xml

<execution>
   <id>default-compile</id>
   <phase>none</phase>
</execution>

How to change Custom Directory for source/target folder for compilation

By default, the location for Java files is src/main/java. You can override the input files using the following option.

<configuration>
   <include>src/javafolder</include>
</configuration>

By default, compiled source files, i.e., class files, will be written to the target/test/classes folder. To override the target folder with target/myclasses, you can use the following configuration.

<build>
   <outputDirectory>${project.build.directory}/myclasses</outputDirectory>
</build>

We can do it in two ways.

  • Using tree command:
mvn dependency:tree -Dverbose
  • Using verbose option in the configuration: By default, verbose is false, change it to true to get detailed information about this plugin.
<configuration>
   <verbose>true</verbose>
</configuration>

maven compiler plugin Errors

Let’s walk through some of the errors encountered during Maven compiler plugin usage.

  • Continue compilation when error/warning occurs

failOnError and failOnWarning options allow the compiler to continue compilation.

Errors will be shown once the compilation build process is completed.

  • javac Task: source release version requires target release version

It is a common error running maven projects during the application build process.

The error is due to mismatching Java version incompatibility.

If maven-compiler-plugin is not defined, Please add a plugin in pom.xml with the following information. Plugin property change via the below code:

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

This contains source and target are valid Java versions.

Or you can change via project properties tag

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

Change encode to UTF-8 in compiler plugin

If the encoding attribute is set to UTF-8 inside the Maven Compiler Plugin configuration, it means that the plugin is configured to use UTF-8 as the character encoding for the source files during compilation.

This is a common and recommended approach, especially for applications that want to support a wide range of characters and i18n support

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

        </plugins>
    </build>

Conclusion

You have learned how to add the Maven Compiler Plugin to Java applications, change the Java version in compiler options, modify the encoding, disable plugins, and use examples for the compile and test-compile goals.