0

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

信息:validateJarFile(C:\Program Files (x86)\apache-tomcat-7.0.47\webapps\MyApp\WEB-
INF\lib\servlet-api-2.5-6.1.11.jar) - jar 未加载。请参阅 Servlet 规范 2.3,第 9.7.2 节。违规类:javax/servlet/Servlet.class 16-gen-2014 14.18.25 org.apache.catalina.core.StandardContext startInternal GRAVE:错误 listenerStart 16-gen-2014 14.18.25 org.apache.catalina.core.StandardContext startInternal GRAVE:上下文 [/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>

我该如何解决?

4

1 回答 1

0

the important part of the error message is

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

This means that you're trying to load servlet-api-2.5-6.1.11.jar as part of a web application which contains the definition of javax.servlet.Servlet. Section 9.7.2 of the mentioned specification states that the classloader which the container uses to load servlets must not allow the WAR to override J2SE or Java servlet API classes.


In my case I managed to exclude certain jars when building the war file with the following lines in the pom.xml file:

<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>
于 2014-06-10T10:03:18.723 回答