使用 ANT 替换整个 XML 标签 [英] Replacing an entire XML tag using ANT

查看:27
本文介绍了使用 ANT 替换整个 XML 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PhoneGap Build 提供一项服务(目前在测试期间免费),他们可以在其中获取您的 HTML+JS+CSS 源代码,以及用于配置构建选项 (config.xml) 的 XML 文件,并为所有支持的平台(iOS、Android、Blackberry、Symbian、WebOS)进行服务器端构建,这可以节省大量时间并为我节省大量资金关于头痛药.

PhoneGap Build provides a service (currently free during beta) where they take your HTML+JS+CSS source, along with an XML file to configure build options (config.xml), and do server-side builds for all supported platforms (iOS, Android, Blackberry, Symbian, WebOS), which is a huge time saver and saves me tons of money on headache medicine.

不幸的是,外部网站链接的工作方式存在不一致的行为:

目前为了在外部浏览器中打开链接,需要上传不同的代码到PhoneGap Build.对于 iOS,域必须列入白名单,对于 Android,域必须不列入名单.

Currently in order to have links open in an external browser, it requires uploading different code to PhoneGap Build. For iOS the domain must be whitelisted, and for Android the domain must be unlisted.

您可以通过向访问标记添加一个属性来使其保持一致,例如 includeForAndroid=false",它不会包含 Android 上的访问标记内容,以便链接可以在外部浏览器中打开,就像在 iOS 上一样.

You could make it consistent by adding an attribute to the access tag, like includeForAndroid="false", which would NOT include the access tag contents on Android, so that links would open in an external browser, just like on iOS.

Config.xml 文档;这是相关博文.

虽然员工提出了修复建议,但尚未在产品中制作和发布:

While there is a proposed fix by an employee, it has yet to be made and released in the product:

我们希望在 Cordova 开源项目中解决这个问题:同时,对于 PhoneGap Build,我正在考虑这样的事情:

We're hoping to get this straightened out in Cordova open source project: in the meantime, for PhoneGap Build, I'm thinking something like this:

<access origin="*" onlyInBrowser="true" />

Android 上不会包含标签,但 iOS 上会.

That will not include the tag on Android, but will on iOS.

在进行此更改并在产品中可用之前,解决方法是制作我将上传的两个 zip 文件:一个用于 Android(没有访问标签),另一个用于 iOS(带有访问标签).不理想,但这是一致的应用行为所必须做的.

Until this change is made and available in the product, the work-around is to make two zip files that I will upload: one for Android (without the access tags) and one for iOS (with the access tags). Not ideal, but it's what must be done for consistent app behavior.

我已经在使用 ANT 来自动执行该项目中的许多任务,因此如果 ANT 也可以为我执行 config.xml 更新,那就太理想了.

I'm already using ANT to automate many tasks within this project, so it would be ideal if ANT could do the config.xml updates for me too.

作为参考,这是我构建两个 zip 的 ANT 代码:

For reference, here's my ANT code that would build the two zips:

<target name="BUILD-ZIP" depends="verify-using-minified-js,prepare-for-build">

    <tstamp>
        <format property="build.tstamp" pattern="yyyy-MM-dd__HH-mm-ss" />
    </tstamp>
    
    <antcall target="zip-ios">
        <param name="tstamp" value="${build.tstamp}" />
    </antcall>
    
    <!-- build android second so that we can just remove the access tags -->
    <antcall target="zip-android">
        <param name="tstamp" value="${build.tstamp}" />
    </antcall>
        
</target>

<target name="zip-ios">
    <zip destfile="${dir.pkg.phonegap}${tstamp}-apple.zip">
        <zipfileset dir="${dir.pkg.tmp}">
            <exclude name="build.xml" />
            <exclude name="build.properties" />
            <exclude name="settings.xml" />
            <exclude name=".project" />
            <exclude name="**/*.psd" />
            <exclude name="assets/js/app.js" />
            <exclude name="assets/js/cordova-1.5.0.js" />
        </zipfileset>
    </zip>
</target>

<target name="zip-android">
    
    <!-- before building android zip, get rid of the <access> tags -->
    
    <zip destfile="${dir.pkg.phonegap}${tstamp}-android.zip">
        <zipfileset dir="${dir.pkg.tmp}">
            <exclude name="build.xml" />
            <exclude name="build.properties" />
            <exclude name="settings.xml" />
            <exclude name=".project" />
            <exclude name="**/*.psd" />
            <exclude name="assets/js/app.js" />
            <exclude name="assets/js/cordova-1.5.0.js" />
        </zipfileset>
    </zip>
</target>
    
<target name="prepare-for-build">
    
    <!-- increment build number -->
    <propertyfile file="build.properties">
        <entry key="version.build.number" type="int" operation="+" default="1"/>
    </propertyfile>
    <property file="build.properties"/>
    <echo message="BUILD NUMBER: ${version.build.number}"/>
    
    <delete includeemptydirs="true" verbose="false">
        <fileset dir="${dir.pkg.tmp}" includes="**/*"/>
    </delete>
    <filter token="BUILD_NUMBER" value="${version.build.number}" />
    <filter token="VERSION_MAJOR" value="${version.major}" />
    <filter token="VERSION_MINOR" value="${version.minor}" />
    <copy todir="${dir.pkg.tmp}" verbose="true" overwrite="true" filtering="true">
        <fileset dir="${dir.dev}" includes="**/*" />
    </copy>
    
</target>

zip-android 目标中有一条注释:

There's a comment in there, in the zip-android target:

<!-- before building android zip, get rid of the <access> tags -->

这是我想要更换的地方.我已经尝试使用 任务以及 ,但我不能似乎找到了一种方法来做到这一点,这一切都归结为我需要替换字符串的事实:<access .../> 并且这些替换方法似乎都不允许<> 在令牌/匹配/等属性中.我试过使用 CDATA,但也无法正常工作.

This is where I want to do the replacement. I've tried using the <filter> task as well as <replace> and <replaceRegExp>, but I can't seem to find a way to do it, and it all boils down to the fact that I need to replace the string: <access ... /> and none of these replacement methods seem to allow < or > in the token/match/etc attributes. I've tried using CDATA, but couldn't get that working either.

最接近我来的,因为 ANT 不会抛出任何错误,是这样的:

The closest I've come, in that ANT doesn't throw any errors, is this:

<replace file="${dir.pkg.tmp}config.xml" failOnNoReplacements="true">
    <replacetoken>
        <![CDATA[<access origin="http://example.com" subdomains="true" />]]>
    </replacetoken>
</replace>

理论上,这会将指定的 <access .../> 标记替换为空字符串,因为我省略了 value 属性.

In theory, this would replace the specified <access ... /> tag with an empty string, because I've omitted the value attribute.

不幸的是,它由于 failOnNoReplacements 属性而失败,并且无论出于何种原因,它都没有进行任何替换.

Unfortunately, it fails because of the failOnNoReplacements attribute and the fact that, for whatever reason, it's not doing any replacements.

这可能吗?如果是这样,我做错了什么?!

Is this possible? If so, what am I doing wrong?!

推荐答案

哈,我设法组合了一个解决方案来绕过尖括号的阻塞.

Ha, I managed to put together a solution that gets around the blocking of angle brackets.

在 config.xml 中:

In config.xml:

<!--access_tag1_here-->

在 build.xml 中:

In build.xml:

<replace 
    file="${dir.pkg.tmp}config.xml" 
    failOnNoReplacements="true"
    token="!--access_tag1_here--" 
    value="access origin='http://example.com' subdomains='true' /"
    />

这会将评论转换为必要的访问标记.

This transforms the comment into the necessary access tag.

我改变了我的 zip 顺序,以便 Android 现在先运行,而评论仍然是评论,然后 iOS zip 进程插入访问标签.

I switched my zip order so that Android now goes first while the comments are still comments, and then the iOS zip process inserts the access tags.

这篇关于使用 ANT 替换整个 XML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆