Search code examples
javamavensolrj

maven dependency:tree log: version1 compile (version managed from version2)


  1. I faced a problem with dependency:tree. It troubled me a few days. I defined solr-solrj with version 6.6.0 in my pom.xml in one of my modules named A as below: <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>6.6.0</version> </dependency>

  2. Another module named B dependencies on module A. when I run mvn dependency:tree -Dverbose -Dincludes=org.apache.solr:solr-solrj, the console appears org.apache.solr:solr-solrj:jar:5.5.3:compile (version managed from 6.6.0). I find all the pom.xml, I haven't defined the 5.5.3 version. And I haven't found any jar dependencies on 5.5.3 version transitively in my project.

  3. What's the meaning of 5.5.3:compile (version managed from 6.6.0 ? And why 5.5.3 version appeared in my project?

  4. 1

2

pic1 is that I use idea to show dependencies. pic2 is I clicked solrj jump to pom.xml.


Solution

  • Either the pom for module B or the parent pom for module B includes a <dependencyManagement> section. Dependency management is used in maven to centralize the versions used for jars. It is likely overriding the version of solr-solrj you defined to be version 5.5.3. Dependency management functions in two main ways. You are probably experiencing what is described in section 2. Please see the conclusion for a summary specific to your question.

    1. It is a central place for defining versions of dependencies.

    So when an artifact version is defined in the <dependencyManagement> section you can declare the dependency in your pom without defining the version, and the version defined in <dependencyManagement> will be used automatically.

    Example 1

    Let's say your parent pom defined the following

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.mememe</groupId>
          <artifactId>mylib</artifactId>
          <version>1.1.12</version>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    Then if you define the following in your pom

    <dependencies>
      <dependency>
        <groupId>com.mememe</groupId>
        <artifactId>mylib</artifactId>
      </dependency>
    </dependencies>
    

    Your project will automatically use version 1.1.12 as defined in the <dependencyManagement> section of your parent pom.

    2. Overriding the versions of transitive dependencies.

    If you have an artifact version defined in your <dependencyManagement> and one of your dependencies has a transitive dependency on the same artifact, the version of the artifact defined in your <dependencyManagement> section is used automatically.

    Example 2

    Let's say this artifact

    <dependency>
      <groupId>com.youyou</groupId>
      <artifactId>yourlib</artifactId>
      <version>3.0.0</version>
    </dependency>
    

    Has this transitive dependency

    <dependency>
      <groupId>com.morestuff</groupId>
      <artifactId>morelib</artifactId>
      <version>2.0.0</version>
    </dependency>
    

    Now let's say our parent pom defines this <dependencyManagement> section

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.morestuff</groupId>
          <artifactId>morelib</artifactId>
          <version>2.5.2</version>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    Then if you define this dependency in your pom

    <dependency>
      <groupId>com.youyou</groupId>
      <artifactId>yourlib</artifactId>
      <version>3.0.0</version>
    </dependency>
    

    Maven will override yourlib's dependency on morelib version 2.0.0 with morelib version 2.5.2.

    Conclusion

    Specific to your question, someone has defined a <dependencyManagement> section like the following in the pom or parent pom of module B:

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.apache.solr</groupId>
          <artifactId>solr-solrj</artifactId>
          <version>5.5.3</version>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    Since solr-solrj is a dependency of module A and mondule A is a dependency of module B. Dependency management is overriding the version of solr-solrj to be 5.5.3 . You should speak with the person who added this dependencyManagement configuration in your project, there is likely a good reason why they only wanted version 5.5.3 to be used.

    Docs

    See more details in the official maven docs here.