Gradle脚本:如何在XML文件中创建和写入数据

Ach*_*hin 3 xml groovy android gradle build.gradle

我正在创建xml,并通过使用gradle脚本并在xml文件中写入一些数据。我已经创建了.gradle文件,运行gradle文件后,我无法实现xml文件的确切格式。我在下面发布我的代码:请任何人指导我。导入groovy.xml.MarkupBuilder

task generatepublicxml {

    //def resDir = project.projectDir.absolutePath + "/src/main/res-public"

    // Create new public.xml with writer
    new File("/home/signity/Desktop/public.xml").withWriter { writer ->
        // Create MarkupBuilder with 4 space indent
        def destXml = new MarkupBuilder(new IndentPrinter(writer, "    ", true));
        def destXmlMkp = destXml.getMkp();

        // GIST NOTE: our project needed the ResourceName suppression, but its not needed in general
        destXml.resources('xmlns:tools': 'http://schemas.android.com/tools', 'tools:ignore': 'ResourceName')
                {
            // Leave file comment
            destXmlMkp.yield "\r\n"
            destXmlMkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task")

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的必需文件格式:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="remoteAccessConsumerKey">3MVG92mNMNiWvonjPDM9qqaDip0MFl9TGc</string>
    <string name="oauthRedirectURI">saleschap:///mobilesdk/detect/oauth/done</string>

    <string-array name="oauthScopes">
        <item>api</item>
        <item>web</item>
        <item>refresh_token</item>
    </string-array>

    <string name="androidPushNotificationClientId"></string>

</resources>
Run Code Online (Sandbox Code Playgroud)

Mat*_*and 6

由于我看到您是在gradle构建文件中执行此操作的,因此首先请注意gradle执行阶段。

我假设您希望在执行任务时而不是在加载构建文件时运行代码。如果是这样,则需要将代码放在任务内的doFirstdoLast块内。如果您按照问题的方式编写代码,则无论构建程序是否generatepublicxml运行,都将在加载构建文件时执行代码(即,您可以运行某些完全不同的任务,并且代码仍将运行)。

当开始使用gradle时,这通常并不明显,如果您感到困惑,我建议阅读构建生命周期中gradle文档

顺便说一句,下面的build.gradle:

import groovy.xml.*

task generatepublicxml {
  doLast { 
    file("public.xml").withWriter { writer ->
      // Create MarkupBuilder with 4 space indent
      def xml = new MarkupBuilder(new IndentPrinter(writer, "    ", true))

      xml.doubleQuotes = true
      xml.mkp.xmlDeclaration(version: '1.0', encoding: 'utf-8')

      xml.resources('xmlns:tools': 'http://schemas.android.com/tools', 'tools:ignore': 'ResourceName') {
        string(name: 'remoteAccessConsumerKey', '3MVG92mNMNiWvonjPDM9qqaDip0MFl9TGc')
        string(name: 'oauthRedirectURI',        'saleschap:///mobilesdk/detect/oauth/done')
        'string-array'(name: 'oauthScopes') {
          item('api')
          item('web')
          item('refresh_token')
        }
        string(name: 'androidPushNotificationClientId')

        // Leave file comment
        mkp.yield('\n    ')
        mkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task")

      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

运行时:

~> gradle generatepublicxml

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
Run Code Online (Sandbox Code Playgroud)

结果如下public.xml

~> gradle generatepublicxml

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
Run Code Online (Sandbox Code Playgroud)

如果您不希望元素的xmlns属性等,则resources可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName">
    <string name="remoteAccessConsumerKey">3MVG92mNMNiWvonjPDM9qqaDip0MFl9TGc</string>
    <string name="oauthRedirectURI">saleschap:///mobilesdk/detect/oauth/done</string>
    <string-array name="oauthScopes">
        <item>api</item>
        <item>web</item>
        <item>refresh_token</item>
    </string-array>
    <string name="androidPushNotificationClientId" />
    <!-- AUTO-GENERATED FILE.  DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task -->
</resources>
Run Code Online (Sandbox Code Playgroud)

结果是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   ...
Run Code Online (Sandbox Code Playgroud)