【问题标题】:Exclude servlet-api from test scope Maven从测试范围 Maven 中排除 servlet-api
【发布时间】:2012-06-18 14:22:37
【问题描述】:

我的 pom.xml 中有以下依赖项,因此我的 IDE (IntelliJ) 在编译期间具有可用的 servlet-api 类,但在构建中未提供。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
    <scope>provided</scope>
 </dependency>

但是在测试范围内运行时,提供的范围会将此依赖项中的类添加到类路径中,这对于我以编程方式启动的 Jetty 来说是个问题。因为它已经在它的库中,所以我得到了一个

 java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package

如果我删除此依赖项,Jetty 服务器会在测试范围内正确启动,但我需要此依赖项才能让 IntelliJ 编译我的代码。解决这个问题的最佳方法是什么,有没有办法可以排除测试范围的这种依赖关系?

【问题讨论】:

    标签: maven-3


    【解决方案1】:

    我自己也遇到了这个问题,想分享一下:

    • 依赖于javax.servlet:servlet-api:3.0-alpha-1,范围为provided,因此它不会干扰我的WAR 最终部署到的容器
    • 依赖于org.eclipse.jetty:jetty-webapp,范围为test,这样我就可以在单元测试中运行Jetty Server
    • 随后是org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016 的传递依赖,jetty-webapp 需要

    排除jetty.orbit:javax.servlet(对我来说)是没有选择的,因为码头Server需要javax.servlet.HttpConstraintElement,而javax.servlet:servlet-api:3.0-alpha-1没有提供。我最终这样做了:

    1. 移除对javax.servlet:servlet-api的依赖
    2. 显式添加对jetty.orbit:javax.servlet 的依赖,范围为provided,因此完全替换javax.servlet:servlet-api

    我不知道它需要的HttpConstraintElement 是什么情况;也许它会在 javax.servlet:servlet-api 的未来版本中可用,这在某种程度上感觉比 Jetty 的实现更可取。

    编辑:

    顺便说一句,这个问题是我通过摆弄一个自动格式化 POM 文件的插件的配置来引入的。它重新排序了依赖关系,因此与另一张海报重新排序 POM 文件的解决方案背道而驰。在我丰富的 Maven 经验中:如果您“依赖”依赖项的顺序,那是一种主要气味

    【讨论】:

      【解决方案2】:

      尝试将其设置为编译范围

      【讨论】:

      • 我在这里迷路了——你能详细说明一下吗? “编译范围”是什么意思?
      • 编译范围是指只在java文件的编译过程中包含jar库文件。
      【解决方案3】:

      对我来说,同样的错误来了。我发现旧版本的 Servlet (2.5) 与 servlet 3.0 一起存在于我的路径中。一旦我删除(排除)旧版本,我的问题就解决了。

      【讨论】:

        【解决方案4】:

        我在尝试不在运行 junit 测试的类路径中包含 javax.servlet-api 时找到了解决方案。实际上,我将 servlet-api 移到了类路径中 jar 的最末端,然后启示来了……

        我使用了错误版本的 servlet-api。我使用的是 2.5,但需要 3.0。 Maven 范围我选择“提供”。适用于在 eclipse 中运行 junit 和“mvn test”执行。

        但是,我不明白为什么没有冲突。如果我做对了,即使是“提供的”依赖项也会在测试时暴露在类路径中,所以可能会发生冲突 - 或者,当然 - 如果我完全命中了用于编译的 servlet-api 和码头的正确版本那么servlet-api就没有冲突了。

        无论如何,它对我有用。

        这是我的依赖项/* pom-setup for jetty + servlet api:

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>8.1.4.v20120524</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>8.1.4.v20120524</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>8.1.4.v20120524</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-jsp</artifactId>
            <version>8.1.4.v20120524</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        

        【讨论】:

          【解决方案5】:

          我使用以下 sbt 项目设置来解决类似的问题:

            "any dependency program that includes servlet-api java library code" %  excludeAll ExclusionRule(organization = "org.eclipse.jetty.servlet-api"),
            "org.mortbay.jetty" % "servlet-api" % "3.0.20100224"
          

          【讨论】:

            【解决方案6】:

            您也可以混合使用 grizzly 和 jetty 依赖项。

            【讨论】:

              【解决方案7】:

              在我的情况下,排除还不够,但将码头降级到 7.6.14.v20131031 对我有用。

              【讨论】:

                【解决方案8】:

                对于 Gradle 用户,使用 Jetty 运行基于 Spring WebMVC 的嵌入式 web 应用程序的设置适用于以下依赖项:

                apply plugin: 'war'
                apply plugin: 'jetty'
                apply plugin: 'eclipse-wtp'
                dependencies {
                
                   // Logging support
                   compile 'org.slf4j:slf4j-api:1.7.7'
                   runtime 'org.slf4j:slf4j-simple:1.7.7'
                
                   // Spring WebMVC part
                   compile 'org.springframework:spring-web:4.0.6.RELEASE'
                   compile 'org.springframework:spring-webmvc:4.0.6.RELEASE'
                   compile 'org.springframework:spring-context:4.0.6.RELEASE'
                   compile 'org.springframework:spring-core:4.0.6.RELEASE'
                   compile 'org.springframework:spring-beans:4.0.6.RELEASE'
                   compile 'org.springframework:spring-expression:4.0.6.RELEASE'
                
                   // Jetty support
                   compile 'org.eclipse.jetty:jetty-server:8.1.4.v20120524'
                   compile 'org.eclipse.jetty:jetty-servlet:8.1.4.v20120524'
                   compile 'org.eclipse.jetty:jetty-webapp:8.1.4.v20120524'
                   compile 'org.eclipse.jetty:jetty-jsp:8.1.4.v20120524'
                
                   // Web Container interaction
                   //providedCompile 'javax.servlet:servlet-api:2.5'
                   runtime 'jstl:jstl:1.2'
                
                   // Unit Testing
                   testCompile 'junit:junit:4.11'
                   testCompile 'org.mockito:mockito-all:1.9.5'
                   testCompile 'org.springframework:spring-test:3.2.0.RELEASE'
                }
                

                【讨论】:

                  猜你喜欢
                  • 2019-11-25
                  • 2013-09-22
                  • 2013-01-20
                  • 2012-08-20
                  • 2019-11-02
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-11-12
                  • 2020-10-31
                  相关资源
                  最近更新 更多