0

Scenario

I am trying to add server's servlet-api into my existing Dynamic Web Application after reading BalusC's answer on this post.

So from start in am using maven dependency for my project

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${servlet.version}</version>
    <scope>provided</scope>
</dependency>

After reading the BalusC's answer I removed the javax.servlet dependency from pom.xml and I added *Target Runtime into my project.

*Target Runtime

Problem

After this all the errors (in eclipse's window) have gone. But when I try to maven clean install it gives [ERROR] COMPILATION ERROR : package javax.servlet does not exist.

But again if I added the javax.servlet's dependency in pom.xml then it works fine.

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>IncidentManagement</groupId>
    <artifactId>IncidentManagement</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src.main.java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <java.version>1.8</java.version>
        <junit.version>4.12</junit.version>
        <servlet.version>3.1.0</servlet.version>
        <mojarra.version>2.2.12</mojarra.version>
        <primefaces.version>5.3</primefaces.version>
        <maven.compiler.plugin.version>3.3</maven.compiler.plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>

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

        <!-- ORACLE database driver -->

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency> 

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>

        <!-- <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency> -->
        <!-- Mojarra JSF -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${mojarra.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${mojarra.version}</version>
        </dependency>
        <!-- PrimeFaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces.version}</version>
        </dependency>

        <!-- Excel and CSV -->

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
            <type>jar</type>
        </dependency>

        <!-- PDF -->

        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

        <!-- JSON -->
        <dependency>
           <groupId>com.googlecode.json-simple</groupId>
           <artifactId>json-simple</artifactId>
           <version>1.1.1</version>
        </dependency>

        <!-- Primefaces Theme -->
        <!-- https://mvnrepository.com/artifact/org.primefaces.extensions/all-themes -->
        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.8</version>
        </dependency>


    </dependencies>

</project>

P.S: I am using Java 8, Apache tomcat 8 and maven 3.

Community
  • 1
  • 1
Junaid
  • 2,572
  • 6
  • 41
  • 77
  • 1
    I'm assuming you mean this post http://stackoverflow.com/a/4076706/3385618 when you write about BalusC's answer. That answer does say **Never carry around loose server-specific JAR files** but it also says, two paragraphs below that: "In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as provided". So keep the dependency on servlet-api with scope=provided and you should be good. – Bewusstsein Mar 20 '17 at 10:02
  • @Bewusstsein can you tell me that how determine that which version of `servlet-api` should I use in the `pom.xml` w.r.t. server ? – Junaid Mar 20 '17 at 10:37
  • according to this https://tomcat.apache.org/whichversion.html with Tomcat 8 you can use up to version 3.1. – Bewusstsein Mar 20 '17 at 11:27

2 Answers2

0

It's quite correct in stating it can't find the class as you don't have it in your pom file...

Having the dependency there as "provided" is correct. At compile time it'll use stubs downloaded from a repository, at runtime it will use the actual classes installed into and provided by the servlet container.

Balus's answer isn't relevant to maven projects, only native Eclipse projects that aren't built externally to Eclipse.

jwenting
  • 5,505
  • 2
  • 25
  • 30
-1

@Junaid, I had the same problem at my workplace. Later I realized that servlet-api.jar file was not the repository provided by workplace. I guess, we are behind the proxy and that downloads the file from local repository than maven. I had to use the j2ee.jar file for the purpose.