{

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 to automate the build process for Java-based projects.

maven-compiler-plugin is a basic plugin that every developer uses to compile the source code of the maven project. By default, this plugin is not configured.compile goal will be called as part of the maven lifecycle process without defining it in pom.xml.

If you want to do some configurations. add this plugin in pom.xml.

JDK provides a javac tool to compile the source code. This plugin internally calls JDK javac tool_ to compile the code.

maven compiler plugin 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>

Plugin help

The below maven commands provide helpful information about the compiler plugin

mvn compiler:help -Ddetail=true

Compiler goals usage

There are two goals available.
compile goal It executes the compiler:compile option to compile java files as part of the compilation phase during the build process.

mvn compile

test-compilegoal : It executes the compiler:testCompile option to compile test java files as part of the test compilation phase during the testing process.

mvn test-compile

Plugin configuration Options

There are a lot of configurations, we can change/override default values. We can add different options to the tag.

<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</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?

Change executable and compilerVersion tags in the configuration options as per your java settings.

Please see the below example 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

JDK javac tool has source and target options to set JDK versions during compilation.

This plugin also provides configuration for source and target options. Using these options, We can change the default compiler version with a new version in pom.xml.

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

Passing arguments to maven compiler

Sometimes, We need to pass memory-related and garbage configurations to the compilation phase. This plugin provides a CompilerArgs tag to do it.

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

Enable encoding for source files in the compiler

We used to get the warning during the compilation process if the encoding is not set.

We can do it 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 affects all the modules of the project

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

disable this compiler plugin

By default, the Compiler plugin runs without configuring it in pom.xml.

please make configuration changes in pom.xml to disable it.

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

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

By default, src/main/java is java files location. Input files can be overridden using the below option

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

Compiled source files i.e class files will be written to the target/test/classes folder by default.

To override the target folder with target/myclasses, We can use the below 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>

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 8 requires target release 1.8

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

It is 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 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

Inside plugin configuration, the encoding attribute is set to true.

 <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 learned how to add the maven compiler plugin to java applications How to change the java version in compiler options, change the encoding, disable plugins, compile and test-compile goals usage examples

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.