如何在Gradle中创建文件集合的集合?

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

How to create a collection of file collections in gradle?

问题

我正在将一个蚂蚁项目迁移到 Gradle 项目。我有一组文件集合,这些文件将作为多个任务的输入。在蚂蚁中可以使用<union>来实现这一点。

&lt;zipfileset id=&quot;zipfileset1&quot; src=&quot;C:/reporter/servlet/reporter.war&quot;&gt;
    &lt;include name=&quot;WEB-INF/classes/**/*.class&quot;/&gt;
    &lt;exclude name=&quot;WEB-INF/classes/**/*_jsp*.class&quot;/&gt;
&lt;/zipfileset&gt;
&lt;fileset id=&quot;fileset1&quot; dir=&quot;C:/lib&quot;&gt;
	&lt;include name=&quot;test*.jar&quot;/&gt;
&lt;/fileset&gt;
&lt;union id=&quot;resources.reports.lib&quot;&gt;
    &lt;fileset refid=&quot;fileset1&quot;/&gt;
    &lt;fileset id=&quot;fileset2&quot; dir=&quot;C:/adaptors/lib&quot;&gt;
		&lt;include name=&quot;com*.jar&quot;/&gt;
	&lt;/fileset&gt;
    &lt;zipfileset refid=&quot;zipfileset1&quot;/&gt;
&lt;/union&gt;

在 Gradle 中是否有与蚂蚁中的<union>等效的方法呢?

英文:

I am working on moving an ant project to gradle project. I have a set of file collection where to be used in as input for several tasks. This is possible in ant with <union>.

&lt;zipfileset id=&quot;zipfileset1&quot; src=&quot;C:/reporter/servlet/reporter.war&quot;&gt;
    &lt;include name=&quot;WEB-INF/classes/**/*.class&quot;/&gt;
    &lt;exclude name=&quot;WEB-INF/classes/**/*_jsp*.class&quot;/&gt;
&lt;/zipfileset&gt;
&lt;fileset id=&quot;fileset1&quot; dir=&quot;C:/lib&quot;&gt;
	&lt;include name=&quot;test*.jar&quot;/&gt;
&lt;/fileset&gt;
&lt;union id=&quot;resources.reports.lib&quot;&gt;
    &lt;fileset refid=&quot;fileset1&quot;/&gt;
    &lt;fileset id=&quot;fileset2&quot; dir=&quot;C:/adaptors/lib&quot;&gt;
		&lt;include name=&quot;com*.jar&quot;/&gt;
	&lt;/fileset&gt;
    &lt;zipfileset refid=&quot;zipfileset1&quot;/&gt;
&lt;/union&gt;

Is there any method in gradle equivalent to "union" in ant.

答案1

得分: 3

这里有一个工作示例链接

注意,Gradle与Ant有紧密集成(见此处),因此一个选项是从Ant代码直接迁移。例如:

// 注意:这里使用本地目录而不是Windows路径(例如C:\lib)

ant.zipfileset(id: "zipfileset1", src: "c_reporter/servlet/reporter.war") {
    include(name: "WEB-INF/classes/**/*.class")
    exclude(name: "WEB-INF/classes/**/*_jsp*.class")
}
ant.fileset(id: "fileset1", dir: "c_lib") {
    include(name: "test*.jar")
}
ant.union(id: "resources.reports.lib") {
    fileset(refid: "fileset1")
    fileset(id: "fileset2", dir: "c_adaptors/lib") {
        include(name: "com*.jar")
    }
    zipfileset(refid: "zipfileset1")
}

根据评论,另一种替代且更符合Gradle风格的方法是:(在上面的链接中也有示例)

project.ext.zipFiles1 = 
zipTree("${projectDir}/c_reporter/servlet/reporter.war").matching {
    include 'WEB-INF/classes/**/*.class'
    exclude 'WEB-INF/classes/**/*_jsp.class'
}

project.ext.files1 = 
fileTree("${projectDir}/c_lib").matching {
    include 'test*.jar'
}

project.ext.files2 = 
fileTree("${projectDir}/c_adaptors/lib").matching {
    include 'com*.jar'
}

project.ext.allFiles = 
project.ext.zipFiles1.plus(project.ext.files1)
                     .plus(project.ext.files2)
英文:

There is a working example here

Note that Gradle has tight integration with Ant (see here) so one option is a straight migration from the Ant code. For example:

// NOTE: this uses local dirs instead of Windows paths (e.g. C:\lib)

ant.zipfileset(id:&quot;zipfileset1&quot;, src:&quot;c_reporter/servlet/reporter.war&quot;) {
    include(name:&quot;WEB-INF/classes/**/*.class&quot;)
    exclude(name:&quot;WEB-INF/classes/**/*_jsp*.class&quot;)
}   
ant.fileset(id:&quot;fileset1&quot;, dir:&quot;c_lib&quot;) {
    include(name:&quot;test*.jar&quot;)
}   
ant.union(id:&quot;resources.reports.lib&quot;) {
    fileset(refid:&quot;fileset1&quot;)
    fileset(id:&quot;fileset2&quot;, dir:&quot;c_adaptors/lib&quot;) {
        include(name:&quot;com*.jar&quot;)
    }   
    zipfileset(refid:&quot;zipfileset1&quot;)
}   

Per comment, an alternative -- and more Gradle-esque approach -- is: (also illustrated in the link above)

project.ext.zipFiles1 = 
zipTree(&quot;${projectDir}/c_reporter/servlet/reporter.war&quot;).matching {
    include &#39;WEB-INF/classes/**/*.class&#39;
    exclude &#39;WEB-INF/classes/**/*_jsp.class&#39;
}

project.ext.files1 = 
fileTree(&quot;${projectDir}/c_lib&quot;).matching {
    include &#39;test*.jar&#39;
}

project.ext.files2 = 
fileTree(&quot;${projectDir}/c_adaptors/lib&quot;).matching {
    include &#39;com*.jar&#39;
}

project.ext.allFiles = 
project.ext.zipFiles1.plus(project.ext.files1)
                     .plus(project.ext.files2)

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

发表评论

匿名网友

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

确定