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.
What is the Maven compiler plugin?
Maven compiler plugin is a maven plugin used to compile the java source code and generates class. It is also called the maven compiler. It contains two goals
- compile: compile source files of a maven application
- testCompile: Compile 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 Maven compiler version is 3.10.1. you can specify the version using the version property of a dependency in pom.xml. Without version tag, It takes 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 source or binary file and copy it to the repository folder.
Another way is automatic. Once confgiured plugin in pom.xml It downloads the maven compiler for the first time to the local repository. Next time onwards, it does not download from the remote repository instead of local cache repository
What is the use of Maven compiler source and target?
maven compiler compiles source files and generates class files of a maven project. source and target are configuration options that tell generated class files are backward compatible with java versions.
What is the latest version of the Maven compiler plugin?
maven compiler plugin’s latest version is 3.10.1. Add the dependency in pom.xml with the below values
- groupId:org.apache.maven.plugins
- artifactId:maven-compiler-plugin
- version: 3.10.1
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.13</source>
<target>1.13</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
There are two goals available with maven-compiler-plugin
compile
goal It executes the compiler:compile option to compile java files as part of the compilation phase during the build process.
It takes java files in the src
folder of the maven project, generates the target/classes folder with .class files
mvn compile
test-compile
goal : It executes the compiler:testCompile option to compile test java files as part of the test compilation phase during the testing process.
It takes java files in thetest
folder of the maven project, generates target/test/classes folder with .class files
mvn test-compile
maven compiler 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 the 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 a 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 in maven application
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>
Print dependency information of the maven compiler plugin
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 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 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
Inside the 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