maven配置默认使用的JDK版本(转)

 

转自:http://blog.youkuaiyun.com/kqygww/article/details/12922135

 

设置全局的jdk,在setting.xml文件中的profiles元素下添加如下profile元素

 

[html]  view plain  copy
 
  1. <profile>  
  2.     <id>jdk1.8</id>  
  3.     <activation>  
  4.         <activeByDefault>true</activeByDefault>  
  5.         <jdk>1.7</jdk>  
  6.     </activation>  
  7.     <properties>  
  8.         <maven.compiler.source>1.8</maven.compiler.source>  
  9.         <maven.compiler.target>1.8</maven.compiler.target>  
  10.         <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
  11.     </properties>   
  12. </profile>  
 


设置局部的jdk,在项目的pom,xml文件中添加如下build元素

[html]  view plain  copy
 
  1. <build>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <groupId>org.apache.maven.plugins</groupId>  
  5.             <artifactId>maven-compiler-plugin</artifactId>  
  6.             <configuration>  
  7.                 <source>1.8</source>  
  8.                 <target>1.8</target>  
  9.             </configuration>  
  10.         </plugin>  
  11.     </plugins>  
  12. </build>  

 

 

=============================================================================

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.6.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. 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. In the same way, setting the  source option does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch Maven, refer to the  Compile Using A Different JDK  example.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值