How to append string in js file using ant build

时间:2017-01-30 14:22:25

标签: javascript ant

I want to append data to a js file at specific location using ant build

Here is the js file

fun1= function(){
    var data="data1";
}

I want to append one line only if this does not exist in file

data =data+"data2";

inside the fun1. Is there any way to do so as xmltask is specific to XML files only?

2 个答案:

答案 0 :(得分:1)

我会使用代币替换。在JS中将您的字符串格式化为:

fun1= function(){
    var data="data1 @@@@";
}

然后在蚂蚁中你可以说:

<replace file="script.js" token="@@@@" value="data2"/>

它会将符号@@@@替换为值data2

请注意,替换是在适当的位置完成的,所以不要在原始源代码上执行,而是在编译版本上执行,或者至少在副本上执行。否则你只能进行一次替换。

答案 1 :(得分:0)

我已经使用条件

解决了这个问题
<condition property="pluginEntryExists">
    <resourcecontains resource="js_file_location" substring="data =data+'data2';"/>
</condition>

并且基于属性设置目标将被执行

<target name="append-data" unless="${pluginEntryExists}">
    <!-- to support propertyregex include antcontrib.jar -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="./lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <loadfile property="configFileData" srcFile="js_file_location" />
    <propertyregex property="pluginList" input="${configFileData}" regexp='var data=".*;' select="\0" />

    <replaceregexp file="js_file_location" match='var data=".*;' replace="${pluginList}data+='data2';" />
</target>