【发布时间】:2014-03-04 00:53:31
【问题描述】:
Java 编译在 ANT 和 Gradle 中运行良好。我只是想找到在 Gradle 中编写代码以生成 xml 文件的方法,ANT 正在做什么(请参见下文)。
文件夹结构:
pattern.properties 文件的内容是:
# xml header information and root element for the document
pattern.xml.header=<?xml version="1.0"?>\
<patternFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Patterns.xsd">
# Patterns in order to load.
pattern.filelist=\
GlobalPoints-Dashboard.xml,\
IVCompatibility.xml,\
IVCompatibilityAgent.xml,\
IVCompatibilityProduct.xml,\
TVServicesClassSearch.xml,\
TVServicesSearchMartindaleIndexNom.xml
# end element for the file
pattern.xml.trailer=</patternFile>
生成我需要的最终 Patterns.xml 文件的 ANT 代码是:
pattern.src.dir = src/xml/patterns
pattern.schema.dir = src/xml/schema
pattern.xml.filename = Patterns.xml
pattern.xsd.filename = Patterns.xsd
pattern.src.xsd = src/xml/schema/Patterns.xsd
pattern.output.dir = 温度
pattern.output.file = temp/Patterns.xml
pattern.properties = pattern.properties
<!-- transform the template patterns -->
<target name="templates" >
<xslt basedir="src/xml/patterns"
destdir="src/xml/patterns"
extension=".xml"
force="true"
style="src/xml/stylesheets/SearchTemplate.xsl"
includes="FOX-Tool-Active-Ingredient.template,
FOX-Tool-Product.template,
FOX-Tool-Product-Material.template">
</xslt>
</target>
<!-- create the pattern definition file by concatenating all parts -->
<target name="build-xml" depends="init,templates">
<loadproperties srcFile="${pattern.src.dir}/${pattern.properties}" />
<!-- make sure all files exist -->
<resourcecount property="fileCount">
<fileset dir="${pattern.src.dir}" includes="${pattern.filelist}" />
</resourcecount>
<fail message="Files are missing.">
<condition>
<not>
<resourcecount count="${fileCount}">
<filelist dir="${pattern.src.dir}" files="${pattern.filelist}"/>
</resourcecount>
</not>
</condition>
</fail>
<concat destfile="${pattern.output.file}">${pattern.xml.header}</concat>
<concat append="yes" destfile="${pattern.output.file}">
<filelist dir="${pattern.src.dir}" files="${pattern.filelist}"/>
</concat>
<concat append="yes" destfile="${pattern.output.file}">${pattern.xml.trailer}</concat>
<!-- copy the schema to the output directory for validation -->
<copy file="${pattern.src.xsd}"
todir="${pattern.output.dir}"
overwrite="yes"/>
<!-- validate that the combined file matches the schema -->
<schemavalidate file="${pattern.output.file}">
<schema file="${pattern.src.xsd}"
namespace="http://www.w3.org/2001/XMLSchema-instance"/>
</schemavalidate>
</target>
使用以下 Gradle 代码,我可以执行“模板”目标在 ANT 中所做的第一部分。
// Perform XSL transformations in work directory as similar to ANT "templates" target
def performXslt() {
ant {
xslt(
basedir: "${pattern.src.dir}",
destdir: "${pattern.src.dir}",
extension: ".xml",
force: "true",
style: "src/xml/stylesheets/SearchTemplate.xsl",
includes: "*.template"
)
}
}
我可以将上述任务称为我将编写的另一个 Gradle 自定义任务的 dependsOn 或 doFirst。对于与 ANT 中的“build-xml”目标等效的第二个任务,我正在尝试与大家核实 - 编写它的有效方法是什么。
正如我在 ANT 中看到的“build-xml”目标,它所做的只是首先调用“模板”目标,然后读取具有 3 个变量的 pattern.properties 文件,1)标题字符串。 2) .xml 文件列表和 3) 尾迹/脚注字符串,它所做的就是连接那些变量/字符串的值 1(string) + (所有 .xml 文件内容的内容,如第二个变量中提到的) + 3 (字符串)到最终的 Patterns.xml 文件中。最后,它会在同一个输出文件夹中复制另一个 .xsd 文件,并在最终的 Patterns.xml 文件上使用该文件运行一些架构验证。
有人可以帮我在 Gradle 中编写上述“build-xml”ANT 目标登录作为“buildXML”任务,这样我就不会最终编写 2 页脚本。
谢谢。
【问题讨论】: