Cannot change version of project facet Dynamic Web Module to 3.0 requires Java 1.6 or newer 解决方案

Eclipse Maven 开发一个 jee 项目时,编译时遇到以下错误
Description Resource Path Location Type
Dynamic Web Module 3.0 requires Java 1.6 or newer. bdp line 1 Maven Java EE Configuration Problem

Description Resource Path Location Type
One or more constraints have not been satisfied. bdp line 1 Maven Java EE Configuration Problem
如图:
Dynamic Web Module 3.0 requires Java 1.6 or newer
但是 Eclipse 明明已经将编译级别设置为 1.7:
Eclipse compiler
这是由于你的 Maven 编译级别是 jdk1.5 或以下,而你导入了 jdk1.6 以上的依赖包:查看 Eclipse 的 Navigator 视图下该项目的 .classpath 文件:

[html] view plain copy
 
 print?
  1. <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">  
  2.     <attributes>  
  3.         <attribute name="maven.pomderived" value="true"/>  
  4.     </attributes>  
  5. </classpathentry>  


解决办法
使用 maven-compiler-plugin 将 maven 编译级别改为 jdk1.6 以上:

[html] view plain copy
 
 print?
    1. <build>  
    2.     <plugins>  
    3.         <!-- define the project compile level -->  
    4.         <plugin>  
    5.             <groupId>org.apache.maven.plugins</groupId>  
    6.             <artifactId>maven-compiler-plugin</artifactId>  
    7.             <version>2.3.2</version>  
    8.             <configuration>  
    9.                 <source>1.7</source>  
    10.                 <target>1.7</target>  
    11.             </configuration>  
    12.         </plugin>  
    13.     </plugins>  
    14. </build

dynamic web module 版本之间的区别:

Servlet 3.0 December 2009 JavaEE 6, JavaSE 6 Pluggability, Ease of development, Async Servlet, Security, File Uploading
Servlet 2.5 September 2005 JavaEE 5, JavaSE 5 Requires JavaSE 5, supports annotations
Servlet 2.4 November 2003 J2EE 1.4, J2SE 1.3 web.xml


错误Cannot change version of project facet Dynamic Web Module to 3.0:
在项目右键属性的Project facts中把Dynamci Web Module设置为3.0,如果报错则直接修改项目文件:工程.settings目录下的org.eclipse.wst.common.project.facet.core.xml,同时把web.xml开头设置由2.5改成3.0为:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
然后执行项目右键Maven的Update Project...即可。

posted @ 2018-01-05 14:49  ppjj  阅读(416)  评论(0编辑  收藏  举报