最近对maven进行了试探性的研究,今天对其进行总结!感谢snowolf 同学的大力支持!基础的maven介绍与配置请参见 Maven零散笔记――常用配置,这里仅对一些容易出现问题的地方进行说明!
一、别忘了修改project文件
当我们使用maven创建一个web项目后,首先要记得修改project文件哦!
自动生成的文件:
<buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.maven.ide.eclipse.maven2Builder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> <nature>org.maven.ide.eclipse.maven2Nature</nature> </natures>
需要替换的部分证实buildSpec和natures。
<buildSpec> <buildCommand> <name>org.eclipse.wst.jsdt.core.javascriptValidator</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.wst.common.project.facet.core.builder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.maven.ide.eclipse.maven2Builder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.wst.validation.validationbuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.springframework.ide.eclipse.core.springbuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.springframework.ide.eclipse.core.springnature</nature> <nature>org.eclipse.jem.workbench.JavaEMFNature</nature> <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> <nature>org.maven.ide.eclipse.maven2Nature</nature> <nature>org.eclipse.wst.common.project.facet.core.nature</nature> <nature>org.eclipse.wst.jsdt.core.jsNature</nature> </natures>
有人要问了,为什么需要改project啊?原因很简单,因为我们下一步要设置project facets。你可能发现在未修改project时,project facets在properties里是根本没有的!

二、如果项目中需要servlet-api-2.4.jar等tomcat的lib库中已有的jar包,请将jar包放在WEB-INF的lib下,而不是使用pom从maven检出,因为这样会引起tomcat启动后的异常
javax.servlet.ServletException: java.lang.NoSuchMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
等。
三、jsp相关jar包jstl和standard
<dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
四、神奇的class not found!

我的项目用到了spring,在启动时遇到了神奇的 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener!而在maven Dependencies中相应的jar包已经存在了,这是为什么呢?我在.classpath文件中找到了解决办法!
原始calsspath文件
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry excluding="**|src/main/java/|src/main/java/" kind="src" output="target/classes" path="src/main/resources"/> <classpathentry kind="src" path="com.main.java"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> <classpathentry kind="output" path="target/classes"/> </classpath>
修改后的classpath文件,对org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER 添加了一个attribute,并添加了一个classpathentry!
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> </attributes> </classpathentry>
刷新项目,重启!一切OK!