`

maven+eclipse搭建webapp总结

阅读更多
最近对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!

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

development assembly 中,添加maven dependencies 就好了!和改配置文件应该是一样的作用。
  • 大小: 66.8 KB
  • 大小: 144.7 KB
分享到:
评论
1 楼 zxjlwt 2015-10-21  
学习了。
http://surenpi.com

相关推荐

Global site tag (gtag.js) - Google Analytics