Other Tutorials

Guide to Maven Compiler Plugin

1. Introduction:

Maven Compiler plugin is one of the core plugins of Apache Maven. It helps us to compile the sources of our maven project. It uses javax.tools.JavaCompiler as its default compiler. If we wish to use javac as the compiler, we can set forceJavacCompilerUse option value to true.

2.Compiler Plugin Goals:

The Maven Compiler Plugin has two goals:

  1. compiler:compile: Compiles the main source files and is bound to the compile phase
  2. compiler:testCompile: Used for compiling the test source files and is bound to test compilation phase

Both of these goals are already bound to the Maven Lifecycle phases, so they’re automatically executed as part of the lifecycle. To compile our main source files, we can use:

mvn compile

To compile our test source files along with all the main source files, we can execute:

mvn test-compile

3. Configuring Compiler Plugin:

Unlike other maven plugins, we can easily configure this plugin as we need not specify executions, it is automatically executed as a lifecycle phase. To do so, we can update the plugin section of our pom.xml to be something like:

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

We can always find the latest version of this plugin at Maven Central.

Note that we have specified source and target configurations to be Java 1.8 version. This is so because the default values for them as of today is Java 1.6 version unless specified.

We can also optionally specify additional compiler arguments:

<configuration>
   ...
   <compilerArgs>
      <arg>-verbose</arg>
   </compilerArgs>
</configuration>

Conclusion:

In this article, we looked at the basic setup of a compiler plugin. We suggest you check out more possible configurations for the Maven Compiler Plugin at its official website.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *