maven+eclipse搭建webapp总结

最近对maven进行了试探性的研究,今天对其进行总结!感谢[url=http://snowolf.iteye.com/]snowolf[/url] 同学的大力支持!基础的maven介绍与配置请参见 [url=http://snowolf.iteye.com/blog/953735]Maven零散笔记——常用配置[/url],这里仅对一些容易出现问题的地方进行说明!
一、别忘了修改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里是根本没有的!

[img]http://dl.iteye.com/upload/attachment/0071/4137/4d8d32c3-acbb-3794-ab23-5b2dd68bdf0d.jpg[/img]

二、如果项目中需要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!


[img]http://dl.iteye.com/upload/attachment/0071/4139/9c2e110e-9c09-37e6-8325-d50f73e899d9.jpg[/img]

我的项目用到了spring,在启动时遇到了神奇的 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener!而在maven Dependencies中相应的jar包已经存在了,这是为什么呢?我在.classpath文件中找到了解决办法!
原始calsspath文件
[code]
<?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>
[/code]

修改后的classpath文件,对org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER 添加了一个attribute,并添加了一个classpathentry!

[code]
<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>
[/code]

刷新项目,重启!一切OK!

后记:
关于eclipse中class not found的问题
最近换了新版本的eclipse,发现这个问题只需要在项目中设置一下就OK了!

development assembly 中,添加maven dependencies 就好了!和改配置文件应该是一样的作用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值