diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom.kt index 3e1d046d..48f6965e 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom.kt @@ -1,7 +1,7 @@ package com.beust.kobalt.maven -import com.beust.kobalt.misc.log import com.beust.kobalt.misc.toString +import com.beust.kobalt.misc.warn import com.google.inject.assistedinject.Assisted import kotlinx.dom.childElements import org.w3c.dom.Element @@ -18,6 +18,33 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String, var artifactId: String? = null var packaging: String? = null var version: String? = null + + /** + * If the version is a string, extract it, look it up and evaluate it if we find it. Otherwise, error. + */ + private fun calculateVersion(s: String) : String { + val v = extractVar(s) + if (v != null) { + val value = properties[v] + if (value != null) { + return value + } else { + warn("Unknown variable for version: " + s) + return "" + } + } else { + return s + } + } + + private fun extractVar(s: String) : String? { + if (s.startsWith("\${") && s.endsWith("}")) { + return s.substring(2, s.length - 1) + } else { + return null + } + } + var name: String? = null var properties = sortedMapOf() var repositories = listOf() @@ -29,28 +56,9 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String, data class Dependency(val groupId: String, val artifactId: String, val packaging: String?, val version: String, val optional: Boolean = false, val scope: String? = null) { - /** When a variable is used in a maven file, e.g. ${version} */ - private val VAR = "$" + "{" - val mustDownload: Boolean get() = !optional && "provided" != scope && "test" != scope - val isValid: Boolean - get() { - var result = false - if (version.contains(VAR)) { - log(3, "Skipping variable version ${this}") - } else if (groupId.contains(VAR)) { - log(3, "Skipping variable groupId ${this}") - } else if (artifactId.contains(VAR)) { - log(3, "Skipping variable artifactId ${this}") - } else { - result = true - } - return result - } - - val id: String = "$groupId:$artifactId:$version" } @@ -81,7 +89,7 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String, var groupId: String? = null var artifactId: String? = null var packaging: String? = null - var version: String = "" + var readVersion: String = "" var optional: Boolean? = false var scope: String? = null for (j in 0..d.length - 1) { @@ -91,16 +99,17 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String, "groupId" -> groupId = e.textContent "artifactId" -> artifactId = e.textContent "type" -> packaging = e.textContent - "version" -> version = e.textContent + "version" -> readVersion = e.textContent "optional" -> optional = "true".equals(e.textContent, true) "scope" -> scope = e.textContent } } } + val version = calculateVersion(readVersion) val tmpDependency = Dependency(groupId!!, artifactId!!, packaging, version, optional!!, scope) dependencies.add(tmpDependency) } } - override fun toString() = toString("Pom", id, "id") + override fun toString() = toString("Pom", "id", id) } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt index 7ab3ffad..974e9dc3 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt @@ -120,8 +120,9 @@ class MavenDependency @Inject constructor( override fun directDependencies() : List { val result = arrayListOf() try { - pomFactory.create(id, pomFile.get()).dependencies.filter { - it.mustDownload && it.isValid + val pom = pomFactory.create(id, pomFile.get()) + pom.dependencies.filter { + it.mustDownload }.forEach { result.add(create(MavenId.toId(it.groupId, it.artifactId, it.packaging, it.version))) } diff --git a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt index a592fe0d..ab1f2055 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt @@ -22,7 +22,7 @@ class DownloadTest @Inject constructor( val depFactory: DepFactory, val localRepo: LocalRepo, val mdFactory: MavenDependency.IFactory, - val finderFactory: RepoFinderCallable.IFactory, + val dependencyManager: DependencyManager, val executors: KobaltExecutors) : KobaltTest() { private var executor: ExecutorService by Delegates.notNull() @@ -165,5 +165,13 @@ class DownloadTest @Inject constructor( // Assert.assertEquals(results.size, 1) // Assert.assertEquals(results[0].version, "1.1.0") } + + @Test + fun variablesShouldBeExpanded() { + val dep = dependencyManager.createMaven("org.mapdb:mapdb:3.0.0-M3") + val closure = dependencyManager.transitiveClosure(listOf(dep)) + val d = closure.filter { it.id.contains("eclipse-collections-api")} + Assert.assertEquals(d.size, 1) + } }