From d7890e199cca6095c4d61b29b627798a4be89484 Mon Sep 17 00:00:00 2001 From: Oleksandr Vyshniak Date: Mon, 8 Jul 2024 13:20:15 +0300 Subject: [PATCH] Improve support of custom ivy home configuration Signed-off-by: Oleksandr Vyshniak --- .../com/here/bom/internal/IvyPomLocator.scala | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) 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 + } } /**