IntelliJ IDEA 16将maven依赖项添加到classpath

时间:2016-09-07 04:13:41

标签: java spring maven intellij-idea

我是IntelliJ的新手并使用2016.2版本。以前我在使用Eclipse。我正在尝试建立一个简单的maven spring测试项目,但我无法弄清楚出了什么问题。

注意:我知道异常意味着什么,我知道使用Eclipse的解决方案

注意2:我尝试了一个干净的Idea安装

根据我的理解,想法会自动包含maven依赖项(如果我错了,请纠正我)

编辑1: 解决方案

  1. 项目 - >右键单击 - >添加框架支持 - >检查Spring / Spring MVC
  2. 添加<packaging>war</packaging>
  3. 重新导入maven依赖项
  4. 我尝试做什么

    1. 重新导入maven依赖项
    2. 关闭IntelliJ并删除所有* .iml文件和所有.idea文件夹
    3. 异常

      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>
      

5 个答案:

答案 0 :(得分:8)

  

根据我的理解,想法将包括maven依赖项   自动(纠正我,如果我错了)

是的,如果在提示时检查了Auto Import,那么当您打开IntelliJ时会被点击。

如果没有,请点击Intellij右侧窗格中的Maven Projects,然后在refresh - &gt;之后点击按钮。 Generate sources and auto import。这会再次触发该过程。

Maven-IntelliJ

如果上述方法不起作用且仍有IDE问题,请转到File -> Invalidate Cache/Restart选项。提示如下。

enter image description here

单击Invalidate and restart,这将重新索引所有依赖项到工作区。

答案 1 :(得分:2)

对项目中的所有pom执行以下操作:

右键单击pom.xml并单击“添加为maven项目”。

答案 2 :(得分:1)

要强制重新导入,请从“视图”菜单(或默认停靠的屏幕右侧)打开maven项目工具窗口,然后按最左侧的图标(看起来像带有箭头的蓝色圆圈) )。
这应该强制IntelliJ从头开始解析pom并导入它缺少的任何依赖项 如果自动导入不能按预期工作,通常比重启和清除缓存更快。

答案 3 :(得分:0)

转到文件 - &gt;设置 - &gt;构建,执行,部署 - &gt; buildtools - &gt; maven maven主目录到您的主目录。 如果它不起作用,请在文件 - >设置 - &gt;构建,执行,部署 - &gt; buildtools - &gt; maven-&gt;导入标签中,自动检查导入maven项目。

答案 4 :(得分:0)

根据接受的答案,如果有人在提示时忘记检查自动导入并且 maven 构建未在外部库下显示任何 Maven 依赖项,请尝试此操作(对于那些在 2021 年看到的人)。

<块引用>

右键单击项目文件夹

<块引用>

Maven->重新导入

enter image description here