Maven Spring Boot:在类路径上发现多次出现 org.json.JSONObject:

新手上路,请多包涵

当我运行 mvn test 我收到这个警告。我该如何解决?

 Found multiple occurrences of org.json.JSONObject on the class path:

        jar:file:/C:/Users/Chloe/.m2/repository/org/json/json/20140107/json-20140107.jar!/org/json/JSONObject.class
        jar:file:/C:/Users/Chloe/.m2/repository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar!/org/json/JSONObject.class

You may wish to exclude one of them to ensure predictable runtime behavior

这是我的 pom.xml 。对 JSON 的唯一引用是

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
    </dependency>

阿帕奇行家 3.5.3

原文由 Chloe 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.1k
2 个回答

添加在

 <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>

以下排除:

  <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </exclusion>
    </exclusions>

同样,对于 Gradle 项目:

 testCompile("org.springframework.boot:spring-boot-starter-test") {
    exclude group: "com.vaadin.external.google", module:"android-json"
}

原文由 user7294900 发布,翻译遵循 CC BY-SA 4.0 许可协议

背景org.json 效果很好,但有一些人不喜欢的许可条款(“该软件应用于善良,而不是邪恶。”)。所以 Vaadin 想使用这个图书馆,但不能确定他们有一天会不会用它来作恶。相反,他们重新实现了接口,发布了 android-json 并将其用作 org.json 的替代品。其他人也开始使用 android-json ,这样他们也不会被不使用他们的软件作恶的要求所束缚。

这是一个很好的解决方案,除了当两个库在类路径上时,它们会发生冲突。

解决方案: 如果你从冲突的传递依赖中得到这个错误,那么你最好的办法是排除 Vaadin 的 android-json 库(由 Spring 引入),或者排除 org.json 库(由 Spring 引入)由另一个依赖)。 Vaadin 的版本旨在实现相同的实现,但存在细微差别。

如果您在代码中使用 org.json 并且它与 Spring 的 Vaadin 依赖项冲突,那么我建议您尝试 open-json 。这是 Vaadin 重新实现 org.json 的端口,但他们更改了包,因此您不会与 org.json:jsoncom.vaadin.external.google:android-json -b-cf1 发生任何冲突

https://github.com/openjson/openjson

添加 gradle 依赖:

     implementation('com.github.openjson:openjson:1.0.12')

或者在 Maven 中:

     <dependency>
        <groupId>com.github.openjson</groupId>
        <artifactId>openjson</artifactId>
        <version>1.0.12</version>
    </dependency>

然后更新 org.json 类正在使用的任何导入。

原文由 Planky 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题