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

 

转自:http://blog.csdn.net/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.

猜你喜欢

转载自bugyun.iteye.com/blog/2347369
今日推荐