Getting file name inside Ant copy task filter

Deadly 提交于 2019-12-01 09:04:12

问题


Is it possible to get the current file name being copied inside an Ant copy task? I am trying to run a beanshell script and would like access to the current file name:

<target>
    <mkdir dir="${project.build.directory}/generated-sources"/>
    <copy todir="${project.build.directory}/generated-sources"
          includeemptydirs="true" failonerror="true" verbose="true">
        <fileset dir="${project.build.sourceDirectory}"/>
        <filterchain>
            <tokenfilter>
                <filetokenizer/>
                <scriptfilter language="beanshell" byline="true"><![CDATA[
                    import java.io.BufferedReader;
                    import java.io.StringReader;
                    int count = 1;
                    BufferedReader br = new BufferedReader(new StringReader(self.getToken()));
                    StringBuilder builder = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        builder.append(line.replace("\"__LINE__\"", Integer.toString(count))).append('\n');
                        count++;
                    }
                    self.setToken(builder.toString());
                ]]></scriptfilter>
            </tokenfilter>
        </filterchain>
    </copy>
</target>

回答1:


This has been bugging me for a while - I hoped there would be a nice way to do this, but I've not found it yet.

I've had a look at the Ant source code for the 'copy' task. The actual copy is done in the ResourceUtils class, but the names of the source and destination files are not exposed in a way that would make them accessible from the filterchain. Similarly, the iteration over the fileset takes place in the copy taskdef where the 'current' file names are not held in public variables.

The least-bad option I've come up with is to use an ant-contrib 'for' task to iterate over the fileset and copy each file one by one. As you iterate, the names of the files are then available in the property specified in the 'param' attribute:

<for param="file.name">
  <path>
    <fileset dir="${project.build.sourceDirectory}"/>
  </path>
  <sequential>
    <local name="file.name"/>
    <property name="file.name" value="@{file.name}"/>
    <copy file="${file.name}" ... >
      ...
      <filterchain>
        <scriptfilter ...>
          ...
          current_file = project.getProperty( "file.name" );
          ...
        </scriptfilter>
      </filterchain>
      ...
    </copy>
  </sequential>
</for>


来源:https://stackoverflow.com/questions/4376795/getting-file-name-inside-ant-copy-task-filter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!