You are going to learn about How to compile in Maven using Compiler plugin and all its usage. Following sections will be covered in this article.

Sections

  1. Compiler Plugin in Maven
  2. Goals of Compiler Plugin
  3. Different combination of compile command
    • mvn compile
    • mvn test
    • mvn test-compile
  4. Compile Using A Different JDK
  5. Compile Using -source and -target javac Options
  6. Compile Using Memory Allocation Enhancement
  7. Pass Compiler Arguments

Compiler Plugin in Maven

By default Maven uses javax.tools.JavaCompiler plugin for compiling java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

<forceJavacCompilerUse>

compiler can now use javax.tools if available in your current jdk, you can disable this feature using -Dmaven.compiler.forceJavacCompilerUse=true or in the plugin configuration
  • Typeboolean
  • Since3.0
  • RequiredNo
  • User Propertymaven.compiler.forceJavacCompilerUse
  • Defaultfalse

Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.

Goals of Compiler Plugin

The Compiler Plugin has two goals. Both are already bound to their proper phases within the Maven Life cycle and are therefore, automatically executed during their respective phases.

  • compiler:compile is bound to the compile phase and is used to compile the main source files.
  • compiler:testCompile is bound to the test-compile phase and is used to compile the test source files.

Different combination of answer for “how to compile in maven”?

The goals for the Compiler Plugin are bound to their respective phases in the build lifecycle. So to compile your sources, you need only to tell maven until which life cycle to execute.

Change to the directory where pom.xml is created by archetype:generate and execute the following command to compile your application sources:

mvn compile

Upon executing this command you should see output like the following:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.talksinfo.testapp:test-app >-------------------
[INFO] Building test-app 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.696 s
[INFO] Finished at: 2019-03-31T12:41:43+05:30
[INFO] ------------------------------------------------------------------------

As you can see from the output, the compiled classes were placed in ${basedir}/target/classes, which is another standard convention employed by Maven.

Now you’re successfully compiling your application’s sources and now you’ve got some unit tests that you want to compile and execute.

Execute the following command:

mvn test

Upon executing this command you should see output like the following:

[INFO] Building test-app 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test-app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test-app ---
[INFO] Surefire report directory: E:\Workspace\MavenSamples\test-app\target\surefire-reports
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom (2.4 kB at 1.6 kB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 kB at 43 kB/s)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.talksinfo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.976 s
[INFO] Finished at: 2019-03-31T13:05:01+05:30
[INFO] ------------------------------------------------------------------------

Some things to notice about the output:

  • Maven downloads more dependencies this time. These are the dependencies and plugins necessary for executing the tests (it already has the dependencies it needs for compiling and won’t download them again).
  • Before compiling and executing the tests Maven compiles the main code (all these classes are up to date because we haven’t changed anything since we compiled last).

If you simply want to compile your test sources (but not execute the tests), you can execute the following:

mvn test-compile

Compile Using A Different JDK

It is possible to tell the Compiler Plugin the specific JDK to use during compilation. Note that such configuration will be specific to this plugin, and will not affect others.

The compilerVersion parameter can be used to specify the version of the compiler that the plugin will use. However, you also need to set fork to true for this to work. For example:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
          <executable><!-- path-to-javac --></executable>
          <compilerVersion>1.3</compilerVersion>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

To avoid hard-coding a filesystem path for the executable, you can use a property. For example:

<executable>${JAVA_1_4_HOME}/bin/javac</executable>

Compile Using -source and -target javac Options

Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target. The Compiler Plugin can also be configured to provide these options during compilation.

For example, if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>

or configure the plugin directly

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error.

Compile Using Memory Allocation Enhancement

The Compiler Plugin accepts configurations for meminitial and maxmem. You can follow the example below to set the initial memory size to 128MB and the maximum memory usage to 512MB:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <fork>true</fork>
          <meminitial>128m</meminitial>
          <maxmem>512m</maxmem>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Pass Compiler Arguments

Sometimes, you need to pass other compiler arguments that are not handled by the Compiler Plugin itself but is supported by the compilerId selected. For such arguments, use the Compiler Plugin’s compilerArgs parameter The following example passes compiler arguments to the javac compiler:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <compilerArgs>
            <arg>-verbose</arg>
            <arg>-Xlint:all,-options,-path</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Conclusion

Now that you can compile your application sources, compile your tests, and execute the tests. To get you with better understanding on some usages of the Compiler Plugin, we also looked over other options to use with compiler plugin.

Please follow and like us: