简体   繁体   English

在 finalizedBy 中调用任务时,如何使用 arguments 运行 JavaExec gradle 任务?

[英]How to run a JavaExec gradle task with arguments when the task is called in finalizedBy?

I created a JavaExec task that connects to a db and does some checks.我创建了一个连接到数据库并进行一些检查的 JavaExec 任务。 In my flyway build.gradle I call the task like this:在我的飞行路线 build.gradle 我这样称呼任务:

flywayMigrate.finalizedBy(rootProject.checkOracleStandards)

The task works fine, but the problem is that the connection url, user and password are hardcoded in the program that connects to the db and does the checks.该任务工作正常,但问题是连接 url、用户和密码在连接到数据库并进行检查的程序中被硬编码。 I want to pass them as args to the custom task.我想将它们作为参数传递给自定义任务。

How to run the custom task with args after flywayMigrate? flywayMigrate后如何使用args运行自定义任务?

This is how my task gradle file looks like:这就是我的任务 gradle 文件的样子:

apply plugin: 'java'

dependencies {
    implementation rootProject.files("libs/check-oracle-db-standards-1.jar")

    implementation group: 'com.oracle.database.jdbc', name: 'ojdbc8', version: '21.3.0.0'
    implementation group: 'org.springframework', name: 'spring-jdbc', version: '5.3.13'
    implementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
}

task checkOracleStandards(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'com.package.checkoracledbstandards.Main'
}

Since the best way to share/pass arguments between tasks is through files, have a task write them to a file somewhere, and then have your checkOracleStandards task load them from that file.由于在任务之间共享/传递 arguments 的最佳方法是通过文件,因此让任务将它们写入某个文件,然后让您的checkOracleStandards任务从该文件加载它们。

Make sure the arguments are written to the file in the doLast block, to avoid having the task run every time gradle syncs for example.确保将 arguments 写入doLast块中的文件,以避免每次 gradle 同步时都运行任务。

Finally, have your checkOracleStandards task open up the file, parse the parameters and use them somehow.最后,让您的checkOracleStandards任务打开文件,解析参数并以某种方式使用它们。

val outputPath = "${rootProjectPath}/build/check_params" <-- you may not want to use a folder for this
val paramFile = file("${outputPath}/check_params")

 doLast {
            if (paramFile.exists().not()) {
                paramFile.writeText()

                File(outputPath)
                    .walk()
                    .maxDepth(1)
                    .filter {
                        it.isFile
                                && it.name.endsWith("_params")
                    }
                    .forEach {
                        println("Passing params of ${it.name} into ${paramsFile.absolutePath}")
                        // PARSE params here
                        paramsFile.appendText("url: ${use_your_real_url}\tuser: ${use_your_real_user}\tpass: ${use_the_password}")
                        paramsFile.appendText("\n")
                    }

if unsure, should make this part of a "pass params" task to run before your checkOracleStandards task, and then just modify your checkOracleStandards task to read params from this file and use them.如果不确定,应该在您的checkOracleStandards任务之前运行“传递参数”任务的这一部分,然后只需修改您的checkOracleStandards任务以从该文件中读取参数并使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM