1

由于我的存储库中有重复的 jar,我收到了重复的类错误。Jars 带有一个开源 Apache 项目的子模块 pom, project is James, sub module is container-spring

这是 James 根目录中的 pom.xml:

http://svn.apache.org/repos/asf/james/server/tags/james-server-3.0-beta4/pom.xml

这是子模块container-spring的pom.xml:

http://svn.apache.org/repos/asf/james/server/tags/james-server-3.0-beta4/container-spring/pom.xml

4

2 回答 2

3

为了回答您的问题,我使用以下 pom.xml 配置了一个简单的 maven 项目:

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.stackoverflow.tuxi</groupId>
  <artifactId>duplicatedjar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.james</groupId>
      <artifactId>james-server</artifactId>
      <version>3.0-beta4</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>org.apache.james</groupId>
      <artifactId>james-server-container-spring</artifactId>
      <version>3.0-beta4</version>
    </dependency>
  </dependencies>
</project>

然后执行maven依赖树命令分析依赖关系:

% mvn dependency:tree                                                                                                                                                                                              
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building duplicatedjar 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ duplicatedjar ---
[INFO] com.stackoverflow.tuxi:duplicatedjar:jar:1.0-SNAPSHOT
[INFO] +- org.apache.james:james-server:pom:3.0-beta4:compile
[INFO] \- org.apache.james:james-server-container-spring:jar:3.0-beta4:compile
[INFO]    +- org.apache.james:james-server-lifecycle-spring:jar:3.0-beta4:compile
[INFO]    |  +- org.apache.geronimo.specs:geronimo-annotation_1.1_spec:jar:1.0.1:compile
[INFO]    |  \- org.apache.geronimo.specs:geronimo-jpa_2.0_spec:jar:1.0:compile
[INFO]    +- org.apache.james:james-server-lifecycle-api:jar:3.0-beta4:compile
[INFO]    +- org.apache.james:james-server-filesystem-api:jar:3.0-beta4:compile
[INFO]    |  +- org.apache.james:apache-jsieve-manager-api:jar:0.5:compile
[INFO]    |  \- commons-io:commons-io:jar:2.0.1:compile
[INFO]    +- org.apache.james:james-server-mailetcontainer-api:jar:3.0-beta4:compile
[INFO]    +- org.apache.james:james-server-protocols-library:jar:3.0-beta4:compile
[INFO]    |  +- org.apache.james:james-server-util:jar:3.0-beta4:compile
[INFO]    |  +- org.apache.james.protocols:protocols-netty:jar:1.6.2:compile
[INFO]    |  \- io.netty:netty:jar:3.3.1.Final:compile
[INFO]    +- org.apache.james:james-server-data-api:jar:3.0-beta4:compile
[INFO]    +- org.apache.james:james-server-core:jar:3.0-beta4:compile
[INFO]    |  \- org.apache.james:apache-mailet-base:jar:1.1:compile
[INFO]    +- org.apache.james:apache-james-mailbox-api:jar:0.4:compile
[INFO]    +- org.apache.james:apache-james-mailbox-tool:jar:0.4:compile
[INFO]    |  +- org.apache.james:apache-james-mailbox-store:jar:0.4:compile
[INFO]    |  |  +- org.apache.james:apache-mime4j-core:jar:0.7.2:compile
[INFO]    |  |  \- org.apache.james:apache-mime4j-dom:jar:0.7.2:compile
[INFO]    |  +- org.apache.james:apache-james-mailbox-memory:jar:0.4:compile
[INFO]    |  +- org.apache.geronimo.specs:geronimo-annotation_1.0_spec:jar:1.1.1:compile
[INFO]    |  \- org.apache.openjpa:openjpa:jar:2.1.0:compile
[INFO]    |     +- net.sourceforge.serp:serp:jar:1.13.1:compile
[INFO]    |     |  \- junit:junit:jar:3.8.1:compile
[INFO]    |     +- org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:compile
[INFO]    |     +- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile
[INFO]    |     \- commons-pool:commons-pool:jar:1.5.4:compile
[INFO]    +- org.apache.james.protocols:protocols-api:jar:1.6.2:compile
[INFO]    +- org.apache.james:apache-mailet:jar:2.4:compile
[INFO]    +- org.springframework:spring-core:jar:3.1.0.RELEASE:compile
[INFO]    |  +- org.springframework:spring-asm:jar:3.1.0.RELEASE:compile
[INFO]    |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO]    +- org.springframework:spring-beans:jar:3.1.0.RELEASE:compile
[INFO]    +- org.springframework:spring-context:jar:3.1.0.RELEASE:compile
[INFO]    |  +- org.springframework:spring-aop:jar:3.1.0.RELEASE:compile
[INFO]    |  \- org.springframework:spring-expression:jar:3.1.0.RELEASE:compile
[INFO]    +- org.springframework:spring-web:jar:3.1.0.RELEASE:compile
[INFO]    |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO]    +- org.springframework.osgi:spring-osgi-extender:jar:1.2.1:compile
[INFO]    |  +- org.springframework.osgi:spring-osgi-core:jar:1.2.1:compile
[INFO]    |  |  +- org.springframework:org.springframework.aop:jar:2.5.6.SEC01:compile
[INFO]    |  |  |  \- org.aopalliance:com.springsource.org.aopalliance:jar:1.0.0:compile
[INFO]    |  |  +- org.springframework:org.springframework.beans:jar:2.5.6.SEC01:compile
[INFO]    |  |  +- org.springframework:org.springframework.context:jar:2.5.6.SEC01:compile
[INFO]    |  |  \- org.springframework:org.springframework.core:jar:2.5.6.SEC01:compile
[INFO]    |  \- org.springframework.osgi:spring-osgi-io:jar:1.2.1:compile
[INFO]    +- org.osgi:org.osgi.core:jar:4.2.0:compile
[INFO]    +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO]    |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO]    |  \- commons-lang:commons-lang:jar:2.4:compile
[INFO]    +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO]    +- javax.mail:mail:jar:1.4.4:compile
[INFO]    \- log4j:log4j:jar:1.2.16:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.140s
[INFO] Finished at: Sun Sep 30 12:30:30 BRT 2012
[INFO] Final Memory: 12M/169M
[INFO] ------------------------------------------------------------------------

我不知道您项目中的哪些 jar 与上面的 jar 冲突,但我注意到 spring 依赖项取决于apache commons 套件(commons-xxx deps)。很多项目都使用它们(IMO 的很多东西都应该包含在 JDK 本身中,但那是另一个线程)但是一些公共库有一些问题:

  • 向后兼容性问题
  • groupid 和 artifactid 多年来一直在变化,因此您最终可以得到具有不同名称的相同 jar(commons-xxx 和 org.apache.commons.xxx)

实际上,与 apache commons 套件有冲突的依赖关系是很正常的,所以我在这里猜测这是你的实际问题。

通常,解决方案是直接在依赖项中声明项目中需要的 apache 公共库,并在依赖项部分中排除冲突的库,就像@Vikdor 说的那样

您最终会得到类似这样的结果(重要的是只删除冲突的,而不是像下面的示例中那样全部删除,否则您的软件将无法正常工作):

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.stackoverflow.tuxi</groupId>
  <artifactId>duplicatedjar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.james</groupId>
      <artifactId>james-server</artifactId>
      <version>3.0-beta4</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>org.apache.james</groupId>
      <artifactId>james-server-container-spring</artifactId>
      <version>3.0-beta4</version>
      <!-- DO NOT exclude all of them, only the conflicting ones -->
      <exclusions>
        <exclusion>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
        </exclusion>
        <exclusion>
          <groupId>commons-pool</groupId>
          <artifactId>commons-pool</artifactId>
        </exclusion>
        <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
        </exclusion>
        <exclusion>
          <groupId>commons-collections</groupId>
          <artifactId>commons-collections</artifactId>
        </exclusion>
        <exclusion>
          <groupId>commons-lang</groupId>
          <artifactId>commons-lang</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>
于 2012-09-30T15:49:28.873 回答
3

您应该在主 jar 的依赖项声明中指定与要排除的子项目对应的 jar。

有关如何定义依赖项排除的更多详细信息,请参阅maven 文档。

于 2012-09-23T18:36:34.097 回答