Maven package打包webapp項目遇到的問題


環境
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目錄,反之不會


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM