Skip to content

Commit

Permalink
Improve support of custom ivy home configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Vyshniak <[email protected]>
  • Loading branch information
molekyla committed Jul 8, 2024
1 parent de8561e commit d7890e1
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions plugin/src/main/scala/com/here/bom/internal/IvyPomLocator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

/**
Expand Down

0 comments on commit d7890e1

Please sign in to comment.