sbt publish (or publishLocal) VS sbt assembly for distribution purposes and dependency conflicts resolution

北慕城南 提交于 2019-12-24 18:29:14

问题


Bottom line is that I want to distribute a library that can be integrated using SBT or Maven and whose dependencies won't conflict with the integrating project's dependencies or transitive dependencies.

Currently I am distributing my library through SBT using the publish command which is configured to publish the artifacts to my private JFrog Artifactory.

It is working as expected in the sense that it will publish the library to artifactory and that I can easily integrate the resulting library to different projects successfully with SBT and also with Maven.

Unfortunately, my published library has a 3rd party dependency. It is using the cassandra datastax library version 3.3.0.

I came across a nasty situation where one of the consumers of my library is also dependent on a different 3rd party library (out of my control) which is itself dependent on the same cassandra driver (flink-connector-cassandra whose POM can be found here https://github.com/apache/flink/blob/master/flink-connectors/flink-connector-cassandra/pom.xml), and whose casandra-driver version is 3.0.0.

I've tried various combinations of exclusions, as well as changing the cassandra version of my library to match the 3rd party version (3.3.0 ~> changed it to 3.0.0 as in the flink-connector-cassandra) in the consuming SBT file.

Unfortunately the consumer is crashing at run time due to unfound classes and methods inside the cassandra-driver library.

I thought that perhaps I could distribute my library as a "Fat" jar, inlining the cassandra dependency (as well as its own transitive 3rd party dependencies) and "squash" the namespaces to nest under the my libraries namespace in order to avoid conflicts (i.e com.cassandra.driver could nest under com.myproject as com.myproject.cassandra.driver or similar effect) hopefully which would resolve the runtime conflicts with the flink cassandra driver's.

Is there a practical solution to avoiding these conflicts using assembly or publish commands?

My libraries' SBT looks like this (The library I am publishing):

    lazy val commonSettings = Seq(
      scalaVersion := "2.11.8",
      sources in doc in Compile := List(),
      organization := "com.myLibrary.library",
      name := "commons",
      version := "0.4.1.1",
      publishTo := Some("Artifactory Realm" at "https://a.b.c.jfrog.io/myLibrary"),
      javacOptions ++= Seq("-source", "1.7", "-target", "1.7", "-Xlint")
    )

    lazy val myLibrary: Project = project.in(file("myLibrary"))
      .settings(
        commonSettings,
        name := "myLibrary",
        assemblyJarName in assembly := "myLibrary.jar",
        libraryDependencies ++= Seq(
          "com.datastax.cassandra" % "cassandra-driver-core" % "3.3.0" // << I've also tried to change this to match the flink's version of 3.0.0
// I've also tried to add
// classifier "shaded"
        )
      )

The consuming SBT file looks like this:

val dependencies: Seq[ModuleID] = commonDependencies ++ flinkDeps ++ Seq(
    "org.apache.flink" %% "flink-connector-cassandra" % "1.4.1" ,
    "com.mylibrary" %% "library" % "0.4.1"
  )

I've tried to modify the consuming SBT to exclude the cassandra driver but it didn't work:

val dependencies: Seq[ModuleID] = commonDependencies ++ flinkDeps ++ Seq(
        "org.apache.flink" %% "flink-connector-cassandra" % "1.4.1" excludeAll ExclusionRule(organization = "com.datastax"),
        "com.mylibrary" %% "library" % "0.4.1"
      )

来源:https://stackoverflow.com/questions/51380302/sbt-publish-or-publishlocal-vs-sbt-assembly-for-distribution-purposes-and-depe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!