From 6a489d0e7ef468b3b90ab86b4753a775f255d97e Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 22 Mar 2016 14:42:30 +0400 Subject: [PATCH] Switched to Pom2. --- .../kotlin/com/beust/kobalt/maven/MavenId.kt | 2 +- .../kotlin/com/beust/kobalt/maven/Pom2.kt | 83 +++++++++++++++---- .../beust/kobalt/maven/RepoFinderCallable.kt | 2 +- .../maven/dependency/MavenDependency.kt | 11 ++- 4 files changed, 77 insertions(+), 21 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt index 6888e819..3bd3276d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt @@ -79,7 +79,7 @@ class MavenId private constructor(val groupId: String, val artifactId: String, v fun toId(groupId: String, artifactId: String, packaging: String? = null, version: String?) = "$groupId:$artifactId:$version" + - (if (packaging != null) "@$packaging" else "") + (if (packaging != null && packaging != "") "@$packaging" else "") } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom2.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom2.kt index 0e5a7612..fffddb61 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom2.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Pom2.kt @@ -1,18 +1,34 @@ package com.beust.kobalt.maven import org.w3c.dom.Element +import org.xml.sax.InputSource import java.io.File +import java.io.FileReader import javax.xml.bind.JAXBContext import javax.xml.bind.annotation.XmlAnyElement import javax.xml.bind.annotation.XmlElement import javax.xml.bind.annotation.XmlRootElement +import javax.xml.parsers.SAXParserFactory +import javax.xml.transform.sax.SAXSource @XmlRootElement(name = "project") class PomProject { var modelVersion: String? = null var groupId: String? = null + get() { + if (field != null && field!!.contains("\${")) { + println("VARIABLE GROUP") + } + return field + } var artifactId: String? = null var version: String? = null + get() { + if (field != null && field!!.contains("\${")) { + println("VARIABLE VERSION") + } + return field + } var name: String? = null var description: String? = null var url: String? = null @@ -22,7 +38,13 @@ class PomProject { var scm: Scm? = null var properties: Properties? = null var parent: Parent? = null - var dependencies: Dependencies? = null + @XmlElement(name = "dependencies") @JvmField + var pomDependencies: Dependencies? = null + val dependencies: List + get() = + if (pomDependencies != null) pomDependencies!!.dependencies + else emptyList() + var pluginRepositories: PluginRepositories? = null val propertyMap = hashMapOf() @@ -37,19 +59,23 @@ class PomProject { } fun main(argv: Array) { - Pom2().read("/Users/beust/t/pom.xml") + val p = Pom2(File("/Users/beust/t/pom.xml")) + val pom = p.pom + println("Dependencies: " + pom.dependencies[0]) } -class Pom2 { - fun read(s: String) { - val ins = File(s).inputStream() +class Pom2(val documentFile: File) { + val pom: PomProject by lazy { + val ins = documentFile.inputStream() val jaxbContext = JAXBContext.newInstance(PomProject::class.java) - val pom = jaxbContext.createUnmarshaller().unmarshal(ins) as PomProject - println("License: " + pom.licenses?.licenses!![0].name) - println("Developer: " + pom.developers?.developers!![0].name) - println("Scm: " + pom.scm?.connection) - println("Properties: " + pom.propertyValue("kotlin.version")) - println("Plugin repositories: " + pom.pluginRepositories?.pluginRepository!![0]) + val unmarshaller = jaxbContext.createUnmarshaller() + + val sax = SAXParserFactory.newInstance() + sax.isNamespaceAware = false + val reader = sax.newSAXParser().xmlReader + val er = SAXSource(reader, InputSource(FileReader(documentFile))) + + unmarshaller.unmarshal(er) as PomProject } } @@ -96,11 +122,36 @@ class Dependencies { } class Dependency { - var groupId: String? = null - var artifactId: String? = null - var version: String? = null - var scope: String? = null - var packaging: String? = null + var groupId: String = "" + fun groupId(pom: Pom2) : String = expandVariable(groupId, pom) + + var artifactId: String = "" + fun artifactId(pom: Pom2) : String = expandVariable(artifactId, pom) + + var version: String = "" + fun version(pom: Pom2) : String = expandVariable(version, pom) + + var optional: String = "false" + var scope: String = "" + var packaging: String = "" + + val id: String = "$groupId:$artifactId:$version" + + val mustDownload: Boolean + get() = ! optional.toBoolean() && "provided" != scope && "test" != scope + + val isValid : Boolean get() = ! isVariable(groupId) && ! isVariable(artifactId) && ! isVariable(version) + + private fun isVariable(s: String) = s.startsWith("\${") && s.endsWith("}") + + private fun expandVariable(s: String, pom: Pom2) : String { + if (isVariable(s)) { + println("Expanding variable $s") + return s + } else { + return s + } + } } class Scm { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinderCallable.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinderCallable.kt index 457382a5..faeca5e2 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinderCallable.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinderCallable.kt @@ -91,7 +91,7 @@ class RepoFinderCallable @Inject constructor(@Assisted val id: String, File(localRepo.toFullPath(depPomFile)).let { pomFile -> pomFile.parentFile.mkdirs() Kurl(HostConfig(url)).toFile(pomFile) - val dependencies = pomFactory.create(id, pomFile).dependencies + val dependencies = Pom2(pomFile).pom.dependencies val result = arrayListOf() dependencies.map { it.id }.forEach { result.addAll(RepoFinderCallable(it, repo, localRepo, pomFactory).call()) 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 974e9dc3..a2df2042 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 @@ -7,6 +7,7 @@ import com.beust.kobalt.api.Kobalt import com.beust.kobalt.maven.* import com.beust.kobalt.misc.DependencyExecutor import com.beust.kobalt.misc.Versions +import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn import com.google.inject.Key import com.google.inject.assistedinject.Assisted @@ -120,11 +121,15 @@ class MavenDependency @Inject constructor( override fun directDependencies() : List { val result = arrayListOf() try { - val pom = pomFactory.create(id, pomFile.get()) - pom.dependencies.filter { + val pom = Pom2(pomFile.get()) + pom.pom.dependencies.filter { it.mustDownload }.forEach { - result.add(create(MavenId.toId(it.groupId, it.artifactId, it.packaging, it.version))) + if (it.isValid) { + result.add(create(MavenId.toId(it.groupId(pom), it.artifactId(pom), it.packaging, it.version(pom)))) + } else { + log(2, "Skipping invalid id: ${it.id}") + } } } catch(ex: Exception) { warn("Exception when trying to resolve dependencies for $id: " + ex.message)