Exclude servlet-api from test scope Maven

2020-06-08 14:30发布

问题:

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.

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

But provided scope adds the classes in this dependency to the classpath when running in test scope, that's a problem for Jetty which I start programmatically. Since it already has it in its library I get a

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

If I remove this dependency the Jetty server starts correctly in test scope, but I need this dependency for IntelliJ to compile my code. Whats the best way to solve this, is there a way I can exclude this dependency for the test scope ?

回答1:

try to set it to compile scope



回答2:

I just had this problem myself and wanted to share it:

  • Dependency on javax.servlet:servlet-api:3.0-alpha-1, with scope provided, so that it does not interfere with the container that my WAR is ultimately deployed onto
  • Dependency on org.eclipse.jetty:jetty-webapp, with scope test, so that I can run Jetty Server as part of my unit tests
  • Subsequently a transitive dependency on org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016, needed by jetty-webapp

Exclusion of jetty.orbit:javax.servlet is no option (for me) because Jetty Server needed a javax.servlet.HttpConstraintElement that's not offered by javax.servlet:servlet-api:3.0-alpha-1. I ended up doing this:

  1. Remove the dependency on javax.servlet:servlet-api
  2. Explicitly add the dependency on jetty.orbit:javax.servlet, with scope provided, hence fully replacing javax.servlet:servlet-api

I don't know what the deal is with the HttpConstraintElement that it needed; perhaps it'll be available in future versions of javax.servlet:servlet-api, which sorta feels to be a preferable dependency over Jetty's implementation of the same.

Edit:

By the way, the problem got introduced by me by fiddling with the configuration of a plugin that automatically formats POM files. It reordered dependencies and as such works against the solution of another poster to reorder the POM file. In my vast Maven experience: if you're "dependent" on the order of your dependencies, that's a major smell.



回答3:

For me same error came. I found old version of Servlet (2.5) existed in my path along with servlet 3.0. Once i remove(exclude) old version my issue solved.



回答4:

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>


回答5:

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"


回答6:

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



回答7:

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



回答8:

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'
}


标签: maven-3