简体   繁体   English

Maven中的jar文件冲突

[英]jar file conflicts in maven

Right now I am using a maven project . 现在我正在使用一个Maven项目 And this project uses some logging.1.jar . 这个项目使用了logging.1.jar There is a new functionality which we are going to add to this project and is dependent on logging.3.jar . 我们将要添加到此项目的新功能依赖于logging.3.jar

The compilation is fine, but the application is failing at runtime due to jar file conflict. 编译很好,但是由于jar文件冲突,应用程序在运行时失败。

Reason is the newly added functionality, it is using logging.1.jar instead of logging.3.jar 原因是新添加的功能,它使用logging.1.jar而不是logging.3.jar

I cannot initialize a class which is present is newly added functionality as it dependent on logging.3.jar . 我无法初始化一个类,该类是新添加的功能,因为它依赖于logging.3.jar How can it be resolved? 如何解决?

Will it be resolved using URLClassLoader ? 是否可以使用URLClassLoader解决?

Add proper jar dependency into your pom.xml and run mvn clean-install -U command. 将适当的jar依赖项添加到pom.xml中,然后运行mvn clean-install -U命令。 this command clean your discrepancies and update correctly. 此命令清除您的差异并正确更新。

See part Dependency Exclusions on Maven introduction . 请参阅“ Maven简介中的 依赖项排除 ”部分。

So you should have to something like this in your pom: 因此,您应该在pom中执行以下操作:

<dependencies>
    ...
    <dependency>
      <groupId>org.examle</groupId>
      <artifactId>including-jar</artifactId>
      <version>1.0.0</version>
      <exclusions>
        <exclusion>  <!-- declare the exclusion here -->
          <groupId>org.logging</groupId>
          <artifactId>logging</artifactId> (or logging1)
        </exclusion>
      </exclusions> 
    </dependency>
    ...
  </dependencies>

The tricky part is that you must find the artifact(s) ( including-jar(s)) that are dependent on the old version and add this exclusion to all of those artifacts. 棘手的部分是,您必须找到依赖于旧版本的工件(包括jar),并将此排除项添加到所有这些工件中。

On the other hand it is also possible to prevent this older artifact to be actually loaded by setting its scope as provided, like: 另一方面,也可以通过设置所提供的范围来防止实际加载此较旧的工件,例如:

<dependencies>
    ...
    <dependency>
      <groupId>org.examle</groupId>
      <artifactId>logging</artifactId> // or logging1
      <version>1.0.0</version>
      <scope>provided</scope>
    </dependency>
    ...
  </dependencies>

However this latter solution might not suit all situations and I personally prefer the first because it does not obfuscate the real purpose (so excluding) and will not cause surprises later. 但是,后一种解决方案可能并不适合所有情况,我个人更喜欢第一种,因为它不会混淆实际目的(因此不包括在内),以后也不会引起意外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM