Add compiler argument to build Maven project

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

Maven projects often require passing specific arguments to the Java compiler, such as defining source and target releases or handling options like -XDignore.symbol.file for Java 8's modularized source code. To integrate these arguments into a Maven build, developers must configure the maven-compiler-plugin within their project's pom.xml. For plugin versions 3.1 and later, compiler arguments are specified using the block containing multiple elements. Earlier versions (prior to 3.1) utilize a single tag, which has the limitation of only processing the last argument if multiple are provided. Therefore, it is strongly recommended to use maven-compiler-plugin version 3.1 or newer for reliable and flexible compiler argument management.

Maven is a software project to manage a project's build, reporting and documentation from a central piece of information. It's now widely used t build and deploy projects. It can help automatically maintain the dependencies of projects. There is a central project configuration file named pom.xml. Here you can configure the project you want to build. 

In this post, we will show you how to add compiler argument when using javac to compile Java source code. Sometimes we need to pass compiler arguments when we compile source code, for example, we may want to specify the -source and -target release of the code. Especially in latest Java 8, the source code is now modularized, When javac is compiling code it doesn't link against rt.jar by default in latest Java 8. Instead it uses special symbol file lib/ct.sym with class stubs. The option -XDignore.symbol.file is to ignore the symbol file so that it will link against rt.jar.

To add compiler argument, you need to add the compiler plugin to your project's pom.xml file. Here is a sample plugin configuration:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.3</version>
			<configuration>
				<source>1.8</source> 
				<target>1.8</target> 
				<fork>true</fork>
				<compilerArgs>
					<arg>-XDignore.symbol.file</arg>
				</compilerArgs>
			</configuration>
		</plugin>
	</plugins>
</build>

In this configuration, the plugin version is really important here. Prior to maven-compiler-plugin 3.1, the compiler argument should be <compilerArgument>-XDignore.symbol.file</compilerArgument> instead and it has a limitation, if you put multiple <compilerArgument>...</compilerArgument>s, only the last one will be picked up and all others will be ignored.

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.0</version>
			<configuration>
				<source>1.8</source> 
				<target>1.8</target> 
				<fork>true</fork>
				<compilerArgument>-XDignore.symbol.file</compilerArgument>
			</configuration>
		</plugin>
	</plugins>
</build>

So we strongly recommend you use the plugin starting from 3.1. In the <configuration> block, you can specify the source and target release of the code and also there are s list of javac options can be specified.

You can run maven with command line option -X to display the debug information, there you will see the compiler arguments you specified. For example:

MAVEN COMPILER ARGUMENT COMPILER OPTION JAVA 8

  RELATED

  COMMENT

1
Rohit
Apr 12, 2015 at 11:01 pm

Good tutorial Keep it up.