我創建了一個連接到資料庫的 JavaExec 任務并進行了一些檢查。在我的 flyway build.gradle 中,我這樣呼叫任務:
flywayMigrate.finalizedBy(rootProject.checkOracleStandards)
該任務作業正常,但問題是連接 url、用戶和密碼在連接到資料庫并進行檢查的程式中被硬編碼。我想將它們作為引數傳遞給自定義任務。
flywayMigrate后如何使用args運行自定義任務?
這是我的任務 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'
}
uj5u.com熱心網友回復:
由于在任務之間共享/傳遞引數的最佳方式是通過檔案,讓任務將它們寫入某個檔案中,然后讓您的checkOracleStandards任務從該檔案加載它們。
確保將引數寫入doLast塊中的檔案,以避免每次 gradle 同步時都運行任務。
最后,讓您的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")
}
如果不確定,應該使“傳遞引數”任務的這一部分在您的checkOracleStandards任務之前運行,然后只需修改您的checkOracleStandards任務以從此檔案中讀取引數并使用它們。
編輯:讓我添加一些與答案本身無關的說明——我會發表評論;但是 SO 改變了一些東西,我的瀏覽器目前無法在任何地方發表評論,所以我給了你一個應該只是評論的答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/381959.html
標籤:爪哇 等级 飞行路线 gradle-task
