Coin163

首页 > 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之一 : Maven的pom.xml环境配置及注意方面

基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之一 : Maven的pom.xml环境配置及注意方面

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

        先说下我们的开发环境,用的Eclipse版本为最新下载的Eclipse for JavaEE, Mars.2 Release (4.5.2),已经内置了maven,因为外面maven越来越流行,而且管理库依赖非常方便,所以也顺便用学习学习,但这几天的使用感觉,eclipse下的maven有点傻,而且会无缘无故让项目的配置参数丢失,最后不知道动了什么修改的xml文件、java文件不会自动编译了亦不会同步到tomcat的调试目录,为此不得不重新恢复之前的备份。

        用的框架版本:spring 4.2.0.RELEASE, hibernate-core/hibernate-entitymanager版本4.3.5.Final,postgresql jdbc版本9.4.1208

下面给出完整的pom.xml, pom配置对spring来说至关重要,因为一不小心配置里面有不同版本的依赖同时存在,就很可以让整个spring加载不起来!

(16/Jun/2016重新修改)

<!-- http://mvnrepository.com/ 想要什麽,通過這個URL查找 -->
<!-- 
 -->
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.freestyle.gpchat</groupId>
	<artifactId>gpchat</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>gpchat Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<spring.version>4.2.0.RELEASE</spring.version>
		<struts.version>2.3.28.1</struts.version>
		<aspectj.version>1.8.7</aspectj.version>
		<spring-hibernate.version>4.3.5.Final</spring-hibernate.version>
		<hibernate.version>3.2.7.ga</hibernate.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- Spring dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</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-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-jdbc</artifactId>
    		<version>${spring.version}</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/org.springframework/spring-orm -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-orm</artifactId>
		    <version>${spring.version}</version>
		</dependency>
		
		<!-- http://mvnrepository.com/artifact/org.springframework/spring-aop -->
		<!-- 
		<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-aop</artifactId>
    		<version>${spring.version}</version>
		</dependency>
		 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${aspectj.version}</version>
		</dependency>		
   		<!-- http://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
		<dependency>
    		<groupId>org.aspectj</groupId>
    		<artifactId>aspectjweaver</artifactId>
    		<version>1.8.9</version>
		</dependency>

<!-- http://mvnrepository.com/artifact/javassist/javassist -->
<!-- 
<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.8.1.GA</version>
</dependency>
	 -->	


		<!-- Struts2 -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>${struts.version}</version>
			<exclusions>
				<exclusion>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>

                <exclusion> <!-- we prefer our explicit version, though it should be the same -->
                    <groupId>asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion> <!-- we prefer our explicit version, though it should be the same -->
                    <groupId>asm</groupId>
                    <artifactId>asm-commons</artifactId>
                </exclusion>
                <exclusion> <!-- we prefer our explicit version, though it should be the same -->
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion> <!-- we prefer our explicit version, though it should be the same -->
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm-commons</artifactId>
                </exclusion>
			</exclusions>
		</dependency>

		<!-- http://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>${struts.version}</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-convention-plugin</artifactId>
			<version>${struts.version}</version>
		</dependency>

		<!-- 添加Hibernate依赖 -->
		<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${spring-hibernate.version}</version>
			<exclusions>
        		<exclusion>
            		<groupId>org.javassist</groupId>
            		<artifactId>javassist</artifactId>
        		</exclusion>
    		</exclusions>			
		</dependency>
		<!-- http://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
		<!-- -<dependency>
			<groupId>org.hibernate.javax.persistence</groupId>
			<artifactId>hibernate-jpa-2.0-api</artifactId>
			<version>1.0.1.Final</version>
		</dependency>
		 -->
		<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-entitymanager</artifactId>
		    <version>4.3.11.Final</version>
		    <!-- 
			<exclusions>
        		<exclusion>
            		<groupId>org.javassist</groupId>
            		<artifactId>javassist</artifactId>
        		</exclusion>
    		</exclusions>
    		 -->		    
		</dependency>
		
		
					
		<!-- http://mvnrepository.com/artifact/org.postgresql/postgresql -->
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.4.1208</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/com.cloudhopper.proxool/proxool -->
		<dependency>
			<groupId>com.cloudhopper.proxool</groupId>
			<artifactId>proxool</artifactId>
			<version>0.9.1</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.3.2</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.3.2</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.6.3</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.6.3</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.6.3</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/javax.mail/mail -->
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/commons-logging/commons-logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
		<dependency>
			<groupId>com.belerweb</groupId>
			<artifactId>pinyin4j</artifactId>
			<version>2.5.0</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/com.google.zxing/core -->
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>core</artifactId>
			<version>3.2.0</version>
		</dependency>
		<!-- http://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-lang3</artifactId>
		    <version>3.0</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
		<dependency>
		    <groupId>commons-beanutils</groupId>
		    <artifactId>commons-beanutils</artifactId>
		    <version>1.9.2</version>
		</dependency>

<!-- http://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.1</version>
</dependency>


	</dependencies>
	<build>
		<finalName>gpchat</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>  
        <testSourceDirectory>src/test/java</testSourceDirectory>  
  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
            </resource>  
        </resources>  
        <testResources>  
            <testResource>  
                <directory>src/test/resources</directory>  
            </testResource>  
        </testResources>  
  
        <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>  
        <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>  
  
        <plugins>  
  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.2</version>  
                <configuration>  
                    <source>1.6</source>  
                    <target>1.6</target>  
                </configuration>  
            </plugin>  
  
            <plugin>  
                <artifactId>maven-resources-plugin</artifactId>  
                <version>2.5</version>  
                <executions>  
                    <execution>  
                        <phase>compile</phase>  
                    </execution>  
                </executions>  
            </plugin>  
  
            
  
            <plugin>  
                <artifactId>maven-clean-plugin</artifactId>  
                <version>2.4.1</version>  
                <configuration>  
                    <filesets>  
                        <fileset>  
                            <directory>src/main/webapp/WEB-INF/lib</directory>  
                            <followSymlinks>false</followSymlinks>  
                        </fileset>  
                    </filesets>  
                </configuration>  
            </plugin>  
  
        </plugins>  
  
    		
	</build>
</project>



    另外,需要注意的是如果tomcat启动加载spring失败,提示spring组件类找不到,但如果复制这个类在ide里面随便打开个java文件用其定义变量又可以解析成功,这时候通常是maven的依赖库没有分发到tomcat的调试目录 ,而且是用着用着就丢失的,真不知道做错了什么动作,解决办法是右键打开project的properties,找到 deployment assembly,将maven依赖分发出去,如下图:




maven install后,请打开target\xxx\WEB-INF\lib,仔细检查是否有同一个jar是否有不同版本存在,如果有,则说明pom里面有重复配置,需要删除,按照上面的pom.xml运行,我的jar file list 如下:

2016/06/01  09:21            62,983 activation-1.1.jar
2015/09/11  10:56           445,288 antlr-2.7.7.jar
2015/09/11  10:56             4,467 aopalliance-1.0.jar
2016/06/16  11:59            47,895 asm-4.2.jar
2015/12/25  16:37           116,927 aspectjrt-1.8.7.jar
2016/06/14  11:53         1,864,922 aspectjweaver-1.8.9.jar
2016/01/18  17:35            32,112 avalon-framework-api-4.3.jar
2016/01/18  17:35            83,582 avalon-logkit-2.1.jar
2016/06/16  11:59           283,080 cglib-3.1.jar
2016/06/15  12:17           233,859 commons-beanutils-1.9.2.jar
2016/05/31  17:51           232,771 commons-codec-1.6.jar
2015/09/11  10:56           575,389 commons-collections-3.2.1.jar
2016/01/19  09:17            69,002 commons-fileupload-1.3.1.jar
2016/01/19  09:17           173,587 commons-io-2.2.jar
2016/06/15  12:14           306,008 commons-lang3-3.0.jar
2015/09/12  10:49            60,686 commons-logging-1.1.1.jar
2016/06/01  09:21           544,173 core-3.2.0.jar
2016/05/31  17:14           313,898 dom4j-1.6.1.jar
2016/05/31  17:42         1,300,487 freemarker-2.3.22.jar
2016/01/18  17:35            96,369 geronimo-spec-javamail-1.3.1-rc3.jar
2016/01/18  17:35            30,838 geronimo-spec-jms-1.1-rc4.jar
2016/06/14  10:46            75,311 hibernate-commons-annotations-4.0.4.Final.jar
2016/06/14  10:46         5,230,007 hibernate-core-4.3.5.Final.jar
2016/06/14  10:22           648,087 hibernate-entitymanager-4.3.11.Final.jar
2016/06/13  17:01           113,371 hibernate-jpa-2.1-api-1.0.0.Final.jar
2016/05/31  17:50           587,762 httpclient-4.3.2.jar
2016/05/31  17:49           282,269 httpcore-4.3.2.jar
2016/05/31  17:49            37,148 httpmime-4.3.2.jar
2016/05/31  17:53            46,989 jackson-annotations-2.6.3.jar
2016/05/31  17:53           258,875 jackson-core-2.6.3.jar
2016/05/31  17:52         1,170,801 jackson-databind-2.6.3.jar
2016/06/14  10:47            76,551 jandex-1.1.0.Final.jar
2016/06/14  10:47           714,194 javassist-3.18.1-GA.jar
2016/06/14  10:46            57,183 jboss-logging-3.1.3.GA.jar
2016/06/14  10:22            11,558 jboss-logging-annotations-1.2.0.Beta1.jar
2016/06/14  10:22            27,717 jboss-transaction-api_1.2_spec-1.0.0.Final.jar
2015/12/22  12:09           414,240 jstl-1.2.jar
2016/06/01  09:20           489,884 log4j-1.2.17.jar
2016/06/01  09:21           521,157 mail-1.4.7.jar
2016/05/31  17:33           230,352 ognl-3.0.14.jar
2016/06/01  09:21           209,862 pinyin4j-2.5.0.jar
2016/05/31  17:43           648,601 postgresql-9.4.1208.jar
2016/01/18  17:35           200,759 proxool-0.9.1.jar
2016/01/18  15:35           334,469 proxool-cglib-0.9.1.jar
2016/01/18  17:35            77,977 servlet-api-2.3.jar
2016/06/14  10:20           363,974 spring-aop-4.2.0.RELEASE.jar
2016/06/14  10:20           729,550 spring-beans-4.2.0.RELEASE.jar
2016/06/14  10:21         1,084,471 spring-context-4.2.0.RELEASE.jar
2016/06/14  10:19         1,052,383 spring-core-4.2.0.RELEASE.jar
2016/06/14  10:21           262,391 spring-expression-4.2.0.RELEASE.jar
2016/06/14  10:22           421,645 spring-jdbc-4.2.0.RELEASE.jar
2016/06/14  10:52           465,589 spring-orm-4.2.0.RELEASE.jar
2016/06/14  10:22           262,961 spring-tx-4.2.0.RELEASE.jar
2016/06/14  10:19           765,877 spring-web-4.2.0.RELEASE.jar
2016/06/14  10:21           865,404 spring-webmvc-4.2.0.RELEASE.jar
2016/05/31  17:33            67,524 struts2-convention-plugin-2.3.28.1.jar
2016/05/31  17:31           874,267 struts2-core-2.3.28.1.jar
2016/05/31  17:33            21,696 struts2-spring-plugin-2.3.28.1.jar
2016/05/31  17:15           109,318 xml-apis-1.0.b2.jar
2016/05/31  17:33           677,347 xwork-core-2.3.28.1.jar


原文

        先说下我们的开发环境,用的Eclipse版本为最新下载的Eclipse for JavaEE, Mars.2 Release (4.5.2),已经内置了maven,因为外面maven越来越流行,而且管理库依赖非常方便,所以也顺便用

------分隔线----------------------------
相关推荐