gpt4 book ai didi

java - Gradle依赖资源访问

转载 作者:行者123 更新时间:2023-12-03 05:43:00 26 4
gpt4 key购买 nike

如何从依赖项访问资源?我有这样的事情:

包装项目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会是什么。

关于java - Gradle依赖资源访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51594329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com