Robocide
Robocide

Reputation: 6751

json-simple.jar is missing from spring boot war

The json-simple jar is missing in packaged WAR because it is marked as optional in spring-boot-starter-parent, BUT I do include a dependency gelfj that declares json-simple as dependency..... example below (used with 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] when packaging war with maven 3.0.4 i get the json-simple inside war (without asking for the jar explicitly).

[2] when packaging with maven 3.3.x war i dont have this jar in my war(unless i explicitly ask for it) file.

which leaves me with these questions:

Question 1: 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?

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

Upvotes: 4

Views: 1542

Answers (2)

A_Di-Matteo
A_Di-Matteo

Reputation: 27852

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?

The spring-boot-starter-parent pom declares the json-simple dependency as optional in its dependencyManagement section (and not in its dependencies section):

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

dependencyManagement has a strong governance over declared dependencies and transitive dependencies, making in this case json-simple as optional for any transitive dependency (re-defining its mediation hence) of your declared dependencies, that's why it would not be added to the packaged war (even if the gelfj dependency has it as transitive dependency).

From 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 is has a default binding to Maven War Plugin version 2.1.1, while Maven 3.3.3 has a default binding to Maven War Plugin version 2.2, that's the main difference concerning the two Maven versions and the War plugin.

However, the spring-boot-starter-parent pom declares the WAR plugin as version 2.5 in its pluginManagement, hence impacting the version you will finally use as part of your build and removing the difference above between the two maven versions.

So this was a Maven Core bug rather than a WAR Plugin bug and this bug looks pretty much the fix (in the Maven version 3.1.0).

Upon my tests, I was able to reproduce the following scenarios:

  • Maven 3.0.4, no gelfj dependency declared, hence json-simple inherited as optional, WAR plugin did not package it: CORRECT BEHAVIOR
  • Maven 3.3.3, no gelfj dependency declared, again json-simple as optional, not packaged: CORRECT BEHAVIOR
  • Maven 3.0.4, gelfj dependency declared, json-simple should still be as optional, WAR Plugin did package it: INCORRECT BEHAVIOR
  • Maven 3.3.3, gelfj dependency declared, again json-simple still optional, WAR Plugin did not package it: CORRECT BEHAVIOR

I would hence suggest to rather upgrade your maven version (recommended, avoid 3.0.4) or explicitly declared the dependency as not optional (by declaring it as your dependency, for instance, or as not optional in your dependencyManagement section).

Upvotes: 3

If you have a dependency management entry for json-simple with optional=true in your parents, this might lead to the described situation. Have a look at the effective pom in eclipse or with mvn help effective-pom than search for any occurence of json-simple it might help.

For the version differences : You might use effective-pom for this, also

Upvotes: 2

Related Questions