diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 484b33ef..7dc11647 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -55,6 +55,7 @@ private class Main @Inject constructor( println(Banner.get() + Kobalt.version + "\n") // runTest() runWithArgs(jc, args) + log(1, "************ shutting down executors") executors.shutdown() debug("All done") }) diff --git a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt index f46968c2..7a7c2129 100644 --- a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt @@ -75,14 +75,46 @@ public class ProjectGenerator : KobaltLogger { put("version", pom.version ?: "0.1") put("name", pom.name ?: pom.artifactId) } + + val properties = pom.properties + val mapped = properties.entrySet().toMap({it.key}, {translate(it.key)}) + + map.put("properties", properties + .entrySet() + .map({ Pair(mapped.get(it.key), it.value) })) val partition = pom.dependencies.groupBy { it.scope } .flatMap { it.value } + .map { updateVersion(it, mapped) } .sortedBy { it.groupId + ":" + it.artifactId } .partition { it.scope != "test" } + mainDeps.addAll(partition.first) testDeps.addAll(partition.second) } + private fun updateVersion(dep: Dependency, mapped: Map): Dependency { + if ( dep.version.startsWith("\${")) { + val property = dep.version.substring(2, dep.version.length() - 1) + println("property = ${property}") + return Dependency(dep.groupId, dep.artifactId, "\${${mapped.get(property)}}", dep.optional, dep.scope) + } else { + return dep + } + } + + private fun translate(key: String) : String { + val split = key.split('.') + return split.mapIndexed( { index, value -> if (index == 0) value else value.upperFirst() }).join("") + } + + private fun String.upperFirst(): String { + if (this.isBlank()) { + return this + } else { + return this.substring(0, 1).toUpperCase() + this.substring(1) + } + } + /** * Detect all the languages contained in this project. */ diff --git a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt index d5537735..deb7bbdd 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt @@ -5,12 +5,10 @@ import com.beust.kobalt.misc.ToString import com.google.inject.assistedinject.Assisted import org.w3c.dom.Element import org.w3c.dom.NodeList -import org.w3c.dom.Text import org.xml.sax.InputSource import java.io.FileReader import javax.xml.xpath.XPathConstants -import kotlin.dom.get -import kotlin.dom.toXmlString +import kotlin.dom.childElements public class Pom @javax.inject.Inject constructor(@Assisted val id: String, @Assisted documentFile: java.io.File) : KobaltLogger { @@ -65,6 +63,12 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, version = XPATH.compile("/project/version").evaluate(document) name = XPATH.compile("/project/name").evaluate(document) + var list = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList + var elem = list.item(0) as Element? + elem.childElements().forEach { + properties.put(it.nodeName, it.textContent) + } + val deps = DEPENDENCIES.evaluate(document, XPathConstants.NODESET) as NodeList for (i in 0..deps.getLength() - 1) { val d = deps.item(i) as NodeList diff --git a/src/main/resources/build-template.mustache b/src/main/resources/build-template.mustache index 89a96586..79d8e088 100644 --- a/src/main/resources/build-template.mustache +++ b/src/main/resources/build-template.mustache @@ -2,7 +2,12 @@ import com.beust.kobalt.* import com.beust.kobalt.plugin.packaging.assemble {{imports}} +{{#properties}} +val {{first}} = {{second}} +{{/properties}} + val p = {{directive}} { + name = "{{name}}" group = "{{group}}" artifactId = name diff --git a/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt b/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt new file mode 100644 index 00000000..376cd79a --- /dev/null +++ b/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt @@ -0,0 +1,19 @@ +package com.beust.kobalt.maven + +import org.testng.Assert +import org.testng.annotations.Test +import java.io.File + +class PomTest { + @Test + fun importPom() { + val pom = Pom("testing", File("src/test/resources/pom.xml")); + + Assert.assertEquals(pom.groupId, "com.foo.bob") + Assert.assertEquals(pom.artifactId, "rawr") + Assert.assertEquals(pom.name, "rawr") + Assert.assertEquals(pom.version, "1.2.3") + Assert.assertEquals(pom.properties.get("commons.version"), "2.1.1") + Assert.assertEquals(pom.properties.get("guice.version"), "4.0") + } +} \ No newline at end of file diff --git a/src/test/resources/pom.xml b/src/test/resources/pom.xml new file mode 100644 index 00000000..43be77d6 --- /dev/null +++ b/src/test/resources/pom.xml @@ -0,0 +1,152 @@ + + 4.0.0 + + com.foo.bob + rawr + jar + 1.2.3 + + rawr + + + + sonatype + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + install + + + src/main/resources + + something.properties + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + true + + 1.8 + 1.8 + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + + compile + compile + + compile + + + + + test-compile + test-compile + + test-compile + + + + + + org.apache.maven.plugins + maven-release-plugin + 2.4.1 + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + com.foo.bob.Rawr + test + + -Xmx512m + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + true + lib/ + com.foo.bob.Rawr + + + + + + + + + org.apache.commons + commons-email + ${commons.version} + + + + com.google.inject + guice + no_aop + ${guice.version} + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + org.testng + testng + 6.8.7 + test + + + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.5.2 + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.16 + + false + + + + + + + 2.1.1 + 4.0 + UTF-8 + +