如何使用 ant build 在 js 文件中追加字符串

How to append string in js file using ant build

我想使用 ant build 将数据附加到特定位置的 js 文件

这是js文件

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

我只想在文件中不存在的情况下追加一行

data =data+"data2";

在有趣的地方1。有没有办法做到这一点,因为 xmltask 仅特定于 XML 个文件?

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

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

然后在 ant 中你可以说:

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

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

请注意,替换是就地完成的,所以不要在您的原始源代码上执行它,而是在编译版本或至少一个副本上执行。否则,您将只能执行一次替换。

我已经使用条件

解决了这个问题
<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="[=11=]" />

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