环境
Java: JDK_1.7.0_79
Eclipse: Mars(4.5.0)
Maven: 3.3.3
最近公司同事重构某Java web项目,完成之后发现部署启动总是不成功
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.test.test.po.Test
at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:3179)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:178)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:4006)
... 42 more
项目原结构为
test-parent
|-test-common
|-test-dao
|-test-po
|-test-vo
|-test-service
|-test-api
其中test-po包含了Hibernate的数据库映射文件
项目采用SpringMVC+Hibernate
spring: 3.2.14.RELEASE
hibernate: 3.0
applicationContext.xml中的hibernate配置如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.query.substitutions">Y</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath*:com/test/test/test/po/**/*.xml</value>
</list>
</property>
</bean>
使用mvn clean package -Denv=qa打包,一直没有问题,经过重构之后项目结构为
test-api, 即所有类及文件包含在一个项目内,无论使用package或者install打包war后,生成的文件都报上文的错误。经同事定位,是由于生成的war的lib目录下包含了一个jar文件(打包当前webapp项目的.class和resources文件),删掉此文件即不会报错,于是最后修改了maven-war-plugin的配置archiveClasses为false解决问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>artifact-package</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<archiveClasses>false</archiveClasses>
<warSourceExcludes>WEB-INF/lib/servlet-*</warSourceExcludes>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</execution>
</executions>
</plugin>
引申:
maven package打包webapp项目时
a)若没有pom的<name>test-api</name>属性时,必须设置maven-war-plugin
b)若含有pom的<name>test-api</name>属性时,可以不设置maven-war-plugin,但会提示web.xml已存在
[INFO] WEB-INF/web.xml already added, skipping
c)maven-war-plugin的archiveClasses属性若设置为true,webapp模块会生成jar并存在lib目录,反之不会