Search code examples
mavenmaven-3maven-war-plugin

Maven: Why am I getting servlet-api-2.5


I'm creating a web application with Maven, and I'm having a problem - for some reason I get servlet-api-2.5.jar included, which gives weird exceptions. Removing this file from the WAR archive solves all problems, but I'm trying to understand why it's there in the first place. My dependencies in the complete pom.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sillyfly.webapp</groupId>
    <artifactId>Webapp</artifactId>
    <version>0.1</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <webXml>web/WEB-INF/web.xml</webXml>
                    <webappDirectory>web</webappDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.json</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-jstlel</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

And running mvn dependency:tree -Dverbose yields:

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - org.sillyfly.webapp:Webapp:jar:0.1
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] org.sillyfly.webapp:Webapp:jar:0.1
[INFO] +- org.glassfish:javax.json:jar:1.0.4:compile
[INFO] +- org.apache.taglibs:taglibs-standard-impl:jar:1.2.5:compile
[INFO] +- org.apache.taglibs:taglibs-standard-jstlel:jar:1.2.5:compile
[INFO] +- org.apache.taglibs:taglibs-standard-spec:jar:1.2.5:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.38:compile
[INFO] \- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Sat Mar 26 12:12:32 IDT 2016
[INFO] Final Memory: 24M/515M
[INFO] ------------------------------------------------------------------------

Where there is no mention for servlet-api-2.5.jar. I'm now noticing it says jar and not war, so I guess my question is twofold: 1. Are the JAR and WAR dependencies different, and if so how do I get Maven dependency plugin to show me the WAR dependencies? 2. How can I explicitly tell Maven not to include servlet-api-2.5.jar? I've tried adding an exclusion in various places, but since I don't know why it's there in the first place it's mostly a guessing game as to where to put the exclusion, and none of the places I've tried have worked.

mvn --version for referene:

Apache Maven 2.2.1 (rdebian-23)
Java version: 1.8.0_72-internal
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "4.4.0-1-amd64" arch: "amd64" Family: "unix"

Edit: I updated to Maven 3.3.9 and I still get the exact same result. The only difference is that the dependency tree shows dependencies between the various apache taglibs libraries, but still no mention of servlet-api-2.5.


Solution

  • I can't reproduce your problem from the information you gave, so the root cause must be somewhere else.

    Do you see the extra servlet JAR when running mvn package from the command line, or could it be a strange effect of your IDE?

    If the problem persists, try to reduce your app to a minimum and create a self-contained project e.g. on GitHub that people can look at.

    By the way, you must never include the servlet API in WEB-INF/lib (not twice, and not even once). Always use Maven scope provided for the servlet API.