Maven war 插件使用同一个 jar 的多个版本创建 war

Maven war plugin creates war with multiple versions of same jar

我遇到以下问题。我有一个遗留应用程序,其中 maven-war-plugin 以下列方式:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webappDirectory>${basedir}/WebRoot</webappDirectory>
        <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
    </configuration>
    <version>3.2.0</version>
</plugin>

特殊情况是项目源码树中src层有一个特殊的文件夹WebRoot

然后当我更改了某些依赖项的版本并开始构建时 mvn clean package 最后我得到了一个 project.war 和 WEB-INF/lib ,里面装满了神器的新旧版本。这在 servlet 容器中启动 war 期间造成了麻烦,因为不清楚类加载器加载了哪些 jar,并且某些 jar 存在不同的不兼容问题。

编辑: mvn dependency:tree -Dverbose

[INFO] yyy:sometool:war:0.1.15-SNAPSHOT
[INFO] +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.0.1:compile
[INFO] |  +- ch.qos.logback:logback-core:jar:1.0.1:compile
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.6.4:compile - omitted for duplicate)
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.6.4:compile
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.6.4:compile - omitted for duplicate)
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.6.4:compile
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.6.4:compile - omitted for duplicate)
[INFO] +- javax.mail:mail:jar:1.4.1:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] +- javax.servlet:jstl:jar:1.2:provided
[INFO] +- javax.servlet:servlet-api:jar:2.4:provided
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.1:provided
[INFO] +- org.codehaus.woodstox:wstx-lgpl:jar:3.2.7:compile
[INFO] |  \- stax:stax-api:jar:1.0.1:compile
[INFO] +- org.apache.struts:struts2-core:jar:2.1.8:compile
[INFO] |  +- com.opensymphony:xwork-core:jar:2.1.6:compile
[INFO] |  |  +- org.springframework:spring-test:jar:2.5.6:compile
[INFO] |  |  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.0.4)
[INFO] |  |  |  \- (junit:junit:jar:3.8.1:compile - omitted for conflict with 3.8.2)
[INFO] |  |  \- (ognl:ognl:jar:2.7.3:compile - omitted for duplicate)
[INFO] |  +- org.freemarker:freemarker:jar:2.3.15:compile
[INFO] |  +- ognl:ognl:jar:2.7.3:compile
[INFO] |  +- commons-fileupload:commons-fileupload:jar:1.2.1:compile
[INFO] |  \- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- org.apache.struts:struts2-convention-plugin:jar:2.1.8:compile
[INFO] |  \- (org.apache.struts:struts2-core:jar:2.1.8:compile - omitted for duplicate)
[INFO] +- org.apache.struts:struts2-dojo-plugin:jar:2.1.8:compile
[INFO] |  \- (org.apache.struts:struts2-core:jar:2.1.8:compile - omitted for duplicate)
[INFO] +- struts2plugin:struts2pluginLogin:jar:0.0.6:compile
[INFO] |  +- (org.slf4j:slf4j-api:jar:1.6.4:compile - omitted for duplicate)
[INFO] |  \- (ch.qos.logback:logback-classic:jar:1.0.0:compile - omitted for conflict with 1.0.1)
[INFO] +- org.apache.struts:struts2-json-plugin:jar:2.1.8:compile
[INFO] |  +- org.apache.struts:struts2-junit-plugin:jar:2.1.8:compile
[INFO] |  |  +- (junit:junit:jar:3.8.2:compile - omitted for conflict with 4.8.1)
[INFO] |  |  \- (org.apache.struts:struts2-core:jar:2.1.8:compile - omitted for duplicate)
[INFO] |  \- (org.apache.struts:struts2-core:jar:2.1.8:compile - omitted for duplicate)
[INFO] +- org.hibernate:hibernate-annotations:jar:3.3.1.GA:compile
[INFO] |  +- (org.hibernate:hibernate:jar:3.2.6.ga:compile - omitted for conflict with 3.2.5.ga)
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.0.0.ga:compile
[INFO] |  |  \- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  +- org.hibernate:ejb3-persistence:jar:1.0.1.GA:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.0.4:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.3.2.GA:compile
[INFO] |  +- (org.hibernate:hibernate:jar:3.2.6.ga:compile - omitted for duplicate)
[INFO] |  +- (org.hibernate:hibernate-annotations:jar:3.3.1.GA:compile - omitted for duplicate)
[INFO] |  +- (org.hibernate:hibernate-commons-annotations:jar:3.0.0.ga:compile - omitted for duplicate)
[INFO] |  +- (org.hibernate:ejb3-persistence:jar:1.0.1.GA:compile - omitted for duplicate)
[INFO] |  +- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  \- javassist:javassist:jar:3.4.GA:compile
[INFO] +- org.hibernate:hibernate-validator:jar:3.0.0.ga:compile
[INFO] +- org.hibernate:hibernate:jar:3.2.5.ga:compile
[INFO] |  +- net.sf.ehcache:ehcache:jar:1.2.3:compile
[INFO] |  |  +- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  |  \- (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 2.1.1)
[INFO] |  +- (javax.transaction:jta:jar:1.0.1B:compile - omitted for conflict with 1.1)
[INFO] |  +- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  +- asm:asm-attrs:jar:1.5.3:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- cglib:cglib:jar:2.1_3:compile
[INFO] |  |  \- (asm:asm:jar:1.5.3:compile - omitted for duplicate)
[INFO] |  +- asm:asm:jar:1.5.3:compile
[INFO] |  \- (commons-collections:commons-collections:jar:2.1.1:compile - omitted for conflict with 3.2.1)
[INFO] +- mysql:mysql-connector-java:jar:5.1.13:compile
[INFO] +- c3p0:c3p0:jar:0.9.1.2:compile
[INFO] +- xerces:xercesImpl:jar:2.9.1:compile
[INFO] |  \- (xml-apis:xml-apis:jar:1.3.04:compile - omitted for conflict with 1.0.b2)
[INFO] +- displaytag:displaytag:jar:1.2:compile
[INFO] |  +- (commons-collections:commons-collections:jar:3.1:compile - omitted for conflict with 2.1.1)
[INFO] |  +- commons-lang:commons-lang:jar:2.3:compile
[INFO] |  +- com.lowagie:itext:jar:1.3:compile
[INFO] |  \- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] +- javax.transaction:jta:jar:1.1:compile
[INFO] +- javax.persistence:persistence-api:jar:1.0:compile
[INFO] +- org.quartz-scheduler:quartz:jar:2.1.5:compile
[INFO] |  +- (c3p0:c3p0:jar:0.9.1.1:compile - omitted for conflict with 0.9.1.2)
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.6.1:compile - omitted for conflict with 1.6.4)
[INFO] +- org.springframework:spring-context:jar:5.0.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.0.2.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-beans:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-beans:jar:5.0.2.RELEASE:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-core:jar:5.0.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:5.0.2.RELEASE:compile
[INFO] |     \- (org.springframework:spring-core:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-webmvc:jar:5.0.2.RELEASE:compile
[INFO] |  +- (org.springframework:spring-aop:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-expression:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-web:jar:5.0.2.RELEASE:compile
[INFO] |     +- (org.springframework:spring-beans:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:5.0.2.RELEASE:compile - omitted for duplicate)
[INFO] +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] +- net.unto.twitter:java-twitter:jar:0.9-SNAPSHOT:compile
[INFO] |  +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  |  +- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  |  \- commons-codec:commons-codec:jar:1.2:compile
[INFO] |  +- (commons-io:commons-io:jar:1.4:compile - omitted for conflict with 1.3.2)
[INFO] |  +- com.google.protobuf:protobuf-java:jar:2.2.0:compile
[INFO] |  +- (commons-lang:commons-lang:jar:2.4:compile - omitted for conflict with 2.3)
[INFO] |  +- net.sf.json-lib:json-lib:jar:jdk15:2.2.3:compile
[INFO] |  |  +- (commons-beanutils:commons-beanutils:jar:1.7.0:compile - omitted for duplicate)
[INFO] |  |  +- (commons-collections:commons-collections:jar:3.2:compile - omitted for conflict with 3.2.1)
[INFO] |  |  +- (commons-lang:commons-lang:jar:2.4:compile - omitted for conflict with 2.3)
[INFO] |  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.0.4)
[INFO] |  |  \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] |  |     \- (commons-lang:commons-lang:jar:2.3:compile - omitted for duplicate)
[INFO] |  +- (joda-time:joda-time:jar:1.6:compile - omitted for conflict with 2.9.9)
[INFO] |  \- com.google.collections:google-collections:jar:0.9:compile
[INFO] +- junit:junit:jar:4.8.1:test (scope not updated to compile)
[INFO] +- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] +- joda-time:joda-time:jar:2.9.9:compile
[INFO] +- com.google.guava:guava:jar:19.0:compile
[INFO] +- utils.networking:httpclient:jar:0.1.4:compile
[INFO] |  +- (junit:junit:jar:4.10:compile - omitted for conflict with 4.8.1)
[INFO] |  +- (org.slf4j:slf4j-api:jar:1.6.4:compile - omitted for duplicate)
[INFO] |  +- (org.slf4j:log4j-over-slf4j:jar:1.6.4:compile - omitted for duplicate)
[INFO] |  +- (org.slf4j:jcl-over-slf4j:jar:1.6.4:compile - omitted for duplicate)
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.1.3:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.1.4:compile
[INFO] |  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.0.4)
[INFO] |  |  \- (commons-codec:commons-codec:jar:1.4:compile - omitted for conflict with 1.2)
[INFO] |  +- (org.springframework:spring-core:jar:3.1.1.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  +- (org.springframework:spring-context:jar:3.1.1.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  +- (org.springframework:spring-beans:jar:3.1.1.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  +- org.springframework.security:spring-security-core:jar:3.1.0.RELEASE:compile
[INFO] |  |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  +- (org.springframework:spring-expression:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-aop:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-context:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-core:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  \- org.springframework.security:spring-security-crypto:jar:3.1.0.RELEASE:compile
[INFO] |  |     \- (org.springframework:spring-core:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  +- org.springframework.security:spring-security-web:jar:3.1.0.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:3.0.6.RELEASE:compile
[INFO] |  |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  |  |  +- (org.springframework:spring-aop:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  |  +- (org.springframework:spring-beans:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  |  +- (org.springframework:spring-context:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  |  \- (org.springframework:spring-core:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-web:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-expression:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-aop:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework.security:spring-security-core:jar:3.1.0.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- org.springframework:spring-jdbc:jar:3.0.6.RELEASE:compile
[INFO] |  |  |  +- (org.springframework:spring-beans:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  |  +- (org.springframework:spring-core:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  |  \- (org.springframework:spring-tx:jar:3.0.6.RELEASE:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-context:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  |  \- (org.springframework:spring-core:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |  \- org.springframework.security:spring-security-config:jar:3.1.0.RELEASE:compile
[INFO] |     +- (org.springframework:spring-context:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |     +- (org.springframework:spring-beans:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |     +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-aop:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] |     +- (org.springframework.security:spring-security-core:jar:3.1.0.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:3.0.6.RELEASE:compile - omitted for conflict with 5.0.2.RELEASE)
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.9.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.3:compile
[INFO] |  +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile - omitted for conflict with 2.9.3)
[INFO] |  \- (com.fasterxml.jackson.core:jackson-core:jar:2.9.3:compile - omitted for duplicate)
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.3:compile
[INFO] \- org.mockito:mockito-core:jar:1.9.0:compile
[INFO]    +- org.hamcrest:hamcrest-core:jar:1.1:compile
[INFO]    \- org.objenesis:objenesis:jar:1.0:compile

我找到了解决办法。问题真的出在这个插件配置上:

<configuration>
    <webappDirectory>${basedir}/WebRoot</webappDirectory>
    <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
</configuration>

webappDirectory 根据定义,maven 放爆炸的地方 war。所以在 mvn clean package 之后 ${basedir}/WebRoot 下有一个 lib 文件夹,里面装满了 pom 中的所有依赖库。另一方面,此文件夹用于打包成 war。因此,每次我更新某个依赖项的版本并启动 mvn clean package 时,都会将新版本的依赖项放入 ${basedir}/WebRoot/lib 而不会删除以前的版本。在下一个 mvn clean package 之后,它将整个 lib 文件夹打包到 war.

解决方法是,不要为 warSourceDirectorywebappDirectory 使用同一个文件夹。将 webappDirectory 指向 <webappDirectory>${project.build.directory}/XYZ</webappDirectory>

的某处