Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         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="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.test"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <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="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>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>
like image 271
Duy Anh Avatar asked Sep 07 '16 04:09

Duy Anh


1 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.

like image 156
Vinay Veluri Avatar answered Nov 14 '22 23:11

Vinay Veluri