在Eclipse Maven项目中更改动态Web模块版本

Geo*_*ker 17 java eclipse maven

我正在尝试设置Dynamic Web Module 3.0以支持Java 6开发.每当我这样做时,我在Eclipse的Problems选项卡中都会收到此错误Maven > Update Project.

Dynamic Web Module 3.1 requires Java 1.7 or newer.

看起来好像没有什么会错,但我一定是失去了一些东西,因为我一直收到此错误.

以下是我的.settings/org.eclipse.wst.common.project.facet.core.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <fixed facet="jst.java"/>
  <fixed facet="jst.web"/>
  <installed facet="jst.java" version="1.6"/>
  <installed facet="jst.web" version="3.0"/>
</faceted-project>
Run Code Online (Sandbox Code Playgroud)

在eclipse中,我的Java编译器正确设置为1.6.在Project Facets选项卡中,Dynamic Web Module被正确指定为版本3.0,而Java在Project Facets下也显示为1.6.

在我的pom.xml文件中,我还相应地设置了Java版本:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

作为旁注,我还应该提到我正在使用一个WebApplicationInitializer没有web.xml文件的纯Java构建.

当我试图压制这个看似无关的错误消息时,我们非常感激任何帮助.

编辑:添加了pom.xml内容

<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.XXXXX</groupId>
    <artifactId>XXXXX</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>XXXXX</name>
    <packaging>war</packaging>
    <url>http://maven.apache.org</url>

    <build>
        <finalName>XXXXX</finalName>

        <plugins>

            <!-- Source level should be 1.6 (not Maven default) for Java EE 6 projects -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <!-- When using xml-less approach, need to disable Maven's warning about 
                missing web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>
    </build>
    <dependencies>

        <!-- JUnit testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>

        <!-- Spring Context without commons-logging -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.2.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Spring Security core -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>

        <!-- Spring Security config -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>

        <!-- Spring Security web -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>

        <!-- jcl-over-slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.6</version>
        </dependency>

        <!-- slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>

        <!-- slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.6</version>
        </dependency>

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- Need servlet API for compiling the classes. Not needed in runtime -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Required for xml-less configuration of Spring via @Configuration annotations -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>3.1</version>
        </dependency>

        <!-- Required for getting Spring working with Hibernate -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.4.Final</version>
        </dependency>

        <!-- For using JPA instead of "pure Hibernate" -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.4.Final</version>
        </dependency>

        <!-- DB connection pooling for production applications -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>

        <!-- Concrete JDBC driver for MySQL DB -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>

        <!-- Add Taglib support -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

rob*_*ann 18

在我们的项目中,我们添加更改web.xml根元素的声明,从以下更改:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
Run Code Online (Sandbox Code Playgroud)

至:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
         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_2_5.xsd"
         version="2.5" >
Run Code Online (Sandbox Code Playgroud)

可能Eclipse期望它满足不同的Servlet版本规范.在我们的例子中,我们既没有编译也没有运行时问题.

编辑:在你的情况下,你没有使用web.xml文件,有以下pom的依赖,这可能会混淆Eclipse的maven插件:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

将其更改为"3.0.1".(事实证明,WebApplicationInitializer需要" Servlet 3.0+环境 "而不是3.1+)

  • 3.0应该足够了,WebApplicationInitializer的javadoc说:"在Servlet 3.0+环境中实现的接口" (2认同)

Enr*_*tín 12

你可以将它添加到pom.xml:

<build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

适用于Spring STS 3.6.3 :)

编辑:

来源:http://crunchify.com/how-to-solve-dynamic-web-module-3-1-requires-java-1-7-or-newer-in-eclipse/