Maven dependency for Servlet 3.0 API?
Asked Answered
S

11

237

How can I tell Maven 2 to load the Servlet 3.0 API?

I tried:

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

I use http://repository.jboss.com/maven2/ but what repository would be correct?

Addendum:

It works with a dependency for the entire Java EE 6 API and the following settings:

<repository>
    <id>java.net</id>
    <url>http://download.java.net/maven/2</url>
</repository>

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

I'd prefer to only add the Servlet API as dependency, but "Brabster" may be right that separate dependencies have been replaced by Java EE 6 Profiles. Is there a source that confirms this assumption?

Sheepish answered 30/12, 2009 at 12:29 Comment(3)
No sources, no javadocs in java.net/maven/2 repository. Oracle, go to hell!Bourse
Using javaee-Api instead of servlet-api does not give you the same version of javax.servlet.ServletContext. I am using spring framework 3.1 and using dynamic dispathcer (annotation). Sa'ad's answer is the only answer that works for me. You really should not go with Pascal as that seems to be more generic. Heck.. gradle beats maven in resolving dependencies.Annuity
OMG, they changed the artifact name from servlet-api to javax.servlet-api. Lost half an hour "debugging"... :/Hydrophilous
M
117

I'd prefer to only add the Servlet API as dependency,

To be honest, I'm not sure to understand why but never mind...

Brabster separate dependencies have been replaced by Java EE 6 Profiles. Is there a source that confirms this assumption?

The maven repository from Java.net indeed offers the following artifact for the WebProfile:

<repositories>
  <repository>
    <id>java.net2</id>
    <name>Repository hosting the jee6 artifacts</name>
    <url>http://download.java.net/maven/2</url>
  </repository>
</repositories>        
<dependencies>
  <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

This jar includes Servlet 3.0, EJB Lite 3.1, JPA 2.0, JSP 2.2, EL 1.2, JSTL 1.2, JSF 2.0, JTA 1.1, JSR-45, JSR-250.

But to my knowledge, nothing allows to say that these APIs won't be distributed separately (in java.net repository or somewhere else). For example (ok, it may a particular case), the JSF 2.0 API is available separately (in the java.net repository):

<dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-api</artifactId>
   <version>2.0.0-b10</version>
   <scope>provided</scope>
</dependency>

And actually, you could get javax.servlet-3.0.jar from there and install it in your own repository.

Magenta answered 30/12, 2009 at 15:24 Comment(6)
One small correction: javaee-web-api includes EL 2.2 (Unified Expression Language 2.2), not EL 1.2Whiles
... and for gradle use: compile 'javax:javaee-web-api:6.0'Galliard
Note that javaee-web-api only contains method stubs (no byte code). You cannot use this dependency outside the provided scope which is why I prefere the suggestion of Sa'ad.Trygve
@Pascal - "I'd prefer to only add the Servlet API as dependency" - you would do that if you are dealing with a pure servlet container (tomcat, jetty) vs a JEE compliant container (TomEE, wildfly, etc)Alidia
As JoD. said, it makes sense to add the Servlet API alone when you don't need "EJB Lite 3.1, JPA 2.0, JSP 2.2, EL 1.2, JSTL 1.2, JSF 2.0, JTA 1.1". For example, that is my need, for Vaadin development.Hinds
the javaee-web-api has been updated to <version>7.0</version>Naive
C
467

This seems to be added recently:

https://repo1.maven.org/maven2/javax/servlet/javax.servlet-api/3.0.1/

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
</dependency>
Cavort answered 4/11, 2011 at 11:55 Comment(10)
Hey, This works fine but I'm not sure this is the exact dependency to be used (with Tomcat 7 for example); the reason is that the sources attached to this dependency do not match what's actually running when you actually do debugging.Shifrah
What does the scope provided mean?Annuity
@TejaswiRana Provided scope means that it is not packaged to war. The dependency is available at compile time, you expect it in the server library folder.Lungwort
Why didn't it just reuse the artifactId servlet-api? Because it's fun to add <excludes> for the old artifactId (in order to prevent getting both the old and new servlet api on your classpath if one of your dependencies still depends on the old one)? :)Weedy
Geoffrey, I'm a bit unclear on that, is it that if I use this, and something else pulls in the old API, I have to use an excludes somewhere?Javelin
Indeed this is the best way, but be warned that this may work with Maven 3.0.x jira.codehaus.org/browse/MNG-5255Calzada
Also when you re-compile make sure you run a clean install (mvn clean install) otherwise all sorts of caching issues may occurCalzada
FYI, the most recent version is javax.servlet-api-3.1.0. Just be sure your Servlet container can handle that version. For example Version 8 of Tomcat can handle 3.1.Hinds
Evidently, this changed from servlet-api 2.5 to 3.x. And I overlooked the artifact name because they're so similar! It changed from servlet-api to javax.servlet-api. Thank you, thank you!Sitting
@Shifrah - what is running is different because you only compile against the spec (hence scope=provided, name includes api), while we run against an implementation (installed server side, or supplied using scope=runtime, name includes - imply).Alidia
M
117

I'd prefer to only add the Servlet API as dependency,

To be honest, I'm not sure to understand why but never mind...

Brabster separate dependencies have been replaced by Java EE 6 Profiles. Is there a source that confirms this assumption?

The maven repository from Java.net indeed offers the following artifact for the WebProfile:

<repositories>
  <repository>
    <id>java.net2</id>
    <name>Repository hosting the jee6 artifacts</name>
    <url>http://download.java.net/maven/2</url>
  </repository>
</repositories>        
<dependencies>
  <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

This jar includes Servlet 3.0, EJB Lite 3.1, JPA 2.0, JSP 2.2, EL 1.2, JSTL 1.2, JSF 2.0, JTA 1.1, JSR-45, JSR-250.

But to my knowledge, nothing allows to say that these APIs won't be distributed separately (in java.net repository or somewhere else). For example (ok, it may a particular case), the JSF 2.0 API is available separately (in the java.net repository):

<dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-api</artifactId>
   <version>2.0.0-b10</version>
   <scope>provided</scope>
</dependency>

And actually, you could get javax.servlet-3.0.jar from there and install it in your own repository.

Magenta answered 30/12, 2009 at 15:24 Comment(6)
One small correction: javaee-web-api includes EL 2.2 (Unified Expression Language 2.2), not EL 1.2Whiles
... and for gradle use: compile 'javax:javaee-web-api:6.0'Galliard
Note that javaee-web-api only contains method stubs (no byte code). You cannot use this dependency outside the provided scope which is why I prefere the suggestion of Sa'ad.Trygve
@Pascal - "I'd prefer to only add the Servlet API as dependency" - you would do that if you are dealing with a pure servlet container (tomcat, jetty) vs a JEE compliant container (TomEE, wildfly, etc)Alidia
As JoD. said, it makes sense to add the Servlet API alone when you don't need "EJB Lite 3.1, JPA 2.0, JSP 2.2, EL 1.2, JSTL 1.2, JSF 2.0, JTA 1.1". For example, that is my need, for Vaadin development.Hinds
the javaee-web-api has been updated to <version>7.0</version>Naive
F
38

Or you can use the Central Maven Repository with the Servlet 3.0 API which is also provided for the Tomcat Server 7.0.X

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>7.0.21</version>
        <scope>provided</scope>
    </dependency>

from here: http://repo2.maven.org/maven2/org/apache/tomcat/tomcat-servlet-api/7.0.21/

Festination answered 27/9, 2011 at 13:3 Comment(0)
S
28

Here is what I use. All of these are in the Central and have sources.

For Tomcat 7 (Java 7, Servlet 3.0)

Note - Servlet, JSP and EL APIs are provided in Tomcat. Only JSTL (if used) needs to be bundled with the web app.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>2.2.4</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

For Tomcat 8 (Java 8, Servlet 3.1)

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
Sidhu answered 11/1, 2012 at 12:27 Comment(2)
This works, but the prescribed dependencies end up being in the Maven section but never included in the WAR file as they are marked as "provided". BUT ...I can never get the project to use the JARs in the Tomcat lib directory, even though I have included this Tomcat lib directory in the Eclipse build path, and they can clearly be seen there. My pom.xml can never resolve these Tomcat JARs and always requires version 3.0.1 of the servlet-api JAR from the local Maven repo, rather than the 3.0 version Tomcat supplies. I have no idea why this is ... can anyone explain?Condolence
Can you give which version of <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> can I use for tomcat 8.5 ?Fougere
D
24

Unfortunately, adding the javaee-(web)-api as a dependency doesn't give you the Javadoc or the Source to the Servlet Api to browse them from within the IDE. This is also the case for all other dependencies (JPA, EJB, ...) If you need the Servlet API sources/javadoc, you can add the following to your pom.xml (works at least for JBoss&Glassfish):

Repository:

<repository>
  <id>jboss-public-repository-group</id>
  <name>JBoss Public Repository Group</name>
  <url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>

Dependency:

<!-- Servlet 3.0 Api Specification -->
<dependency>
   <groupId>org.jboss.spec.javax.servlet</groupId>
   <artifactId>jboss-servlet-api_3.0_spec</artifactId>
   <version>1.0.0.Beta2</version>
   <scope>provided</scope>
</dependency>

I completely removed the javaee-api from my dependencies and replaced it with the discrete parts (javax.ejb, javax.faces, ...) to get the sources and Javadocs for all parts of Java EE 6.

EDIT:

Here is the equivalent Glassfish dependency (although both dependencies should work, no matter what appserver you use).

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.servlet</artifactId>
  <version>3.0</version>
  <scope>provided</scope>
</dependency>
Disguise answered 9/7, 2010 at 11:4 Comment(1)
Why do we need to specify version 1.0.0.Beta2, if it's version 3.0 that we need? This makes it complex.Weedy
R
9

The Apache Geronimo project provides a Servlet 3.0 API dependency on the Maven Central repo:

<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-servlet_3.0_spec</artifactId>
    <version>1.0</version>
</dependency>
Rigamarole answered 9/1, 2011 at 4:48 Comment(1)
This works, and seems the simplest way, thanks! BTW Apache Geronimo has much more to offer: mvnrepository.com/artifact/org.apache.geronimo.specsEgyptian
A
6

Just for newcomers.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
Auburta answered 14/1, 2015 at 14:41 Comment(0)
T
4

I found an example POM for the Servlet 3.0 API on DZone from September.

Suggest you use the java.net repo, at http://download.java.net/maven/2/

There are Java EE APIs in there, for example http://download.java.net/maven/2/javax/javaee-web-api/6.0/ with POM that look like they might be what you're after, for example:

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>6.0</version>
</dependency>

I'm guessing that the version conventions for the APIs have been changed to match the version of the overall EE spec (i.e. Java EE 6 vs. Servlets 3.0) as part of the new 'profiles'. Looking in the JAR, looks like all the 3.0 servlet stuff is in there. Enjoy!

Tourmaline answered 30/12, 2009 at 12:40 Comment(2)
Thanks, it works! The only remaining question is, if Java EE 6 Profiles replaced separate libs. (see addendum in my question)Sheepish
If you depend on this, you can't make portable war (one that works on JBoss, Tomcat, Jetty, ...), because for Tomcat/Jetty, you'll need to put part of that dependency provided (servlet) and part of it not provided (cdi), which is impossible.Weedy
W
3

A convenient way (JBoss recommended) to include Java EE 6 dependencies is demonstrated below. As a result dependencies are placed separately (not all in one jar as in javaee-web-api), source files and javadocs of the libraries are available to download from maven repository.

<properties>
    <jboss.javaee6.spec.version>2.0.0.Final</jboss.javaee6.spec.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-web-6.0</artifactId>
        <version>${jboss.javaee6.spec.version}</version>
        <scope>provided</scope>
        <type>pom</type>
    </dependency>
</dependencies>

To include individual dependencies only, dependencyManagement section and scope import can be used:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee6-specs-bom</artifactId>
                <version>${jboss.javaee6.spec.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- No need specifying version and scope. It is defaulted to version and scope from Bill of Materials (bom) imported pom. -->
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.0_spec</artifactId>
        </dependency>
    </dependencies>
Whiles answered 9/9, 2011 at 9:18 Comment(0)
M
0

Place this dependency, and dont forget to select : Include dependencies with "provided" scope

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
Mannes answered 5/11, 2020 at 23:18 Comment(1)
This is to debug in intellij, i am using jhipsterMannes
P
-3

Try this code...

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0-alpha-1</version>
    </dependency>
Pubescent answered 28/6, 2012 at 3:52 Comment(1)
Dependencies in alpha stage is not always suitable for production application.Cetus

© 2022 - 2024 — McMap. All rights reserved.