Servlet api没有加载到tomcat插件中

时间:2014-01-16 13:34:18

标签: maven tomcat servlets maven-3 embedded-tomcat-7

我尝试通过tomcat7-maven-plugin(tomcat7:deploy)在tomcat7上部署Spring应用程序,但是当执行应用程序时,我收到此错误:

  

INFO:validateJarFile(C:\ Program Files   (86)\ Apache的Tomcat的7.0.47 \ web应用\ MyApp的\ WEB-
  INF \ lib \ servlet-api-2.5-6.1.11.jar) - jar未加载。请参阅Servlet Spe   c 2.3,第9.7.2节。违规类:javax / servlet / Servlet.class   16-gen-2014 14.18.25 org.apache.catalina.core.StandardContext   startInternal GRAVE:Error listenerStart 16-gen-2014 14.18.25   org.apache.catalina.core.StandardContext startInternal GRAVE:Context   由于之前的错误,[/ MyApp]启动失败

如果我通过码头使用码头:运行一切正常。

这是我的插件配置

        <!-- To launch embded jetty server -->
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${maven-jetty-plugin.version}</version>
            <configuration>
                <webAppConfig>
                    <contextPath>/${project.name}</contextPath>
                </webAppConfig>
            </configuration>
        </plugin>
    <!-- To launch embded tomcat server -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <url>http://localhost:8081/manager/text</url>
                <server>my-tomcat</server>
                <path>/MyApp</path>
            </configuration>
        </plugin>

虽然这些是我的依赖项:

    <!-- Spring dependencies -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
         <exclusions>
        <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

 <!-- default j2ee dependencies  -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>${servlet-api.version}</version>
        <scope>provided</scope>
    </dependency>

我该如何解决?

1 个答案:

答案 0 :(得分:0)

错误消息的重要部分是

INFO: validateJarFile(C:\Program Files (x86)\apache-tomcat-7.0.47\webapps\MyApp\WEB- INF\lib\servlet-api-2.5-6.1.11.jar) - jar not loaded. See Servlet Spe c 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

这意味着您尝试加载servlet-api-2.5-6.1.11.jar作为包含javax.servlet.Servlet定义的Web应用程序的一部分。上述规范的第9.7.2节规定容器用于加载servlet 的类加载器不得允许 WAR覆盖J2SE或Java servlet API类。


在我的情况下,我设法在pom.xml文件中使用以下行构建war文件时排除某些jar:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1-alpha-2</version>
  <configuration>
  ...
    <packagingExcludes>WEB-INF/lib/javax.servlet-*.jar</packagingExcludes>
  ...
  </configuration>
</plugin>