1. Compiling Sources Using A Different JDK

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.3</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>

Each developer then defines this property in settings.xml, or sets an environment variable, so that the build remains portable.

<settings>
[...]
<profiles>
[...]
<profile>
<id>compiler</id>
<properties>
<JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>compiler</activeProfile>
</activeProfiles>
</settings>

2. Setting the -source and -target of the Java Compiler

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 enable assertions (-source 1.4) and also want the compiled classes to be compatible with JVM 1.4 (-target 1.4), you can then put:

<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.4</source>
<target>1.4</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. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs.

3. Compile Using Memory Allocation Enhancements

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.3</version>
<configuration>
<fork>true</fork>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>

4. 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.3</version>
<compilerArgs>
<arg>-verbose</arg>
<arg>-Xlint:all,-options,-path</arg>
</compilerArgs>
</plugin>
</plugins>
[...]
</build>
[...]
</project>

Example For maven-compiler-plugin的更多相关文章

  1. [Maven] - Eclipse "maven compiler plugin" 冲突解决

    刚安装最新的Maven 3.2.5,在eclipse中使用maven的Run As->Maven Install,总会提示: Failed to execute goal org.apache. ...

  2. java maven compiler设置默认1.8

    方法一: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...

  3. maven maven.compiler.source和maven.compiler.target的坑

    最近建议产品组把jdk 1.7升级到1.8,昨晚开发报了个问题过来,说maven.compiler.source和maven.compiler.target改成1.8之后,编译出来的代码还是1.7,如 ...

  4. maven surefire plugin介绍

    示例 <!-- 测试运行器,生成测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  5. 学习Maven之Maven Enforcer Plugin

    1.Maven Enforcer plugin是什么鬼? 在说这个插件是什么前我们先思考这么一个问题:当我们开发人员进入项目组进行开发前,要准备开发环境,而领导总是会强调工具的统一,编译环境的统一.比 ...

  6. [Apache Maven Shade Plugin] [example] [001] 官方例子:includes-excludes

    链接地址:[Selecting Contents for Uber JAR](http://maven.apache.org/plugins/maven-shade-plugin/examples/i ...

  7. maven jetty plugin

    转载:http://blog.163.com/xueling1231989@126/blog/static/1026408072013101311395492/ 前言: 在 maven 下测试调试时, ...

  8. 施用 maven shade plugin 解决 jar 或类的多版本冲突

    施用 maven shade plugin 解决 jar 或类的多版本冲突   使用 maven shade plugin 解决 jar 或类的多版本冲突java 应用经常会碰到的依赖的三方库出现版本 ...

  9. 解决Maven出现Plugin execution not covered by lifecycle configuration 错误

    http://blog.163.com/xh_ding/blog/static/1939032892015222368827/ 解决Maven出现Plugin execution not covere ...

  10. Jenkins安装maven integration plugin失败解决方法

    最近装了一个jenkins准备搞一个自动化测试的持续集成,但是在安装maven integration这个插件时报错,试了几次都是失败! 错误原因如下: javadoc安装失败: java.io.IO ...

随机推荐

  1. Leetcode: Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  2. let和const命令//////////////////////z

    let和const命令 let命令 块级作用域 const命令 全局对象的属性 let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的 ...

  3. Windows Phone 二、WP控件

  4. 夺命雷公狗-----React_native---5---初步读懂代码模式

    我们的代码一般导出会用两种方式,如下所示: 这两种方法都是可以的.... 引入方式其实也是很简单的,如下所示: 这样即可...

  5. HBase的数据模型相关操作 使用t这个变量来代替table1

    在andriod的应用程序中,用户所感知的都是一个个应用界面,在android程序里面每个应用界面对应一个 Activity类,这类似于.NET Winform项目中的Form窗体.与WinForm中 ...

  6. Qt之添加QLabel的点击事件

    QLabel功能为显示了一个字符串或者图片等信息,它本身没有click信号.也就不能够响应click点击事件,有什么办法来实现来,我们可以子类化QLabel,实现MouseXXXEvent.class ...

  7. C# 如何获取当前应用程序的上一级路径

    Server.MapPath 的使用方法 用法: 1.Server.MapPath ("/") 应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Serve ...

  8. Android 监听返回键、HOME键

    拦截返回键,HOME键,继承BaseActivity即可 import android.app.Activity; import android.content.BroadcastReceiver; ...

  9. 浅谈 Scrapy 爬虫(二)

    越写越像官方文档的翻译,偏离了初衷.写一些官方文档里没有的内容吧.   在不限制宽带的环境下,根据页面的大小, Scrapy 一秒能爬取40-70个页面,一天在400万到600万页面.也就是说 Scr ...

  10. Hello Spring Framework——依赖注入(DI)与控制翻转(IoC)

    又到年关了,还有几天就是春节.趁最后还有些时间,复习一下Spring的官方文档. 写在前面的话: Spring是我首次开始尝试通过官方文档来学习的框架(以前学习Struts和Hibernate都大多是 ...