Maven集成时依赖jar包版本控制问题

我有一个WEB工程需要用到commons-collections和commons-dbcp.
代码中用到的是commons-collections的3.1版本特性,但是资源库中commons-dbcp依赖的
commons-collections指定了2.1版本。看了下commons-dbcp-1.2.1.pom:

commons-collections
commons-collections
2.1

当我将web工程打包时,maven会将commons-collections-3.1.jar
和commons-collections-2.1.jar都拷贝进WAR包。 如何只让maven只将
commons-collections-3.1.jar拷贝到war包中去?

[b]问题补充:[/b]
谢谢,jansel的答复,方案1是可以行的,但是觉得maven应该有其它解决方案,所以发贴出来咨询一下。方案2我试过了,在我自己的工程pom.xml文件中指定了
高版本的common-collections,但是maven做package的时候依赖会将两个版本的common-collections库都打进WEB-INF/lib,我的maven版本是2.0.9.
[b]问题补充:[/b]
E:\NGWORK\javaprj\webapp>mvn dependency:tree -Dverbose -Dincludes=commons-collections

E:\NGWORK\javaprj\webapp>set MAVEN_OPTS=-Xms64m -Xmx512m
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - asiainfo.cboss:cboss:war:SNAPSHOT
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/asiainfo/cas/aicasclient/1.0/aicasclient-1.0.pom
Downloading: http://repo1.maven.org/maven2/cmcc/twcmcc/1.4/twcmcc-1.4.pom
Downloading: http://repo1.maven.org/maven2/castor/castor-xml/1.0/castor-xml-1.0.pom
Downloading: http://repo1.maven.org/maven2/asiainfo/openboss/openclient/2.0/openclient-2.0.pom
Downloading: http://repo1.maven.org/maven2/xstream/xstream/1.3/xstream-1.3.pom
Downloading: http://repo1.maven.org/maven2/jdk/tools/1.4.2/tools-1.4.2.pom
Downloading: http://repo1.maven.org/maven2/servlet/servlet/2.3/servlet-2.3.pom
Downloading: http://repo1.maven.org/maven2/asiainfo/cboss/portalExternal/1.8/portalExternal-1.8.pom
Downloading: http://repo1.maven.org/maven2/asiainfo/openbuff/EzfrmWeb/1.0/EzfrmWeb-1.0.pom
Downloading: http://repo1.maven.org/maven2/asiainfo/openbuff/EzfrmBiz/1.0/EzfrmBiz-1.0.pom
Downloading: http://repo1.maven.org/maven2/net/sf/jasperreports/2.0.3/jasperreports-2.0.3.pom
Downloading: http://repo1.maven.org/maven2/lowagie/itext/1.3.1/itext-1.3.1.pom
Downloading: http://repo1.maven.org/maven2/lowagie/itext-asian/1.0/itext-asian-1.0.pom
Downloading: http://repo1.maven.org/maven2/net/sf/json/2.2.1/json-2.2.1.pom
Downloading: http://repo1.maven.org/maven2/net/sf/ezmorph/1.0.4/ezmorph-1.0.4.pom
Downloading: http://repo1.maven.org/maven2/edu/concurrent/1.0/concurrent-1.0.pom
Downloading: http://repo1.maven.org/maven2/asiainfo/ny/nysecurity/1.0/nysecurity-1.0.pom
Downloading: http://repo1.maven.org/maven2/cmcc/trustwork/1.4/trustwork-1.4.pom
Downloading: http://repo1.maven.org/maven2/cmcc/pwdSvr/1.4/pwdSvr-1.4.pom
Downloading: http://repo1.maven.org/maven2/poi/poi/2.5.1-FINAL/poi-2.5.1-FINAL.pom
[INFO] [dependency:tree]
[INFO] asiainfo.cboss:cboss:war:SNAPSHOT
[INFO] +- asiainfo.cboss:cboss-common:jar:SNAPSHOT:compile
[INFO] | - org.apache.velocity:velocity:jar:1.5:compile
[INFO] | - (commons-collections:commons-collections:jar:3.1:compile - omitted for conflict with 2.1)
[INFO] +- commons-collections:commons-collections:jar:3.1:runtime (scope not updated to compile)
[INFO] +- commons-dbcp:commons-dbcp:jar:1.2.1:compile
[INFO] | - (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 3.1)
[INFO] +- commons-beanutils:commons-beanutils:jar:1.6:compile
[INFO] | - (commons-collections:commons-collections:jar:2.0:compile - omitted for conflict with 3.1)
[INFO] - commons-pool:commons-pool:jar:1.2:compile
[INFO] - (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 3.1)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 33 seconds
[INFO] Finished at: Wed Sep 09 13:18:59 CST 2009
[INFO] Final Memory: 13M/63M
[b]问题补充:[/b]
的确是按最短路径实现的,我在自己的pom.xml中指定了使用版本,maven就是使用这个版本的。 上午是因为忘记做clean了,所以原来已经拷贝到WEB-INF/lib目录的库没有被清理这样commons-collections-2.1.jar还是会存在, 让我误认为不能生效。

2个回答

引入

commons-dbcp

的时候把commons-collections-2.1 exclude掉就行了

或者

在你的直接的POM中引入

commons-collections

Maven是按照最短路径来的,只要你直接在POM引入了,就会用你直接引入的。

Maven2.0.9是这样的,不知道2.1这个规则有没有改,LZ试一试上面两种方法

正好我也有这么一个工程,同时指定了,但是最好只有一个jar啊。

你看一下 mvn dependency:tree 看一下2.1是谁引入的 然后就是一定要比较一下2.1和3.1的groupId和artifactId 是不是一样 万一不行就把POM文件发上来 我在我的系统上Run一把

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!