diff --git a/plugin/src/main/scala/com/here/bom/internal/IvyPomLocator.scala b/plugin/src/main/scala/com/here/bom/internal/IvyPomLocator.scala index 08f011a..489eb5c 100644 --- a/plugin/src/main/scala/com/here/bom/internal/IvyPomLocator.scala +++ b/plugin/src/main/scala/com/here/bom/internal/IvyPomLocator.scala @@ -27,7 +27,8 @@ import scala.collection.JavaConverters._ object IvyPomLocator { /* - Ivy internals require ivy.home to be absolute. + Ivy internals require ivy.home and sbt.ivy.home to be equals and have absolute path. + Related sbt issue: https://github.com/sbt/sbt/issues/1894 Example of error if this doesn't hold: java.lang.IllegalArgumentException: ivy.home must be absolute: .sbt-cache/ivy2 @@ -36,16 +37,51 @@ object IvyPomLocator { at org.apache.ivy.core.settings.IvySettings.getDefaultCache(IvySettings.java:824) at org.apache.ivy.core.settings.IvySettings.getDefaultResolutionCacheBasedir(IvySettings.java:864) at sbt.internal.librarymanagement.IvySbt$.$anonfun$configureResolutionCache$1(Ivy.scala:582) + */ def tweakIvyHome(logger: Logger): Unit = { - val ivyHome = System.getProperty("ivy.home") - if (ivyHome != null) { - val absHome = new File(ivyHome).getAbsolutePath - if (System.setProperty("ivy.home", absHome) != absHome) { - logger.warn(s"Adjusting ivy.home: $ivyHome -> $absHome") + var sbtIvyHome = System.getProperty("sbt.ivy.home") + var ivyHome = System.getProperty("ivy.home") + if (sbtIvyHome == null && ivyHome == null) + return + + if (sbtIvyHome != ivyHome) { + if (sbtIvyHome != null && ivyHome != null) { + logger.error( + s"System properties ivy.home and sbt.ivy.home have different values: $ivyHome and $sbtIvyHome. Consider use the same value" + ) + throw new RuntimeException( + "System properties ivy.home and sbt.ivy.home must have same value" + ) + } else if (sbtIvyHome != null) { + sbtIvyHome = convertToAbsolutePath(sbtIvyHome) + ivyHome = sbtIvyHome + } else if (ivyHome != null) { + ivyHome = convertToAbsolutePath(ivyHome) + sbtIvyHome = ivyHome } + } else { + sbtIvyHome = convertToAbsolutePath(sbtIvyHome) + ivyHome = sbtIvyHome + } + adjustSystemProperty(logger, "sbt.ivy.home", sbtIvyHome) + adjustSystemProperty(logger, "ivy.home", ivyHome) + } + + private def adjustSystemProperty( + logger: Logger, + systemPropertyName: String, + value: String + ): Unit = { + val oldValue = System.setProperty(systemPropertyName, value) + if (oldValue != value) { + logger.warn(s"Adjusting $systemPropertyName: $oldValue -> $value") } } + + private def convertToAbsolutePath(path: String): String = { + new File(path).getAbsolutePath + } } /** diff --git a/plugin/src/sbt-test/psv/custom_ivy_home_config/build.sbt b/plugin/src/sbt-test/psv/custom_ivy_home_config/build.sbt new file mode 100644 index 0000000..ed202a0 --- /dev/null +++ b/plugin/src/sbt-test/psv/custom_ivy_home_config/build.sbt @@ -0,0 +1,15 @@ +import Dependencies._ +import com.here.bom.Bom + +lazy val deps = Bom.read("com.fasterxml.jackson" % "jackson-bom" % "2.14.2")(bom => Dependencies(bom)) + +lazy val `demo` = project + .in(file(".")) + .settings(scalaVersion := "2.12.15") + .enablePlugins(SbtPlugin) + .settings(deps) + .settings( + name := "custom-ivy-home-test", + libraryDependencies ++= deps.key.value.dependencies, + resolvers := Resolver.DefaultMavenRepository +: resolvers.value + ) diff --git a/plugin/src/sbt-test/psv/custom_ivy_home_config/project/Dependencies.scala b/plugin/src/sbt-test/psv/custom_ivy_home_config/project/Dependencies.scala new file mode 100644 index 0000000..eb45e13 --- /dev/null +++ b/plugin/src/sbt-test/psv/custom_ivy_home_config/project/Dependencies.scala @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2019-2024 HERE Europe B.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * License-Filename: LICENSE + */ +import sbt._ +import com.here.bom.Bom + +case class Dependencies(platformBom: Bom) { + val dependencies: Seq[ModuleID] = Seq( + "com.fasterxml.jackson.core" % "jackson-databind" % platformBom + ) +} \ No newline at end of file diff --git a/plugin/src/sbt-test/psv/custom_ivy_home_config/project/build.properties b/plugin/src/sbt-test/psv/custom_ivy_home_config/project/build.properties new file mode 100644 index 0000000..3161d21 --- /dev/null +++ b/plugin/src/sbt-test/psv/custom_ivy_home_config/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.6.1 diff --git a/plugin/src/sbt-test/psv/custom_ivy_home_config/project/plugins.sbt b/plugin/src/sbt-test/psv/custom_ivy_home_config/project/plugins.sbt new file mode 100644 index 0000000..9f738c3 --- /dev/null +++ b/plugin/src/sbt-test/psv/custom_ivy_home_config/project/plugins.sbt @@ -0,0 +1,7 @@ +addSbtPlugin("com.here.platform.artifact" % "sbt-resolver" % "2.0.24") + +sys.props.get("plugin.version") match { + case Some(x) => addSbtPlugin("com.here.platform" % "sbt-bom" % x) + case _ => sys.error("""|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) +} \ No newline at end of file diff --git a/plugin/src/sbt-test/psv/custom_ivy_home_config/src/main/scala/Demo.scala b/plugin/src/sbt-test/psv/custom_ivy_home_config/src/main/scala/Demo.scala new file mode 100644 index 0000000..6a16e7b --- /dev/null +++ b/plugin/src/sbt-test/psv/custom_ivy_home_config/src/main/scala/Demo.scala @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2019-2024 HERE Europe B.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * License-Filename: LICENSE + */ +package com.here.bom + +import com.fasterxml.jackson.databind.ObjectMapper + +object Demo extends App { + val mapper = new ObjectMapper() + println("ObjectMapper instance: " + mapper) +} \ No newline at end of file diff --git a/plugin/src/sbt-test/psv/custom_ivy_home_config/test b/plugin/src/sbt-test/psv/custom_ivy_home_config/test new file mode 100644 index 0000000..94d4f24 --- /dev/null +++ b/plugin/src/sbt-test/psv/custom_ivy_home_config/test @@ -0,0 +1,10 @@ +> set scriptedBufferLog := false +> eval System.setProperty("ivy.home", "/tmp/custom-ivy-home") +> eval System.setProperty("sbt.ivy.home", "/tmp/custom-sbt-ivy-home") +# check that build failed for the different custom home locations +-> reload +> eval System.setProperty("sbt.ivy.home", "/tmp/custom-ivy-home") +> eval System.setProperty("ivy.home", "/tmp/custom-ivy-home") +# check that build succeeded for the same custom home locations +> reload +> package \ No newline at end of file