开发者

How to create a file in distributed system using ant script?

i am working with mqfte. i have to create a empty txt file with the same name as the source file after it is transferred to the des开发者_开发技巧tination. How can do this using ant script?

Scenario:

Srcpath: \src\test.dat destpath: \dest\test.dat

After the file is moved from src path, i need to create a empty file test.dat in the src path using ant script? how can this be done?


Before you transfer the files, you could use the Touch task to create the new empty files matching those you will be transferring (reuse the same fileset) in a temporary directory. After the transfers are complete, you could use the Move task to copy the empty files into your src dir. You could use overwrite="false" in the Move task to ensure that files remaining in the src dir do not get replaced (e.g. if you wanted to be sure to have empty files only for successful transfers).

Here is an example.

<project default="test">

  <target name="test">

    <touch>
      <fileset dir="src">
        <include name="test*"/>
      </fileset>
      <mapper type="regexp" from="(.*)" to="tmp/\1"/>
    </touch>

    <move todir="dest">
      <fileset dir="src">
        <include name="test*"/>
        <!-- simulate file not transferred -->
        <exclude name="test.doc"/>
      </fileset>
      <globmapper from="test.*" to="result_*.txt"/>
    </move>


    <move todir="src" overwrite="false">
      <fileset dir="tmp"/>
    </move>

  </target>

</project>

The first move stands in for your transfer. One file (test.doc) is not moved out of the src dir (simulate failed transfer). You can test be creating files with some content in the src dir. After the target completes, the test.doc should still have its original content. The other files should be empty.

In response to your question about the regexp mapper:

<mapper type="regexp" from="(.*)" to="tmp/\1"/>

This captures the whole of the incoming file name into a group...

from="(.*)"

and prepends "tmp/" to that captured group...

to="tmp/\1"

(In regular expression \1 refers to the first captured group in the expression. Groups are defined using parentheses.)

So from src/somefile.txt, we will get somefile.txt as the input file to the mapper, and we translate this to tmp/somefile.txt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜