From a6966f1c60efaecde739b3bd5f8625456d6bd545 Mon Sep 17 00:00:00 2001 From: Alexander Shustanov Date: Tue, 6 Oct 2020 16:59:53 +0400 Subject: [PATCH] Support uberjar and war tasks generation for CUBA 7.2 #191 --- .../cli/cubaplugin/deploy/uberjar/UberJarCommand.kt | 5 ++++- .../cli/cubaplugin/deploy/uberjar/UberJarModel.kt | 2 +- .../cuba/cli/cubaplugin/deploy/war/WarCommand.kt | 7 +++++-- .../cuba/cli/cubaplugin/model/ProjectModel.kt | 12 +++++++----- .../com/haulmont/cuba/cli/generation/XmlUtils.kt | 4 +++- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarCommand.kt b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarCommand.kt index c3d2c2c..b4daa39 100644 --- a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarCommand.kt +++ b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarCommand.kt @@ -21,6 +21,7 @@ import com.haulmont.cuba.cli.* import com.haulmont.cuba.cli.commands.GeneratorCommand import com.haulmont.cuba.cli.cubaplugin.deploy.ContextXmlParams import com.haulmont.cuba.cli.cubaplugin.di.cubaKodein +import com.haulmont.cuba.cli.cubaplugin.model.DataSourceProvider import com.haulmont.cuba.cli.generation.TemplateProcessor import com.haulmont.cuba.cli.prompting.Answers import com.haulmont.cuba.cli.prompting.QuestionsList @@ -63,7 +64,9 @@ class UberJarCommand : GeneratorCommand() { askIf("specifyLogback") } - confirmation("generateCustomJetty", "Generate custom Jetty environment file?") + if(projectModel.database.dataSourceProvider == DataSourceProvider.JNDI) { + confirmation("generateCustomJetty", "Generate custom Jetty environment file?") + } ContextXmlParams.run { askContextXmlParams(projectModel, questionName = "customJettyContextParams", askCondition = "generateCustomJetty") diff --git a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarModel.kt b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarModel.kt index 44b1003..5118c65 100644 --- a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarModel.kt +++ b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/uberjar/UberJarModel.kt @@ -25,7 +25,7 @@ class UberJarModel(answers: Answers, projectModel: ProjectModel) { val generateLogback: Boolean by answers val customLogback: String? by answers.withDefault { null } - val generateCustomJetty: Boolean by answers + val generateCustomJetty: Boolean by answers.withDefault { false } val jettyContextParams = answers["customJettyContextParams"]?.let { ContextXmlParams(it as Answers, projectModel) } val customJettyPath: String? by answers.withDefault { null } diff --git a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/war/WarCommand.kt b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/war/WarCommand.kt index f7eb649..5e86db9 100644 --- a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/war/WarCommand.kt +++ b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/deploy/war/WarCommand.kt @@ -21,6 +21,7 @@ import com.haulmont.cuba.cli.* import com.haulmont.cuba.cli.commands.GeneratorCommand import com.haulmont.cuba.cli.cubaplugin.deploy.ContextXmlParams import com.haulmont.cuba.cli.cubaplugin.di.cubaKodein +import com.haulmont.cuba.cli.cubaplugin.model.DataSourceProvider import com.haulmont.cuba.cli.generation.TemplateProcessor import com.haulmont.cuba.cli.prompting.Answers import com.haulmont.cuba.cli.prompting.QuestionsList @@ -60,8 +61,10 @@ class WarCommand : GeneratorCommand() { confirmation("includeTomcatContextXml", "Include Tomcat's context.xml?") - confirmation("generateContextXml", "Generate custom context.xml?") { - askIf("includeTomcatContextXml") + if(projectModel.database.dataSourceProvider == DataSourceProvider.JNDI) { + confirmation("generateContextXml", "Generate custom context.xml?") { + askIf("includeTomcatContextXml") + } } ContextXmlParams.run { diff --git a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/model/ProjectModel.kt b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/model/ProjectModel.kt index 5155bf7..c6e5003 100644 --- a/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/model/ProjectModel.kt +++ b/src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/model/ProjectModel.kt @@ -133,7 +133,8 @@ private fun parseJndiDatabase(projectStructure: ProjectStructure): Database? { .resolve("context.xml") val contextXmlRoot = parse(contextXml).documentElement - val resourceElement = contextXmlRoot.xpath("//Resource[@name=\"jdbc/CubaDS\"]").firstOrNull() as? Element ?: return null + val resourceElement = contextXmlRoot.xpath("//Resource[@name=\"jdbc/CubaDS\"]").firstOrNull() as? Element + ?: return null return Database( getDbTypeByDriver(resourceElement["driverClassName"]), @@ -210,12 +211,13 @@ fun getPrefixUrl(driverClass: String): String = when { else -> throw ProjectScanException("Unrecognized jdbc driver class $driverClass") } -fun getConnectionUrl(dbType: String, prefix: String, host: String, connectionParams: String?, dbName: String) : String { - when(dbType) { +fun getConnectionUrl(dbType: String, prefix: String, host: String, connectionParams: String?, dbName: String): String { + return when (dbType) { "hsql" -> prefix + host + "/" + dbName + (connectionParams?.let { ";$it" } ?: "") - "postgres", "mssql", "oracle", "mysql" -> prefix + host + "/" + dbName + (connectionParams?.let { "?$it" } ?: "") + "postgres", "mssql", "oracle", "mysql" -> prefix + host + "/" + dbName + (connectionParams?.let { "?$it" } + ?: "") + else -> throw ProjectScanException("Unable to construct jdbc url") } - throw ProjectScanException("Unable to construct jdbc url") } private fun Sequence.getGroupValue(groupIndex: Int): String? = diff --git a/src/main/kotlin/com/haulmont/cuba/cli/generation/XmlUtils.kt b/src/main/kotlin/com/haulmont/cuba/cli/generation/XmlUtils.kt index 98d2e26..677163b 100644 --- a/src/main/kotlin/com/haulmont/cuba/cli/generation/XmlUtils.kt +++ b/src/main/kotlin/com/haulmont/cuba/cli/generation/XmlUtils.kt @@ -61,7 +61,9 @@ private fun createDocumentBuilder(): DocumentBuilder { } fun save(document: Document, path: Path) = DOMSerializer(numIndentSpaces = 4).run { - serialize(document, Files.newOutputStream(path)) + Files.newOutputStream(path).use { outputStream -> + serialize(document, outputStream) + } } fun updateXml(path: Path, block: Element.() -> Unit) {