Wednesday, July 18, 2018

Learn Maven Compiler plugin with examples

maven compiler plugin tutorials
Maven is an opensource apache tool to automate the build process for the 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 and compilation goals 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 javac tool to compile the source code. This plugin internally calls jdk javac tool to compile the code.

How to change compiler options in pom.xml

we can configure different parameters using configuration tag in the plugin
<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>

Plugin help 

The below command provides helpful information about the compiler plugin
mvn compiler:help -Ddetail=true

Compiler goals usage 

There are two goals available.
compile goal:- This executes compiler:compile option to compiles java files as part of the compilation phase during the build process
mvn compile
test-compile goal:-This executes compiler:testCompile option to compiles test java files as part of test compilation phase during testing process Compile java code.
mvn test-compile

Plugin configuration Options 

There are a lot of configurations we can change/override default values. We can add different options in 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 examples

How to configure different java path and compiler version for the plugin?

Change executable and compilerVersion tag in configuration options as per your java settings. Please see below example for more information.
<configuration>
   <verbose>true</verbose>
   <fork>true</fork>
   <executable>${JAVA_HOME}/bin/javac</executable>
   <compilerVersion>1.8</compilerVersion>
</configuration>

Configure java compiler version in plugin 

JDK javac tool has source and target options to set JDK versions during compilation. this plugin configuration also provides source and target options Using this 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 maven compiler 

Sometimes, We need to pass memory related and garbage configurations to the compilation phase. This plugin provides 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 the warning during the compilation process sometimes if the encoding is not set. We can do in either compiler level or project level.
Compiler encoding configuration
<configuration>
   <encoding>UTF-8</encoding>
</configuration>
maven project encoding:- This is the root level configuration which affects all the modules of the project
<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

disable this compiler plugin. 

By default 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 below option
<configuration>
   <include>src/javafolder</include>
</configuration>
Compiled source files ie class files will be written to target/test/classes folder by default, To override target folder with target/myclasses
<build>
   <outputDirectory>${project.build.directory}/myclasses</outputDirectory>
</build>

Print dependency information of 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>

Continue compilation when error/warning occurs 

failOnError and failOnWarning options allow compiler continue compilation. Errors will be shown once the compilation build process completed,

Related article


EmoticonEmoticon