Java – Maven: javax.servlet specification deployed

javamaventomcat

I've a web application configured with Maven which uses a library, also configured with Maven and when I package geronimo-servlet_3.0_spec-1.0.jar is included in WEB-INF/lib and I don't understand why.

I check the library with mvn dependency:tree

$ mvn dependency:tree | grep geronimo
[INFO] +- org.apache.geronimo.specs:geronimo-servlet_3.0_spec:jar:1.0:provided

I check my web app:

$ mvn dependency:tree | grep geronimo
$

However when I run mvn:package the file gets included in WEB-INF/lib.

When I run mvn tomcat:run I can see:

INFO: validateJarFile(/home/stivlo/workspace/private/src/main/webapp/WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar) – jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

Why and how to avoid? Thank you.

UPDATE 1: as requested I add the pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.obliquid</groupId>
    <artifactId>test-webapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>private webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <!-- For Jakarta ORO -->
        <repository>
            <id>mvnsearch</id>
            <name>Maven Search</name>
            <url>http://www.mvnsearch.org/maven2/</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.obliquid.helpers</groupId>
            <artifactId>obliquid-helpers</artifactId>
            <version>0.9-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>private</finalName>
    </build>

</project>

UPDATE 2: I followed the advice of Stephen C and modified the build section as follows:

<build>
    <finalName>private</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war</artifactId>
            <version>2.1</version>
            <configuration>
                <overlays>
                    <overlay>
                        <groupId>org.obliquid</groupId>
                        <artifactId>test-webapp</artifactId>
                        <excludes>
                            <exclude>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</exclude>
                        </excludes>
                    </overlay>
                </overlays>
            </configuration>
        </plugin>
    </plugins>
</build>

However geronimo*.jar still gets included. I guess I've made a mistake in this configuration.

UPDATE 3: Stephen C. says that I should use

the groupId the artifactId of the WAR
file that contains the JAR file(s)
that you are trying to exclude.

I didn't know that WAR files could have a groupId and artifactId, in fact in my pom.xml I don't see any. My project builds a WAR file and has a groupId and an artifactId and those were the ones I tested above without success.

The dependency causing the problem is the following (is a JAR, not a WAR):

<dependency>
    <groupId>org.obliquid.helpers</groupId>
    <artifactId>obliquid-helpers</artifactId>
    <version>0.9-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

If I try to use the groupId and artifactId listed in this dependency I've the following error:

[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project test-webapp:
overlay [ id
org.obliquid.helpers:obliquid-helpers]
is not a dependency of the project. ->
[Help 1]

If I try to use the groupId and artifactId of the JAR included by org.obliquid.helpers:

<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>)

I have the same error.

[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project test-webapp:
overlay [ id
org.apache.geronimo.specs:geronimo-servlet_3.0_spec]
is not a dependency of the project. ->
[Help 1]

Reading the War plugin documentation, I found a section about creating skinny WARs. So I tried the following:

<build>
    <finalName>private</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <packagingExcludes>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</packagingExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

Still without any success, geronimo-servlet_3.0_spec-1.0.jar is still there!

<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project test-webapp: overlay [ id org.obliquid.helpers:obliquid-helpers] is not a dependency of the project. -> [Help 1]

<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project test-webapp: overlay [ id org.apache.geronimo.specs:geronimo-servlet_3.0_spec] is not a dependency of the project. -> [Help 1]

UPDATE 4: I discovered that the target/private.war file is not a zip of target/private/ directory, but the exclusions are done at packaging time and not by deleting files in target/private/ — This means, I've to re-test all the things I did before.

  • Suggestion of gouki: doesn't work, the JAR is still there also in the WAR file.
  • Suggestion of Stephen C., maybe mis-understood: actually I just noticed that the pom.xml is always invalid whatever groupId/artifactId I put of the three possibilities explained above. So they didn't work for me.
  • What I found in the documentation (packagingExcludes), works.

Now, if I had to choose one of he answers I would choose Stephen C., because he helped me pointing at the documentation of the WAR plugin (I was reading in the wrong places). However I'd accept an answer that doesn't work, at least in the way I tried (probably wrong). So I'm not going to accept any answer, and add a new answer myself with the final working configuration.

UPDATE 5: I post the relevant part of the pom.xml of obliquid-helpers, that mentions geronimo-servlet_3.0_spec. I've marked it optional and with scope provided, still it gets included by a web-app, unless I mark it as "packagingExclude" in the maven-war-plugin configuration.

<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.obliquid.helpers</groupId>
  <artifactId>obliquid-helpers</artifactId>
  <version>0.9-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>obliquid-helpers</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <repositories>
    [...]
  </repositories>

  <dependencies>
    [...]

    <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-servlet_3.0_spec</artifactId>
      <version>1.0</version>
      <scope>provided</scope>
      <optional>true</optional>
    </dependency>

  </dependencies>

</project>

Best Answer

Clearly, something has a dependency on that JAR file. If it is not showing up in the dependency tree, perhaps it is due to a dependency of your webapp WAR file on another WAR file that has this dependency.

If that is the case, then you could get add an <excludes> to the <overlay> element of the build descriptor for the WAR file plugin; e.g.

...
<build>
  <finalName>webapp</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <overlays>
            <overlay>
              <groupId>xxx</groupId>
              <artifactId>yyy</artifactId>
              <excludes>
                <exclude>WEB-INF/lib/whatever.jar</exclude>
              </excludes>
            </overlay>
          </overlays>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...

If you are using WAR file overlays, you should always include the clean target in the build. Otherwise you can get old dependencies hanging around in the WAR file. (IIRC, there is a Warning in the Maven output each time you build an overlaid WAR without cleaning!)

In fact, this could be the root cause of your problems. For instance, if previously you had the "geronimo" as an ordinary dependency and you haven't run mvn clean since then, the JAR file could still be hanging around.

Related Topic