From 1f314605980a385d5ee136fd6daf4f47ff9bf11b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Jourdan-Weil?= Date: Sun, 28 Jan 2024 18:27:03 +0100 Subject: [PATCH 1/4] feat: add a way to use BOM in dependencyOverrides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See the context of this feature at https://github.com/heremaps/here-sbt-bom/issues/9 Signed-off-by: Gaël Jourdan-Weil --- plugin/src/main/scala/com/here/bom/Bom.scala | 9 ++++++++ .../com/here/bom/internal/BomReader.scala | 7 ++++++ .../com/here/bom/internal/BomReaderSpec.scala | 23 +++++++++++++------ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/plugin/src/main/scala/com/here/bom/Bom.scala b/plugin/src/main/scala/com/here/bom/Bom.scala index 26ded1e..ebf1550 100644 --- a/plugin/src/main/scala/com/here/bom/Bom.scala +++ b/plugin/src/main/scala/com/here/bom/Bom.scala @@ -55,6 +55,10 @@ object Bom { } } + def dependencies(bomArtifact: ModuleID): Def.Setting[Seq[ModuleID]] = { + read(bomArtifact)(bom => bom.bomDependencies) + } + implicit def addBomSyntax(dep: OrganizationArtifactName): BomSyntax = new BomSyntax(dep) class BomSyntax(dep: OrganizationArtifactName) { @@ -85,4 +89,9 @@ object Bom { trait Bom { def version(dependency: OrganizationArtifactName): String + + /** + * List of all dependencies declared in the BOM + */ + def bomDependencies: Seq[ModuleID] } diff --git a/plugin/src/main/scala/com/here/bom/internal/BomReader.scala b/plugin/src/main/scala/com/here/bom/internal/BomReader.scala index 25c354c..230cb01 100644 --- a/plugin/src/main/scala/com/here/bom/internal/BomReader.scala +++ b/plugin/src/main/scala/com/here/bom/internal/BomReader.scala @@ -155,6 +155,13 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S sys.error(s"Version for ${normalized.group}.${normalized.name} not found in BOM") ) } + + override def bomDependencies: Seq[ModuleID] = { + effectiveVersions.map { case ((group, name), version) => + ModuleID(group, name, version) + }.toSeq + } + } } diff --git a/plugin/src/test/scala/com/here/bom/internal/BomReaderSpec.scala b/plugin/src/test/scala/com/here/bom/internal/BomReaderSpec.scala index 790bda3..b0652ae 100644 --- a/plugin/src/test/scala/com/here/bom/internal/BomReaderSpec.scala +++ b/plugin/src/test/scala/com/here/bom/internal/BomReaderSpec.scala @@ -44,6 +44,8 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory { val dependency = "com.test" %% "test_module" % bom dependency.revision should be("1.0") + + bom.bomDependencies shouldBe Seq("com.test" % "test_module_2.12" % "1.0") } "BomReader" should "assemble the BOM correctly with parent" in { @@ -55,13 +57,15 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory { (pomLocatorMock.getPomFile _) .expects(where((moduleId: NormalizedArtifact) => { - moduleId.group.equals("com.test") && moduleId.name.equals("test_bom_parent") && moduleId.version.equals("1.0") + moduleId.group.equals("com.test") && moduleId.name + .equals("test_bom_parent") && moduleId.version.equals("1.0") })) .returns(Some(createMockPomFile("com.test", "test_bom_parent", "1.0", false))) (pomLocatorMock.getPomFile _) .expects(where((moduleId: NormalizedArtifact) => { - moduleId.group.equals("com.test") && moduleId.name.equals("test_bom") && moduleId.version.equals("1.0") + moduleId.group.equals("com.test") && moduleId.name.equals("test_bom") && moduleId.version + .equals("1.0") })) .returns(Some(createMockPomFile("com.test", "test_bom", "1.0", true))) @@ -70,9 +74,16 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory { val dependency = "com.test" %% "test_module" % bom dependency.revision should be("1.0") + + bom.bomDependencies shouldBe Seq("com.test" % "test_module_2.12" % "1.0") } - private def createMockPomFile(groupId: String, artifactId: String, version: String, addParent: Boolean): File = { + private def createMockPomFile( + groupId: String, + artifactId: String, + version: String, + addParent: Boolean + ): File = { val tempFile = File.createTempFile("mock", ".xml") val pomContent = @@ -81,8 +92,7 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory { | $groupId | $artifactId | $version - | ${ - if (addParent) + | ${if (addParent) s""" | | $groupId @@ -101,8 +111,7 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory { | | | | | - |""".stripMargin - } + |""".stripMargin} | | """.stripMargin From 59e5b6858e639dc8325d1c1d18fefd0132055979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Jourdan-Weil?= Date: Sun, 4 Feb 2024 15:47:17 +0100 Subject: [PATCH 2/4] docs(readme): new usage for dependencyOverrides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël Jourdan-Weil --- README.md | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c21cf6..b29ca2c 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,10 @@ This plugin is compatible with: ## How to use it The following text provides a step-by-step guide on how to import [Jackson BOM](https://github.com/FasterXML/jackson-bom) into an sbt project. +### Load the plugin + Enable the `MAVEN_CENTRAL` repository with a special `pattern` configuration: -``` +```scala resolvers += Resolver.url( "MAVEN_CENTRAL", url("https://repo.maven.apache.org/maven2"))( @@ -30,12 +32,39 @@ resolvers += Resolver.url( This will enable the project to access plugin files stored in Maven Central, https://repo.maven.apache.org/maven2/com/here/platform/sbt-bom_2.12_1.0/. Add the `sbt-bom` plugin to `plugins.sbt` file. -``` +```scala addSbtPlugin("com.here.platform" % "sbt-bom" % "1.0.1") ``` +### Read & use a BOM + +#### In a `dependencyOverrides` context + +If you want to get all the dependencies declared in the BOM and use their version to override yours, follow this step. +If you're looking for more control, go to the [general context](#in-a-general-context) section below. + Add BOM dependency to the `build.sbt`: +```scala +import com.here.bom.Bom +lazy val jacksonDependencies = Bom.dependencies("com.fasterxml.jackson" % "jackson-bom" % "2.14.2") ``` + +This creates an sbt `Setting` that resolves the list of `ModuleID` declared in a desired BOM. + +Enable this setting in the module configuration and use this setting in `dependencyOverrides`: +```scala +lazy val `demo` = project + .in(file(".")) + .settings(jacksonDependencies) + .settings( + dependencyOverrides ++= jacksonDependencies.key.value + ) +``` + +#### In a general context + +Add BOM dependency to the `build.sbt`: +```scala import com.here.bom.Bom lazy val jacksonBom = Bom.read("com.fasterxml.jackson" % "jackson-bom" % "2.14.2")(bom => Dependencies(bom)) ``` @@ -44,7 +73,7 @@ This creates an sbt `Setting` that resolves a desired BOM, creates a `Bom` objec Under the `project` directory, create a `Dependencies` class that takes a `Bom` as a constructor parameter. Inside this class, we can refer to the versions from the BOM. The class should expose a list of dependencies that used as `libraryDependencies` -``` +```scala import sbt._ import com.here.bom.Bom @@ -52,11 +81,14 @@ case class Dependencies(bom: Bom) { val dependencies: Seq[ModuleID] = Seq( "com.fasterxml.jackson.core" % "jackson-databind" % bom ) + + // Note: you may also use bom.bomDependencies which gives you the list of all dependencies declared in the BOM. + // This gives the same result as Bom.dependencies (see above). } ``` Enable this setting in the module configuration and use this setting in `libraryDependencies`: -``` +```scala lazy val `demo` = project .in(file(".")) .settings(jacksonBom) From a7be3ce0fb7f0035ebc8e1991869e0ffe6b41f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Jourdan-Weil?= Date: Sun, 4 Feb 2024 15:49:10 +0100 Subject: [PATCH 3/4] docs(readme): add myself (Gael) as a contributor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël Jourdan-Weil --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b29ca2c..9ff2351 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ The credentials can be supplied through these methods, with the following preced - Andrii Banias (https://github.com/abanias) - Anton Wilhelm (https://github.com/devtonhere) - Dmitry Abramov (https://github.com/dmitriy-abramov) +- Gaël Jourdan-Weil (https://github.com/gaeljw) ## License Copyright (C) 2019-2024 HERE Europe B.V. From e2c860b7cc90c1c7a84e58c60f16541d876d2900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Jourdan-Weil?= Date: Sun, 4 Feb 2024 15:52:00 +0100 Subject: [PATCH 4/4] docs(readme): update version referenced in README and only add the resolver if needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël Jourdan-Weil --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9ff2351..810e501 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,12 @@ The following text provides a step-by-step guide on how to import [Jackson BOM]( ### Load the plugin -Enable the `MAVEN_CENTRAL` repository with a special `pattern` configuration: +Add the `sbt-bom` plugin to `plugins.sbt` file. +```scala +addSbtPlugin("com.here.platform" % "sbt-bom" % "1.0.7") +``` + +If you're encountering issues or using earlier versions, you might have to explicitly declare the Maven Central repository: ```scala resolvers += Resolver.url( "MAVEN_CENTRAL", @@ -31,11 +36,6 @@ resolvers += Resolver.url( ``` This will enable the project to access plugin files stored in Maven Central, https://repo.maven.apache.org/maven2/com/here/platform/sbt-bom_2.12_1.0/. -Add the `sbt-bom` plugin to `plugins.sbt` file. -```scala -addSbtPlugin("com.here.platform" % "sbt-bom" % "1.0.1") -``` - ### Read & use a BOM #### In a `dependencyOverrides` context