Gradle AntBuilder
每個Gradle項目都包含一個AntBuilder實例,從而使您的構建文件中可以使用任何和所有Ant功能。 Gradle提供了對現有Groovy AntBuilder的簡單擴展,它增加了一種與現有Ant構建文件交互的簡單而強大的方法: importBuild(Object antBuildFile)方法。 在內部,此方法利用Ant ProjectHelper解析指定的Ant構建文件,然后將所有目標包裝在Gradle任務中,以使它們在Gradle構建中可用。 以下是用于說明的簡單Ant構建文件,其中包含一些屬性和幾個相關的目標。
<?xml version='1.0'?>
<project name='build' default='all'><echo>Building ${ant.file}</echo><property file='build.properties'/><property name='root.dir' location='.'/><target name='dist' description='Build the distribution'><property name='dist.dir' location='dist'/><echo>dist.dir=${dist.dir}, foo=${foo}</echo></target><target name='all' description='Build everything' depends='dist'/>
</project>
使用Gradle導入此構建文件是一種方法。
ant.importBuild('src/main/resources/build.xml')
gradle任務的輸出–全部在命令行上顯示,目標已添加到構建任務中。
$ gradle tasks --all
...
Other tasks
-----------
all - Build everythingdist - Build the distribution
...
Ant構建文件中使用的屬性可以在Gradle構建或命令行中指定,并且與通常的Ant屬性行為不同,Ant或命令行上設置的屬性可能被Gradle覆蓋。 給定一個簡單的build.properties文件,其中的foo = bar為單個條目,這里有一些組合來演示覆蓋行為。
命令行調用 | Gradle構建配置 | 影響 | 結果 |
---|---|---|---|
gradle dist | ant.importBuild('src / main / resources / build.xml') | 使用從ant build加載的build.properties值 | foo = bar |
gradle dist -Dfoo = NotBar | ant.importBuild('src / main / resources / build.xml') | 使用命令行屬性 | foo = NotBar |
gradle dist -Dfoo = NotBar | ant.foo ='NotBarFromGradle' ant.importBuild('src / main / resources / build.xml') | 使用Gradle build屬性 | foo = NotBarFromGradle |
gradle dist -Dfoo = NotBar | ant.foo ='NotBarFromGradle' ant.importBuild('src / main / resources / build.xml') ant.foo ='NotBarFromGradleAgain' | 使用Gradle構建屬性覆蓋 | foo = NotBarFromGradleAgain |
如何處理任務名稱沖突
由于Gradle堅持任務名稱的唯一性,因此嘗試導入包含與現有Gradle任務名稱相同的目標的Ant構建會失敗。 我遇到的最常見的沖突是Gradle BasePlugin提供的clean任務。 借助一些間接的幫助,我們仍然可以通過使用GradleBuild任務來導入和使用任何沖突目標,以在獨立的Gradle項目中引導Ant構建導入。 讓我們在導入的Ant構建中向混合添加一個新任務,并對all任務依賴于螞蟻清理目標添加另一個依賴。
<!-- excerpt from buildWithClean.xml Ant build file --><target name='clean' description='clean up'><echo>Called clean task in ant build with foo = ${foo}</echo></target><target name='all' description='Build everything' depends='dist,clean'/>
還有一個簡單的Gradle構建文件,它將處理導入。
ant.importBuild('src/main/resources/buildWithClean.xml')
最后,在主gradle構建文件中,我們添加了一個任務來運行所需的目標。
task importTaskWithExistingName(type: GradleBuild) { GradleBuild antBuild ->antBuild.buildFile ='buildWithClean.gradle'antBuild.tasks = ['all']
}
這行得通,但不幸的是遇到了一個小問題 。 當Gradle導入這些任務時,它沒有正確遵守依賴項的聲明順序。 而是按字母順序執行從屬螞蟻目標。 在這種特殊情況下,Ant希望在clean之前執行dist目標,而Gradle則以相反的順序執行它們。 可以通過明確說明任務順序來解決此問題,該任務順序絕對不理想,但可行。 這個Gradle任務將按照我們需要的方式執行底層的Ant目標。
task importTasksRunInOrder(type: GradleBuild) { GradleBuild antBuild ->antBuild.buildFile ='buildWithClean.gradle'antBuild.tasks = ['dist', 'clean']
}
其余的Gradle規則
最后,您可以使用Gradle Rule來允許在GradleBuild自舉導入中調用任意目標。
tasks.addRule('Pattern: a-<target> will execute a single <target> in the ant build') { String taskName ->if (taskName.startsWith('a-')) {task(taskName, type: GradleBuild) {buildFile = 'buildWithClean.gradle'tasks = [taskName - 'a-']}}
}
在此特定示例中,這還可以使您將調用串聯在一起,但要警告它們在完全隔離的環境中執行。
$ gradle a-dist a-clean
源代碼
如果您想仔細看一看,本文中引用的所有代碼都可以在github上找到。
相關文章:
- 為什么我喜歡Gradle?
- 一個Groovy / Gradle JSLint插件
- 使用Groovy腳本可以做的五件事
參考:在The Kaptain on…內容博客中, 使用Gradle從我們的JCG合作伙伴 Kelly Robinson 引導您的Legacy Ant構建 。
翻譯自: https://www.javacodegeeks.com/2012/08/using-gradle-to-bootstrap-your-legacy.html