Maven Compiler Plugin

In this section, you will learn about maven compiler plugin and how it can be configure in maven.

Maven Compiler Plugin

Maven Compiler Plugin

In this section, you will learn about maven compiler plugin and how it can be configure in maven.

By implementing this plugin, you can compile our project sources. In maven compiler plugin, javac is the default compiler and it is employed to compile java sources. Also, the default source and target setting is 1.5 without using the JDK you run Maven with.

You can change these defaults , you need to set source and target in pom.xml as shown below :

<configuration>
	<source>1.5</source>
	<target>1.5</target>
</configuration>

The apache-maven-2.2.1 versions is used in the below example.

First, you need to create web application using maven using following command :


C:\project_maven>mvn archetype:generate -DgroupId=net.roseindia -DartifactId=roseindia-webapp -DarchetypeArtifactId=maven-archetype-webapp

For complete example(web application structure creation using maven) click here.

The complete pom.xml containing configuration for maven compiler plugin is given below :

<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>net.roseindia</groupId>
<artifactId>roseindia-webapp</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>roseindia-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>roseindia-webapp</finalName>
<plugins>
<!-- maven-compiler-plugin -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- maven-jetty-plugin -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>


<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>/</contextPath>

</webAppConfig>

<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>

</plugins>
</build>
</project>

Usage

To compile your sources you need to use following command :


mvn compile

To compile your test sources, you need to use the following command :


mvn test-compile

Run the above commands after adding configuration for maven compiler plugin :

C:\project_maven\roseindia-webapp>mvn compile

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building roseindia-webapp Maven Webapp
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Jul 10 16:44:59 GMT+05:30 2012
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------

C:\project_maven\roseindia-webapp>mvn test-compile

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building roseindia-webapp Maven Webapp
[INFO] task-segment: [test-compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\project_maven\roseindia-webapp\src
\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Jul 10 16:47:58 GMT+05:30 2012
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------