[英]Gradle dependency resource access
如何從依賴項訪問資源? 我有這樣的事情:
包裝項目build.gradle
..
dependencies {
compile com.company:mysubproject1:2.0.1
compile com.company:mysubproject2:2.0.1
..
這些子項目在其資源目錄中有一些文件,例如src / main / resources / liquibase / changelog.xml
這些子項目中可以有n個,我需要我的gradle任務來遍歷所有依賴項並獲取所有changelog.xml文件,並從中創建新文件,以供以后使用。
實際上並不難:
doFirst {
println 'Generating master changelog...'
def changelogFiles = []
configurations.runtime.each {
def zip = new ZipFile(it)
def entries = zip.entries()
entries.findAll { entry -> entry.name.contains("liquibase") }
.findAll { entry -> entry =~ /changelog.xml/ }
.each { entry ->
changelogFiles.add(((ZipEntry) entry).name)
}
}
def resDir = sourceSets.main.output.resourcesDir.path + '/liquibase'
def changelogFile = new File("$resDir/changelog-master.xml")
changelogFile.write("<databaseChangeLog xmlns=\"http://www.liquibase.org/xml/ns/dbchangelog\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd\">\n\n")
changelogFiles.each {
println("Including $it to changelog.")
changelogFile << " <include file=\"classpath:$it\"/> \n"
}
changelogFile << "\n</databaseChangeLog>"
}
此代碼僅循環遍歷依賴項,並為名為“ changelog.xml”且路徑中具有“ liquibase”的文件挖掘文件路徑。
可能的問題是,如果2個依賴項具有相同名稱和路徑的文件,則在這種情況下我不知道classpath會是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.