Add compiler argument to build Maven project

  sonic0002        2015-04-10 21:59:00       26,151        1    

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


  1 COMMENT


Rohit [Reply]@ 2015-04-12 23:01:13

Good tutorial Keep it up.



  RANDOM FUN

I need you Internet Explorer