Maven

Maven Compiler Plugin Example

In this example we are going to see most of the capabilities from the maven compiler plugin.

Maven is a build automation tool used mainly for java projects from apache.

You can access to the maven compiler plugin here.

We are going to see some examples of the capabilities of the maven compiler plugin.

For this example we use the following technologies:

  • MAC OSX
  • Eclipse Mars.1
  • Maven3
  • JDK 1.8.0_65 64bits
  • Maven compiler plugin 3.3

1. Introduction

The maven compiler plugin is called implicitely by the maven lifecycle in the appropiate phase so it is a special plugin. We don´t need to define it inside pom.xml it will be downloaded and executed when maven needs it.

Despite that, we can define it inside pom.xml in order to configure the way that maven should compile our classes.

The maven compiler plugin has two goals defined:

  • compile: Compile the classes under src/main/java
  • test-compile: Compile the classes under src/test/java

There is no need to define these goals inside pom.xml, as we said before, maven will invoke these goals when the maven lifecycle have to compile our classes.

Since maven 3 javax.tools.JavaCompiler (JDK 6 or newer) is used to compile java classes. The default source settings and also the default target settings is JDK 1.5 independently of the JDK you are using maven with.

We are going to see how we can change and control those things below.

2. Example project

For this example, we are going to use a java project with maven nature that will be packaged as a jar file. Eclipse Mars comes with maven support out of the box, so you don´t have to install anything. Our project will look like this

Initial project
Initial project

At this point, we have an empty maven project. We are going to define the maven compiler plugin inside plugin.xml in order to test the plugin capabilities.

The pom.xml will look like this

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.examples</groupId>
    <artifactId>maven-compiler-plugin-example</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>Maven compiler ::  example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
        </plugins>
    </build>
</project>

3. Plugin options

We are going to see how we can do several things with the maven compiler plugin:

3.1. Set a different JDK to compile classes

We can set a different JDK in order to compile our classes, the following pom.xml shows how we can do it

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.examples</groupId>
    <artifactId>maven-compiler-plugin-example</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>Maven compiler ::  example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <JAVA_HOME_6>/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home</JAVA_HOME_6>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <executable>${JAVA_HOME_6}/bin/javac</executable>
                    <compilerVersion>1.6</compilerVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

You can set the JAVA_HOME_6 in maven settings.xml file or in another properties file in order to make your pom.xml more portable.

3.2. Specify a compatible JDK

If you want the compiled classes be compatible with a specific java version, you can set a specific JDK target and source, as you can see below

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.examples</groupId>
    <artifactId>maven-compiler-plugin-example</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>Maven compiler ::  example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <JAVA_HOME_6>/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home</JAVA_HOME_6>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <executable>${JAVA_HOME_6}/bin/javac</executable>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.4</source>
                    <target>1.4</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

After compile your classes, it will be compatible with JDK 4.

3.3. Set some arguments to the compiler

You can pass arguments to the compiler, as you can see below

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.examples</groupId>
    <artifactId>maven-compiler-plugin-example</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>Maven compiler ::  example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <JAVA_HOME_6>/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home</JAVA_HOME_6>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <executable>${JAVA_HOME_6}/bin/javac</executable>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.4</source>
                    <target>1.4</target>
                    <fork>true</fork>
                    <meminitial>128m</meminitial>
                    <maxmem>512m</maxmem>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3.4. Set some specific arguments for the compiler that you have selected

If you have selected a specific compiler you can pass arguments to it with compilerArgs, as you can see below

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.examples</groupId>
    <artifactId>maven-compiler-plugin-example</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>Maven compiler ::  example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <JAVA_HOME_6>/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home</JAVA_HOME_6>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <executable>${JAVA_HOME_6}/bin/javac</executable>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.4</source>
                    <target>1.4</target>
                    <fork>true</fork>
                    <meminitial>128m</meminitial>
                    <maxmem>512m</maxmem>
                    <verbose>true</verbose>
                    <compilerArgs>
                        <arg>-verbose</arg>
                        <arg>-Xlint:all,-options,-path</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

You can see all the arguments that you can pass to the maven compiler plugin here.

4. Conclusions

As we have saw the maven compiler plugin offers some interesting capabilities which we can take advantage of in order to make our code more portable and compatible.

5. Download the eclipse project

Download
You can download the full source code of this example here: maven compiler plugin.zip

Francisco Hernandez

JEE technologies geek and development engineer. I have over 13 years of experience as software engineer in JEE architectures: Design, development, improvement etc. Currently I work as software architect and consultant. I am mainly involved in projects related to the bank and energy sectors based on Java technologies and Oracle products. I am also very interested in open-source projects
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button