英文:
what are equivalent of Ant <resources>, <patternset> in gradle?
问题
我正在将基于 Ant 的项目迁移到 Gradle 项目。我必须在许多任务中使用一组文件或模式,这导致我编写重复的代码,而实际上可以简单地将其保存在 Gradle 的资源(resources)和模式集(patternset)标签中。在 Gradle 中是否有类似于 resources 或 patternset 的等效方法呢?
<patternset id="pattern.files.to.instrument">
<include name="lib/cat*.jar"/>
<include name="lib/extensions/cat*.jar"/>
<include name="lib/cas*.jar"/>
<include name="bld/internal-lib/cat*.jar"/>
<exclude name="**/*test.jar"/>
<exclude name="**/*.war"/>
</patternset>
<fileset id="fileset.instrument" dir="${uninstrumented.jars.folder}">
<patternset refid="pattern.files.to.instrument"/>
</fileset>
英文:
I am migrating ant based project to gradle project. I have to use a set of files or pattern in many tasks which makes me write duplicate code while it can be simply kept in resources and patternset tags in ant. Is there any simpler way like equivalent to resources or patternset in gradle.
<patternset id="pattern.files.to.instrument">
<include name="lib/cat*.jar"/>
<include name="lib/extensions/cat*.jar"/>
<include name="lib/cas*.jar"/>
<include name="bld/internal-lib/cat*.jar"/>
<exclude name="**/*test.jar"/>
<exclude name="**/*.war"/>
</patternset>
<fileset id="fileset.instrument" dir="${uninstrumented.jars.folder}">
<patternset refid="pattern.files.to.instrument"/>
</fileset>
答案1
得分: 2
以下是翻译好的内容:
我对于 Ant 不太熟悉,但我对于 Gradle 中类似概念的第一想法是 FileTree
(或 ConfigurableFileTree
)。您可以配置一个 FileTree
一次,然后(由于它实现了 FileCollection
接口)可以在任何需要使用符合您指定的模式的文件的地方引用它:
def filesToInstrument = fileTree(projectDir) {
include 'lib/cat*.jar'
include 'lib/extensions/cat*.jar'
include 'lib/cas*.jar'
include 'bld/internal-lib/cat*.jar'
exclude '**/*test.jar'
exclude '**/*.war'
}
task copyFilesToInstrument(type: Copy) {
from filesToInstrument
into 'my/destination/path'
}
然而,据我所知,FileTree
元素总是绑定到一个根目录,所以如果您只想要重用包含/排除模式,您可以查看 CopySpec
。类型为 CopySpec
的元素可以使用实现了 CopySpec
接口的任务上的 with
方法来重用(例如 Copy
、Zip
、Jar
等):
def filesToInstrumentPattern = copySpec {
include 'lib/cat*.jar'
include 'lib/extensions/cat*.jar'
include 'lib/cas*.jar'
include 'bld/internal-lib/cat*.jar'
exclude '**/*test.jar'
exclude '**/*.war'
}
task zipFilesToInstrument(type: Zip) {
from fileTree('src')
with filesToInstrumentPattern
}
英文:
I'm not familiar with Ant, but my first thought on a similar concept in Gradle is a FileTree
(or a ConfigurableFileTree
). You may configure a FileTree
once and (since it implements FileCollection
) refer to it everywhere where you want to use the files matching the patterns you specified:
def filesToInstrument = fileTree(projectDir) {
include 'lib/cat*.jar'
include 'lib/extensions/cat*.jar'
include 'lib/cas*.jar'
include 'bld/internal-lib/cat*.jar'
exclude '**/*test.jar'
exclude '**/*.war'
}
task copyFilesToInstrument(type: Copy) {
from filesToInstrument
into 'my/destination/path'
}
However, as far as I know, FileTree
elements are always bound to a root directory, so if you just want to reuse include/exclude patterns, you may take a look at CopySpec
. Elements of type CopySpec
may be reused using the method with
on tasks that implement CopySpec
theirselves (e.g. Copy
, Zip
, Jar
...):
def filesToInstrumentPattern = copySpec {
include 'lib/cat*.jar'
include 'lib/extensions/cat*.jar'
include 'lib/cas*.jar'
include 'bld/internal-lib/cat*.jar'
exclude '**/*test.jar'
exclude '**/*.war'
}
task zipFilesToInstrument(type: Zip) {
from fileTree('src')
with filesToInstrumentPattern
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论