我正在尝试将我在 ANT 构建中的任务转换为 Gradle:
<target name="index-assets" depends="copy-assets">
<path id="assets.path">
<!-- contexts (and surplus) -->
<dirset id="assets.dirset" dir="assets/" defaultexcludes="yes"/>
<!-- assets -->
<fileset id="assets.fileset" dir="assets/" includes="**" excludes="asset.index" defaultexcludes="yes"/>
</path>
<pathconvert pathsep="${line.separator}" property="assets" refid="assets.path" dirsep="/">
<mapper>
<globmapper from="${basedir}/assets/*" to="*" handledirsep="yes"/>
</mapper>
</pathconvert>
<echo file="assets/asset.index">${assets}</echo>
</target>
<target name="-pre-build" depends="index-assets"/>
我想我仍然没有完全掌握基本的 Gradle 概念,但这是我尝试过的:
task indexAssets << {
def assets = file("assets")
def contexts = files(assets)
inputs.file(assets)
outputs.file("assets/assets-gradle.index")
def tree = fileTree(dir: 'assets', include: ['**/*'], exclude: ['**/.svn/**', 'asset.index'])
contexts.collect { relativePath(it) }.sort().each { println it }
tree.collect { relativePath(it) }.sort().each { println it }
}
- 树很好,但只包含文件(叶子)路径
- 不过,我似乎无法获得简单干净的目录(上下文)列表。我尝试了其他几种变体(树、包含/排除),但我要么在该目录中获取单个文件,要么获取目录名称本身,要么什么都没有。我只想在“资产”目录中找到一个简单的目录列表。
现在我只是想打印路径,但我也想知道以后将它们写入文件(如 ANT 的回显文件)的正确方法。
更新:
这个 groovy 片段似乎完成了那部分(+ svn 过滤器),但我宁愿找到一种更“Gradley”的方式来完成这项任务。它稍后作为预构建依赖项在构建变体的上下文中运行。(注意:我必须将“项目”指定为此 hack 中路径的一部分,因为我想我不在该任务的项目上下文中?)def list = [] def dir = new File("Project/assets") dir.eachDirMatch (~/^(?!\.svn).*/) { file -> list << file } list.each { println it.name }