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.
The Maven Compiler Plugin has two goals:
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:
To compile our test source files along with all the main source files, we can execute:
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>
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.