使用 build gradle 和 picocli 时如何将参数传递给 kotlin

问题描述

我使用 kotlin、gradle 和 picocli 构建原生镜像。我的配置看起来像:

  @CommandLine.Command(name = "studentService",description = arrayOf("Student "))
    class StudentService() : Runnable,DataSource() {
    
        @CommandLine.Parameters(arity = "1..*",index = "0")
        var name: String? = null
    
        @Spec
        protected var spec: CommandSpec? = null
    
        override fun run() {
         //something run
        }
    
        fun getDataBase() {
            connectDatabase()
            transaction() {
                //do something logic
            }
        }
    }

申请文件

@picocli.CommandLine.Command(
    name = "demo",description = ["Cli"],version = ["Cli 0.0.1"],mixinStandardHelpOptions = true,defaultValueProvider = PropertiesDefaultProvider::class,subcommands = [DataSource::class,StudentService::class]
)
class AppApplication : Runnable {

    override fun run() {
        println("Begin CLI")
    }
}

fun main(args: Array<String>) {
    val properties = DbConfig.loadProperties("application.yml")
    val exitCode = execute(AppApplication::class.java,args,properties)
    System.exit(exitCode)
}

fun execute(clazz: Class<*>,args: Array<String>,properties: Properties): Int {
    ApplicationContext.build(
            clazz,Environment.CLI).start().use { context ->

        return CommandLine(clazz,MicronautFactory(context))
                .addSubcommand("execute",StudentService())
                .setExecutionStrategy(CommandLine.RunAll())
                .setDefaultValueProvider(CommandLine.PropertiesDefaultProvider(properties))
                .setCaseInsensitiveEnumValuesAllowed(true)
                .setUsageHelpAutoWidth(true)
                .execute(*args)
    }
}

应用程序.yml

host: localhost
database: abc
port: 2334
username: example
password: example

当我运行 ./gradlew run --args="studentService someName" 时,它运行成功。如果我没有特定的信息数据库,它会从 application.yml 获得认值。当我指定 ./gradlew run --args="studentService someName --host=abc" 时,它会获取主机 abc 并连接 db。它工作完美 但是当我在构建 ./gradlew assemble 期间使用命令时,我不知道如何在构建期间指定主机、端口或用户名,因为我只想构建一次并永远使用。当我指定 ./gradlew assemble --host=abc 时它不起作用。每次构建和运行时如何指定参数。我不希望每个正在运行的 jar 都必须特定的主机端口或用户名。谢谢你

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)