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

标签 java maven maven-war-plugin

我遇到以下问题。我有一个遗留应用程序,其中 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

enter image description here

然后,当我更改了某些依赖项的版本并开始构建时 mvn clean package 我最终得到了一个带有 WEB-INF/lib 的project.war,其中充满了新旧版本的 Artifact 。这在 servlet 容器的 war 开始时会带来麻烦,因为不清楚类加载器加载了哪些 jar,并且某些 jar 存在不同的不兼容问题。

编辑: mvn 依赖项: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 。

解决方案是,不要对 warSourceDirectory 使用相同的文件夹和webappDirectory 。点webappDirectory某处进入<webappDirectory>${project.build.directory}/XYZ</webappDirectory>

关于java - Maven war 插件使用同一 jar 的多个版本创建 war,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48211324/

相关文章:

java - 如何将变量添加到数组列表,然后对数组列表的所有元素求和?

java - 在 pom.xml 中制作 deb 包的最佳实践是什么?

spring - 无法使用最新的 Firebase SDK 依赖项解析 FirebaseOptions

java - 使用 Maven 构建 .war 文件。缺少文件和目录

java - 在Java中对对象数组进行冒泡排序

JavaFX:将线附加到路径作为动画

java - GWT。排除共享域对象以分离 Maven 模块

java - 优先打包排除和打包包含maven war插件

Maven:为什么我得到 servlet-api-2.5

java - 如何使用 Java 编写 PDF