java IntelliJ IDEA 16 将 maven 依赖项添加到类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39361087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ IDEA 16 add maven dependencies to classpath
提问by Duy Anh
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.
我是 IntelliJ 的新手并使用 2016.2 版本。以前我使用的是Eclipse。我正在尝试建立一个简单的 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)
根据我的理解,idea 将自动包含 maven 依赖项(如果我错了,请纠正我)
edit 1:Solution
编辑1:解决方案
- Project -> Right Click -> Add Framework Support -> Check Spring / Spring MVC
- add
<packaging>war</packaging>
- Re-import maven dependencies
- 项目 -> 右键单击 -> 添加框架支持 -> 检查 Spring / Spring MVC
- 添加
<packaging>war</packaging>
- 重新导入maven依赖
What I tried to do
我试图做的
- Re-import maven dependencies
- Close IntelliJ and remove all *.iml files and all .idea folders
- 重新导入maven依赖
- 关闭 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>
回答by VinayVeluri
As per my understanding, idea will include maven dependencies automatically (correct me if i'm wrong)
根据我的理解,idea 将自动包含 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
. This triggers the process again.
如果没有,请单击Maven Projects
Intellij 的右侧窗格,然后单击refresh
->之后的按钮Generate sources and auto import
。这将再次触发该过程。
If the above doesn't work and still you have problem with IDE, go to File -> Invalidate Cache/Restart
option. That prompts as below.
如果上述方法不起作用并且您仍然遇到 IDE 问题,请转到File -> Invalidate Cache/Restart
选项。那提示如下。
Click on Invalidate and restart
, this will re-index all the dependencies to the workspace.
单击Invalidate and restart
,这将重新索引工作区的所有依赖项。
回答by Shell_Leko
Do the following to all poms in the project:
对项目中的所有poms执行以下操作:
Right click on pom.xml and "Add as maven project".
右键单击 pom.xml 并“添加为 maven 项目”。
回答by jwenting
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.
要强制重新导入,请从“视图”菜单(或默认停靠的屏幕右侧)打开 Maven 项目工具窗口,然后按最左侧的图标(看起来像一个带有箭头的蓝色圆圈)。
这应该会强制 IntelliJ 从头开始解析 pom 并导入它缺少的任何依赖项。
如果自动导入没有按预期工作,通常比重新启动和清除缓存更快。
回答by rathna
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.
转到 File->settings-->build,Execution,Deployment-->buildtools-->maven maven home directory 到你的 home 目录。如果不行,在File->settings-->build,Execution,Deployment-->buildtools-->maven->importing tab,勾选自动导入maven项目。