Fix maven JSTL 1.2.1 dependency so maven-war-plugi

2019-02-24 10:53发布

My setup: jdk 7, Tomcat 7.0.29, ,Eclipse Juno (with m2e[Maven 3.0.4 embedded], m2eclipse-wtp)

I have a Dynamic Web Project with this JSTL dependency:

<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.1</version>
</dependency>

When I mvn package and deploy on Tomcat, I get these non-fatal messages in the log that don't stop my app from deploying:

validateJarFile(...\WEB-INF\lib\jsp-api-2.1.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
validateJarFile(...\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

I check and yes, JARs in question are being packaged in the WAR. I check the dependencies with mvn dependency:tree and get this:

[INFO] \- org.glassfish.web:javax.servlet.jsp.jstl:jar:1.2.1:compile
[INFO]    \- javax.servlet.jsp.jstl:jstl-api:jar:1.2:compile
[INFO]       +- javax.servlet:servlet-api:jar:2.5:compile
[INFO]       \- javax.servlet.jsp:jsp-api:jar:2.1:compile

Both JARs are showing in the compile scope, But if I check the pom.xml on org.glassfish.web:javax.servlet.jsp.jstl:jar:1.2.1 I see this:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.2</version>
  <scope>provided</scope>
</dependency>

that shows them on the provided scope, which I thought would exclude them from the packaging.

Questions:

  1. How do I tell the WAR plugin to not include these JAR's? <excludes/> won't cut it because this also removes them from the build path.
  2. What if I want to develop against the Servlet 3.0 spec but keeping this JSTL version?

2条回答
不美不萌又怎样
2楼-- · 2019-02-24 11:10

For version 1.2.1 use:

<!-- add the selvlet-api that tomcat provides as provided -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.1</version>
    <exclusions>
         <!-- jstl-api was adding selvlet-api 2.5 and jsp-api-->
        <exclusion>
            <artifactId>jstl-api</artifactId>
            <groupId>javax.servlet.jsp.jstl</groupId>
        </exclusion>
    </exclusions>
</dependency>

Because if not the jstl-api 1.2 will be added as a dependency. It is the jstl-api that added the jsp-api and servlet-api dependencies.

查看更多
Luminary・发光体
3楼-- · 2019-02-24 11:12

Figured it out, the jsp-api was sneaking into the WEB-INF\lib as a transative dependency of the jstl, the fix is to exclude like so.

<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>${javax.jstl.version}</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
            <artifactId>jsp-api</artifactId>
            <groupId>javax.servlet.jsp</groupId>
        </exclusion>
    </exclusions>
</dependency>
查看更多
登录 后发表回答