Exclude servlet-api from test scope Maven

后端 未结 8 944
刺人心
刺人心 2021-02-13 04:43

I have the following dependency in my pom.xml so that my IDE (IntelliJ) has the servlet-api classes available during compilation, but not provided in the build.



        
相关标签:
8条回答
  • 2021-02-13 04:55

    Exclusions weren't enough in my case, but downgrading jetty to 7.6.14.v20131031 works for me.

    0 讨论(0)
  • 2021-02-13 04:55

    For Gradle users, a setup with Jetty running an embedded webapp based on Spring WebMVC works with the following dependencies:

    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'
    }
    
    0 讨论(0)
  • 2021-02-13 04:58

    You can also have it with a mixture of grizzly and jetty dependencies.

    0 讨论(0)
  • 2021-02-13 04:59

    I found the solution when trying not to include javax.servlet-api in the classpath running a junit test. Actually I moved the servlet-api at the very end of the jars in the classpath and enlightment came...

    I used the wrong version of the servlet-api. I was using 2.5 but needed 3.0. Maven scope I do choose "provided". Works for running junit inside eclipse and for "mvn test" execution.

    Nevertheless, I do not understand why there is no conflict. If I got it right even the "provided" dependencies will be exposed in the classpath when testing, so there could be a conflict - or, of course - if I exactly hit the right versions of the servlet-api used for compilation and the jetty's servlet-api then there is no conflict.

    Anyways, it works for me.

    Here's my dependencies/* 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>
    
    0 讨论(0)
  • 2021-02-13 05:02

    I use the following sbt project setting to fix a similar problem:

      "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"
    
    0 讨论(0)
  • 2021-02-13 05:16

    try to set it to compile scope

    0 讨论(0)
提交回复
热议问题