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

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

问题描述

PhoneGap Build 提供一项服务(目前在Beta版中是免费的),可将您的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.

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

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目标中有一个注释:

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

这是我要进行替换的地方.我已经尝试过使用<filter>任务以及<replace><replaceRegExp>,但是我似乎找不到解决方法,所有这些都归结为我需要替换字符串的事实:<access ... />,并且这些替换方法似乎都不允许在token/match/etc属性中使用<>.我已经尝试过使用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天全站免登陆