我使用 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 并连接数据库时。它工作得很好但是当我在构建过程中使用命令时,./gradlew assemble
我不知道如何在构建过程中指定特定的主机、端口或用户名,因为我只想构建一次并永远使用。当我明确./gradlew assemble --host=abc
它不起作用时。每次构建和运行时如何具体参数。我不希望每个运行的 jar 都必须使用特定的主机端口或用户名。谢谢