Search code examples
mavensbt

Sbt finds different version for `show externalDependencyClasspath` and `dependencyTree`


We are using a non-standard version number system for our software, which is causing problems with sbt finding the wrong version.

Jars in a branch with name feature/x-y-z will be published with the version x-y-SNAPSHOT to a Maven repo. These might be published by mvn or sbt.

Released jars include the version of a data definition jar, since code compiled against different versions of the base data classes frequently fails in confusing ways, so the released jar will typically have a version like 1.2.3-1, where this is the first release using the 1.2.3 data classes.

My sbt has (in part)

val LibVersion = "x-y-SNAPSHOT"
lazy val root = project.in(file(".")).settings(
  libraryDependences ++= Seq(
    "com.example" % "some-lib" % LibVersion,
    "com.example" %% "some-scala-lib" % "4.3.0"
)

If I run show externalDependencyClasspath, it lists some-lib-x-y-<datetime info>.jar (which is correct). If I run dependencyTree, it lists something like some-lib-1.2.3-6 (which is wrong).

The code is eventually built into a Docker image (I think via sbt stage and some other tooling) with the wrong (1.2.3-6) version of some-lib.

I have also included a

  dependencyOverrides ++= Seq(
    "com.example" % "some-lib" % LibVersion) // with or without force() here

with no apparent effect.

Is there some way to get sbt to use the version I am telling it to use?

I am running sbt 1.9.1 and these plugins from project/plugins.sbt:

addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.16")
addSbtPlugin("org.scalameta"  % "sbt-scalafmt"        % "2.4.6")
addSbtPlugin("org.scoverage"  % "sbt-scoverage"       % "2.0.5")

Solution

  • The problem appears to be in the way that Maven publishes the library. The pom is, in part,

    <project>
        <groupId>com.example</groupId>
        <artifactId>some-lib</artifactId>
        <packaging>jar</packaging>
        <version>${revision}</version>
        <properties>
           <revision>1.2.3-6</revision>
        </properties>
        <!-- Other build stuff -->
    </project>
    

    The jar is published with mvn deploy -Drevision=x-y-SNAPSHOT <other args>.

    The *.pom file that is published in this case has the wrong version number in it: 1.2.3-6 rather than x-y-SNAPSHOT. I am told this is a known bug in maven, although I don't have a reference for that.

    sbt then trusts the published pom and thinks it has the wrong version.

    The workaround is to have the CI job for the library edit the pom and put the correct version in the <revision> line, rather than passing it as a variable value.