Ant的``和``在Gradle中分别对应什么?

huangapple go评论66阅读模式
英文:

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.

&lt;patternset id=&quot;pattern.files.to.instrument&quot;&gt;
    &lt;include name=&quot;lib/cat*.jar&quot;/&gt;
    &lt;include name=&quot;lib/extensions/cat*.jar&quot;/&gt;
    &lt;include name=&quot;lib/cas*.jar&quot;/&gt;
    &lt;include name=&quot;bld/internal-lib/cat*.jar&quot;/&gt;
    &lt;exclude name=&quot;**/*test.jar&quot;/&gt;
    &lt;exclude name=&quot;**/*.war&quot;/&gt;
&lt;/patternset&gt;

&lt;fileset id=&quot;fileset.instrument&quot; dir=&quot;${uninstrumented.jars.folder}&quot;&gt;
    &lt;patternset refid=&quot;pattern.files.to.instrument&quot;/&gt;
&lt;/fileset&gt;

答案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 方法来重用(例如 CopyZipJar 等):

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 &#39;lib/cat*.jar&#39;
    include &#39;lib/extensions/cat*.jar&#39;
    include &#39;lib/cas*.jar&#39;
    include &#39;bld/internal-lib/cat*.jar&#39;
    exclude &#39;**/*test.jar&#39;
    exclude &#39;**/*.war&#39;
}

task copyFilesToInstrument(type: Copy) {
    from filesToInstrument
    into &#39;my/destination/path&#39;
}

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 &#39;lib/cat*.jar&#39;
    include &#39;lib/extensions/cat*.jar&#39;
    include &#39;lib/cas*.jar&#39;
    include &#39;bld/internal-lib/cat*.jar&#39;
    exclude &#39;**/*test.jar&#39;
    exclude &#39;**/*.war&#39;
}

task zipFilesToInstrument(type: Zip) {
    from fileTree(&#39;src&#39;)
    with filesToInstrumentPattern
}

huangapple
  • 本文由 发表于 2020年9月25日 05:22:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64054599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定