Eclipse中的Maven项目应该有一个叫Maven Dependencies的library,如下:
也就是在Configure Build Path界面中的Maven Dependencies:
有时候这个库会消失,这时候在Configure Build Path界面中点击Add Library按钮是没啥用的,我试了几次都添加失败了
正确的方法有两种:
1,在项目上右键-->Maven--> Update Project
Update Project功能本身是用于当项目结构发生一些变化时更新项目,主要是更新.classpath文件,让Eclipse按照最新的.classpath文件的内容处理项目。
2,直接改.classpath文件
Maven Dependencies 这个library在.classpath文件中是这么标记的:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
把这段添加进去.classpath文件,放在<classpath>标签下,比如这样:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry including="**/*.java" kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
就可以了