zoukankan      html  css  js  c++  java
  • 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目录,反之不会

  • 相关阅读:
    整数划分递归模板
    最近点对算法模板
    计算几何模板
    poj1269---直线位置关系
    poj1017----模拟
    MVC 提交List 集合 注意对应的参数名称
    使用 WebClient 來存取 GET,POST,PUT,DELETE,PATCH 網路資源
    对路径访问拒绝,要加上具体filename/c.png
    sql 列集合
    百度地图 Infowidow 内容(content 下标签) 点击事件
  • 原文地址:https://www.cnblogs.com/hiver/p/6386994.html
Copyright © 2011-2022 走看看