json-simple.jar 从 spring 启动 war 中丢失

json-simple.jar is missing from spring boot war

打包的 WAR 中缺少 json-simple jar,因为它在 spring-boot-starter-parent 中被标记为 optional,但我确实包含了一个依赖项 gelfjjson-simple 声明为依赖项..... 下面的示例(与 Maven 3.3.3 一起使用):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.giveandtake</groupId>
  <artifactId>main</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>

  <name>main</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
            <groupId>org.graylog2</groupId>
            <artifactId>gelfj</artifactId>
            <version>1.1.5</version>
        </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
    <version>1.2.3.RELEASE</version>
</dependency>
  </dependencies>
</project>

[1] 当用 Maven 3.0.4 打包 war 时,我得到 json-simple inside war(没有明确要求 jar)。

[2] 使用 Maven 3 打包时。3.x war 我的 war(除非我明确要求)文件中没有这个 jar。

这给我留下了这些问题:

问题 1:如果我有 ProjectX-->(Inherit)Spring-boot-Parent 并且还声明了一个对 [=39 有依赖的依赖 Y =]-simple,simple-json 的依赖不应该传递到 war 并认识到它不再是可选的吗?

问题2:为什么不同的maven版本结果不同[maven bug? , 搜索发行说明但没有找到任何匹配项]

如果您有 json-simple 的依赖项管理条​​目,其中 optional=true 在您的 parents 中,这可能会导致所描述的情况。 看看 eclipse 或 mvn help effective-pom 中的有效 pom 比搜索任何出现的 json-simple 它可能会有所帮助。

对于版本差异:您可以为此使用 effective-pom,也可以使用

if i have ProjectX-->(Inherit)Spring-boot-Parent and also declare a dependency Y that has dependency for json-simple, shouldnt the dependency for simple-json be transitive into war and recognize that as not optional anymore?

spring-boot-starter-parent pom 在其 dependencyManagement 部分(而不是在其 dependencies 部分中将 json-simple 依赖项声明为 optional

<dependencyManagement>
    <dependencies>
        ...
         <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
            <optional>true</optional>
         </dependency>
         ...

dependencyManagement 对声明的依赖关系和传递依赖关系有很强的治理能力,在这种情况下,json-simpleoptional 对于您的任何传递依赖关系(因此重新定义其中介)声明的依赖项,这就是为什么它不会被添加到打包的 war 中(即使 gelfj 依赖项将其作为传递依赖项)。

来自 official documentation:

A second, and very important use of the dependency management section is to control the versions of artifacts used in transitive dependencies [...] dependency management takes precedence over dependency mediation for transitive dependencies.


why is the different results with different maven versions [maven bug? , searched for release notes but didnt find anything matching]

Maven 3.0.4 有一个 default binding to Maven War Plugin version 2.1.1, while Maven 3.3.3 has a default binding 到 Maven War 插件版本 2.2,这是关于两个 Maven 版本和 War 插件.

但是,spring-boot-starter-parent pom 在其 pluginManagement 中将 WAR 插件声明为版本 2.5,因此会影响您最终将要使用的版本作为构建的一部分使用,并消除上述两个 Maven 版本之间的差异。

所以这是一个 Maven 核心错误而不是 WAR 插件错误并且 this bug 看起来几乎是修复(在 Maven 版本 3.1.0).

根据我的测试,我能够重现以下场景:

  • Maven 3.0.4,没有声明gelfj依赖,因此json-simple继承为可选,WAR插件没有打包:正确的行为
  • Maven 3.3.3,没有声明 gelfj 依赖项,再次 json-simple 作为可选,未打包:正确行为
  • Maven 3.0.4, gelfj 依赖声明, json-simple 应该仍然是可选的, WAR 插件确实打包了它: 不正确的行为
  • Maven 3.3.3, gelfj 依赖声明,再次 json-simple 仍然是可选的, WAR 插件没有打包它:正确的行为

因此我建议升级你的 maven 版本(推荐,避免 3.0.4)或者明确声明依赖不是可选的(通过声明它作为你的依赖,因为例如,或者在您的 dependencyManagement 部分中不是可选的)。