IntelliJ IDEA 16 add maven dependencies to classpath

I am new to IntelliJ and using 2016.2 version. Previously I was using Eclipse. I am trying to set up a simple maven spring test project, however I can't figure out what is wrong.

Note: I know what the exception means, and I know the solution using Eclipse

Note 2: I tried on a clean Idea installation

As per my understanding, idea will include maven dependencies automatically (correct me if i'm wrong)

edit 1: Solution

  1. Project -> Right Click -> Add Framework Support -> Check Spring / Spring MVC
  2. add <packaging>war</packaging>
  3. Re-import maven dependencies

What I tried to do

  1. Re-import maven dependencies
  2. Close IntelliJ and remove all *.iml files and all .idea folders

Exception

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi="" xsi:schemaLocation=" " version="3.1"> <servlet> <servlet-name>sample2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sample2</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
</web-app>

sample2-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xmlns:context="" xmlns:mvc="" xsi:schemaLocation=" "> <context:component-scan base-package="com.test"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean>
</beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <modelVersion>4.0.0</modelVersion> <groupId>com.sa</groupId> <artifactId>sample2</artifactId> <version>1.0-SNAPSHOT</version> <properties> <spring.version>3.2.17.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
</project>

5 Answers

As per my understanding, idea will include maven dependencies automatically (correct me if i'm wrong)

Yes, if Auto Import is checked in when it prompts as and when you open the IntelliJ is clicked.

If not, please click on Maven Projects on the right side pane of the Intellij and click on button after refresh -> Generate sources and auto import. This triggers the process again.

Maven-IntelliJ

If the above doesn't work and still you have problem with IDE, go to File -> Invalidate Cache/Restart option. That prompts as below.

enter image description here

Click on Invalidate and restart, this will re-index all the dependencies to the workspace.

3

Do the following to all poms in the project:

Right click on pom.xml and "Add as maven project".

To force a reimport, open the maven projects tool window from the "view" menu (or the right side of the screen where it's docked by default) and press the left most icon (looks like a blue circle with arrows in it).
This should force IntelliJ to parse the pom from scratch and import any dependencies it's missing.
Often faster than a restart and clearing your caches if an automatic import doesn't quite work as expected.

Go to File->settings-->build,Execution,Deployment-->buildtools-->maven maven home directory to your home directory. If it does not work, in File->settings-->build,Execution,Deployment-->buildtools-->maven->importing tab,check import maven projects automatically.

1

As per accepted answer, if someone forget to check Auto Import when prompted and maven build is not showing any Maven dependencies under External libraries, try this (for those who see in 2021).

Right Click on the Project Folder

Maven->Re-Import

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like