Issue
I am making a Maven project it is my first time with Maven. I'm using Netbeans and Tomcat server and I am not able to import any javax.servlet e.g. import javax.servlet.RequestDispatcher; etc. It looks like that:
There is info: javax.servlet does not exist and a solution proposed by Netbeans is for example: "Search Dependency at Maven Repository for javax.servlet.RequestDispatcher. When I click it then there is a pop-up window without anything to do:
I have the pom.xml file located in C://pathToNetbeansProjects/myProject/pom.xml and I added a dependency for javax-servlet now my pom.xml looks like this:
<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.glassfish.jersey.archetypes</groupId>
<artifactId>ParkingSystem</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ParkingSystem</name>
<build>
<finalName>ParkingSystem</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I have no more ideas if I am doing something wrong with my pom.xml or maybe I need to do something in Netbeans to make it work. But I don't know really what.
Solution
The problem is 99% caused by a different import done by Maven on that library.
Maven imports your libs following a hierarchical manner, so probably there's some lib that you have imported that contains the javax.servlet
, but it's not the version that you need.
First I suggest you to looking for which one is doing that for resolving the conflict by looking into maven hierarchy, you can achieve this with console command mvn dependency:tree -Dverbose
( look here for an example).
Then you can omit the unwanted libraries by a specific maven command inside your library:
<dependency>
....
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
This is an explanation useful for understanding "why" is happening this, so you can understand it.
Btw a quick fix, that you can try as first instance, is moving the import you wanted
javax.servlet
as first element of your pom.
Answered By - Leviand
Answer Checked By - Senaida (JavaFixing Volunteer)