Skip to content

Commit

Permalink
Improve support of custom ivy home configuration (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Vyshniak <ext-oleksandr.vyshniak@here.com>
  • Loading branch information
molekyla committed Jul 8, 2024
1 parent de8561e commit 3ae99ce
Show file tree
Hide file tree
Showing 7 changed files with 127 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
15 changes: 15 additions & 0 deletions plugin/src/sbt-test/psv/custom_ivy_home_config/build.sbt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.1
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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 CustomIvyTestApp extends App {
val mapper = new ObjectMapper()
println("ObjectMapper instance: " + mapper)
}
10 changes: 10 additions & 0 deletions plugin/src/sbt-test/psv/custom_ivy_home_config/test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3ae99ce

Please sign in to comment.