【问题标题】:Gradle task with XSL XML template generation使用 XSL XML 模板生成的 Gradle 任务
【发布时间】:2014-03-04 00:53:31
【问题描述】:

Java 编译在 ANT 和 Gradle 中运行良好。我只是想找到在 Gradle 中编写代码以生成 xml 文件的方法,ANT 正在做什么(请参见下文)。

文件夹结构

src/java -- 包含 java src 代码 src/xml -- 包含文件夹: 模式——包含一个 xxx.properties 文件和一堆 .xml 和 .template 文件 schema -- 包含一个 .xsd 文件 样式表——包含 3 个 .xsl 文件

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 页脚本。
谢谢。

【问题讨论】:

    标签: xml file xsd gradle task


    【解决方案1】:

    最终解决方案:(稍作调整,我没有使用 src/xml/patterns,而是创建了一个 tmp/template 位置来执行所有操作)..

       //---
       // 1. Transform the template patterns
       copy {
          into "${buildDir}/tmp/template"
          from "src"
          include "xml/**"
       }
       ant {
          xslt(
             basedir: "${buildDir}/tmp/template/xml/patterns",
             destdir: "${buildDir}/tmp/template/xml/patterns",
             extension: ".xml",
             force: "true",
             style: "${buildDir}/tmp/template/xml/stylesheets/SearchTemplate.xsl",
             includes: "*.template"
          )
       }
    
       // 2. Create the pattern definition file by concatenating all parts
       def outFile = new File ( "${buildDir}/tmp/template", "Patterns.xml" )
       def props = new Properties()
       new File("${buildDir}/tmp/template/xml/patterns/_patternlist.properties").withInputStream {
         stream -> props.load(stream)
       }
       outFile.text = props["pattern.xml.header"].toString()
       props["pattern.filelist"].split(',').each {
          String s1 = "${buildDir}/tmp/template/xml/patterns/" + it
          String s = new File (s1).text
          outFile.append(s)
       }
       outFile.append(props["pattern.xml.trailer"].toString())
    
       // 3. Copy .xsd file
       copy {
          into "${buildDir}/tmp/template"
          from "src/xml/schema"
          include "Patterns.xsd"
       }
    
       // 4. Validate schema
       ant {
          schemavalidate( file: "${buildDir}/tmp/template/Patterns.xml" ) {
              schema( file: "${buildDir}/tmp/template/xml/schema/Patterns.xsd",
                      namespace: "http://www.w3.org/2001/XMLSchema-instance" )
          }
       }
    

    【讨论】:

      【解决方案2】:

      好的。我已经到了这一点,我现在可以使用以下命令读取这些变量的多行值:

      def props = new Properties()
      new File("pattern.properties").withInputStream {
        stream -> props.load(stream)
      }
      // accessing the property from Properties object using Groovy's map notation
      println "pattern.xml.header=" + props["pattern.xml.header"]
      println "pattern.filelist=" + props["pattern.filelist"]
      println "pattern.xml.trailer=" + props["pattern.xml.trailer"]
      

      这输出为:

      -bash-3.2$ /production/gradle/gradle-1.6/bin/gradle -b build.gradle

      pattern.xml.header=<?xml version="1.0"?><patternFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Patterns.xsd">
      pattern.filelist=1.xml,2-a.xml,3-iii.xml,4_dddd.xml,File6.xml
      pattern.xml.trailer=</patternFile>
      

      PS: pattern.properties 文件的上述输出与我在上面的帖子中提到的内容不同(我在本地机器上玩代码,不想使用真实内容).. 但代码适用于任何 pattern.properties 文件。

      现在,我需要做的就是最后一个文件创建部分,这将非常容易......即使用 foreach 循环并写入每个文件的内容(使用第二个参数)并连接 param1+param2(每个文件内容)+param3 就可以了。

      顺便说一句,我会在我有最终解决方案时发布..仍然,如果您知道更有效的方法,请告知。

      【讨论】:

        猜你喜欢
        • 2017-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 2014-07-16
        • 2014-10-10
        • 1970-01-01
        相关资源
        最近更新 更多