简体   繁体   English

IntelliJ IDEA 16 将 Maven 依赖项添加到类路径

[英]IntelliJ IDEA 16 add maven dependencies to classpath

I am new to IntelliJ and using 2016.2 version.我是 IntelliJ 的新手并使用 2016.2 版本。 Previously I was using Eclipse.以前我使用的是Eclipse。 I am trying to set up a simple maven spring test project, however I can't figure out what is wrong.我正在尝试建立一个简单的 maven spring 测试项目,但是我无法弄清楚出了什么问题。

Note: I know what the exception means, and I know the solution using Eclipse注意:我知道异常意味着什么,我知道使用Eclipse的解决方案

Note 2: I tried on a clean Idea installation注 2:我尝试了一个干净的 Idea 安装

As per my understanding, idea will include maven dependencies automatically (correct me if i'm wrong)根据我的理解,想法将自动包含 maven 依赖项(如果我错了,请纠正我)

edit 1: Solution编辑1:解决方案

  1. Project -> Right Click -> Add Framework Support -> Check Spring / Spring MVC项目 -> 右键单击​​ -> 添加框架支持 -> 检查 Spring / Spring MVC
  2. add <packaging>war</packaging>添加<packaging>war</packaging>
  3. Re-import maven dependencies重新导入maven依赖

What I tried to do我试图做的

  1. Re-import maven dependencies重新导入maven依赖
  2. Close IntelliJ and remove all *.iml files and all .idea folders关闭 IntelliJ 并删除所有 *.iml 文件和所有 .idea 文件夹

Exception异常

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

web.xml网页.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 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 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>

As per my understanding, idea will include maven dependencies automatically (correct me if i'm wrong)根据我的理解,想法将自动包含 maven 依赖项(如果我错了,请纠正我)

Yes, if Auto Import is checked in when it prompts as and when you open the IntelliJ is clicked.是的,如果在提示时签入Auto Import ,并在您打开 IntelliJ 时单击。

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 .如果没有,请单击 Intellij 右侧窗格中的Maven Projects并在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.如果上述方法不起作用并且您仍然遇到 IDE 问题,请转到File -> Invalidate Cache/Restart选项File -> Invalidate Cache/Restart That prompts as below.那提示如下。

在此处输入图片说明

Click on Invalidate and restart , this will re-index all the dependencies to the workspace.单击Invalidate and restart ,这会将所有依赖项重新索引到工作区。

Do the following to all poms in the project:对项目中的所有poms执行以下操作:

Right click on pom.xml and "Add as maven project".右键单击 pom.xml 并“添加为 maven 项目”。

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).要强制重新导入,请从“视图”菜单(或默认停靠的屏幕右侧)打开 Maven 项目工具窗口,然后按最左侧的图标(看起来像一个带有箭头的蓝色圆圈)。
This should force IntelliJ to parse the pom from scratch and import any dependencies it's missing.这应该会强制 IntelliJ 从头开始​​解析 pom 并导入它缺少的任何依赖项。
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.转到 File->settings-->build,Execution,Deployment-->buildtools-->maven maven home directory 到你的 home 目录。 If it does not work, in File->settings-->build,Execution,Deployment-->buildtools-->maven->importing tab,check import maven projects automatically.如果不行,在File->settings-->build,Execution,Deployment-->buildtools-->maven->importing tab,勾选自动导入maven项目。

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

Right Click on the Project Folder右键单击项目文件夹

Maven->Re-Import Maven->重新导入

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM