好的,所以我一直在努力使其正常作業,我只想撰寫我的測驗并將其推送到 maven,因此我的站立更新日復一日不一樣。
問題陳述:我有多個 android 專案,它們都需要相同的預配置。這包括宣告 Maven 存盤庫、設定 versionCodes 和 versionNames 以及其他其他配置項。相同的塊被復制并粘貼到每個專案中或放置在本地common.gradle檔案中,然后在專案級build.gradle配置階段應用和呼叫。
我的解決方案:將所有這些通用邏輯提取到一個獨立的 gradle 插件中,并讓每個專案的 build.gradle 檔案應用這個插件來接收這些配置事件。
我創建了一個獨立的插件,對插件進行了覆寫,它看起來像這樣:
class CommonManager: Plugin<Project> {
override fun apply(target: Project) {
println("APPLY FUNCTION HAS BEEN ENTERED")
target.tasks.create("myPluginTask", ReleaseManager::class.java)
}
ReleaseManager 類是一個擴展 DefaultTask 并創建要運行的任務的類。這是 gradle 檔案中推薦的內容,從可測驗性/可重用性的角度來看是有意義的,并且該類看起來像這樣:
abstract class ReleaseManager: DefaultTask() {
@get:Input
abstract val versionHistoryPath: Property<String>
@TaskAction
fun configureVersionsAndReleaseMeta() { // do work... }
根據我讀過的檔案和我翻閱過的其他示例專案,這是正確設定的。我使用 java-library 而不是 java 構建插件只是為了在我的 android 專案中指向一個本地 .jar 檔案以進行快速測驗。這些 build.gradle 檔案之一的示例如下所示:
buildscript {
repositories {
google()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
// misc dependencies ...
classpath files("libs/MyCustomPlugin-1.0-SNAPSHOT.jar")
}
}
apply plugin: "com.myplugin.common"
myPluginTask {
versionHistoryPath.set("sdk/version_history.txt")
}
subprojects {
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
group = 'org.jfrog.test.gradle.publish'
}
I create a dependency on the .jar and apply the plugin, then configure the task with the property it needs to run. However, the task never runs during the configuration phase when the log outputs > Configure project I see my print statement from the apply function in my plugin log but no output or configuration happens in the task class that I defined. If I click the play arrow next to the myPluginTaks{} configuration definition, it does in fact run fine. At this point, I know I'm pointing to the .jar correctly, I can access the task that I create in my plugin class, and I can set its property respectively. I'm newer to doing this kind of custom work with gradle and want to understand what I am doing wrong. Let me outline my thought process and debugging steps I have tried.
My "Bug" Statement: I want the task "myPluginTask" to execute after I apply my plugin in my build.gradle script and I have configured the task with the proper input. It is currently not executing the task.
Thoughts and notes:
I used
createoverregisterin the plugin apply function when creating my task. My understanding is that by using create, I am saying "hey I want to create a task, and I want the build to know about it immediately" rather than "Hey here's a task definition, but lets lazy load it once it is actually invoked on the implementation end"我將 的實作
ReleaseManager作為單獨的函式移動到插件類中,并在任務創建閉包中呼叫這些函式。那個有效。據我了解,這是因為其中定義的任何內容都在構建配置階段運行。如果我愿意,我可以使用 adoFirst或doLast閉包將其推遲到執行階段。我的預感是,我已經創建了一個任務并且確保構建現在立即知道它,但是它至少在執行階段之前沒有執行任務的計劃并且需要一個觸發器。該觸發器是直接呼叫任務還是任務取決于將在執行階段運行的任務。
任何幫助將不勝感激。如果需要,很樂意提供任何其他資訊。
uj5u.com熱心網友回復:
下面的用戶提供了一個有見地的鏈接,幫助我找到了解決方案。在這一點上,我對 Gradle 也有了更好的理解,并且可以更好地闡明我的問題。
TLDR;我的預感是正確的。我已經成功創建了一個任務,但沒有提供觸發此類任務的方法。您可以根據目標專案提供的任務執行此操作。
重申一下,我的插件類如下所示:
class CommonManager: Plugin<Project> {
override fun apply(target: Project) {
println("APPLY FUNCTION HAS BEEN ENTERED")
target.tasks.create("myPluginTask", ReleaseManager::class.java)
}
我應用插件的模塊級 build.gradle 檔案具有以下內容:
apply plugin: "com.myplugin.common"
apply plugin: "com.android.library"
apply 函式將在我的插件中觸發并創建任務。但是它不會運行。為了實作這一點,我需要依賴目標構建中的另一個任務作為觸發器。這實際上如下所示:
class CommonManager: Plugin<Project> {
override fun apply(target: Project) {
println("APPLY FUNCTION HAS BEEN ENTERED")
target.tasks.create("myPluginTask", ReleaseManager::class.java) { task ->
// Have target project create a dependency between its task and mine
target.tasks.findByName("preBuild").dependsOn(task)
}
“preBuild”是android插件提供的一個任務。就我而言,com.android.library這會導致問題二。在構建我的專案時,插件會拋出一個錯誤,指出“preBuild”不是一個定義的任務。起初我以為我只是無法參考 android 庫提供的任務,但后來它擊中了我。如果您查看我的模塊 build.gradle 檔案的上述代碼塊,您會注意到我首先定義了我的通用插件。這意味著在我的插件應用塊觸發時,插件不知道此任務,因為尚未應用 android 插件。
您可以先定義 android 插件并記錄定義順序很重要,但這不是一個務實的解決方案。但是,您可以做的是afterEvaluate在目標專案上實作閉包,如下所示:
class CommonManager: Plugin<Project> {
override fun apply(target: Project) {
println("APPLY FUNCTION HAS BEEN ENTERED")
target.tasks.create("myPluginTask", ReleaseManager::class.java) { task ->
target.afterEvaluate {
// Have target project create a dependency between its task and mine
target.tasks.findByName("preBuild").dependsOn(task)
}
}
這將等待模塊 build.gradle 檔案的評估完成。因此,一旦我的 plugins apply 塊被觸發,我的插件就會知道“preBuild”任務,因為在專案評估期間應用了 android 插件。
額外提示:我正在使用 create 在插件中創建我的任務。如果您正在使用 register,則不需要afterEvaluate關閉。如果你定義了一個,如果運行 gradle 7.0 或更高版本,插件會拋出一個運行時錯誤。gradle 社區認為這是一種冗余。
uj5u.com熱心網友回復:
我不太確定這是否會對您有所幫助。
你有沒有試過
myPluginTask.configure {
mustRunAfter TASKNAME
}
我環顧四周,這也可能有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315023.html
