Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude servlet-api from test scope Maven

Tags:

maven-3

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 ?

like image 622
Thomas Vervik Avatar asked Jun 18 '12 14:06

Thomas Vervik


2 Answers

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.

like image 78
Sander Verhagen Avatar answered Oct 17 '22 05:10

Sander Verhagen


try to set it to compile scope

like image 44
Ramanqul Buzaubak Avatar answered Oct 17 '22 07:10

Ramanqul Buzaubak