Есть ли общий способ доступа к результирующему набору файлов задачи ANT?

Пытаюсь написать замену интегрированной задачи <zip>, чтобы она поддерживала пароли с использованием <exec> и 7za.exe. Идея состоит в том, чтобы заменить задачу <zip>.

Сложность в том, что <zip> поддерживает так много способов объявления файлов для включения / исключения, например:

  • includes
  • includesfile
  • excludes
  • excludesfile
  • defaultexcludes
  • и вложенные <fileset> объявления

Есть ли способ использовать результат этих fileset инструкций внутри задачи exec?


person ViToni    schedule 03.09.2015    source источник


Ответы (2)


Недокументированное свойство ${toString:filesetid} содержит все файлы, разделитель по умолчанию - ';'.
Для преобразования разделителя используйте задача преобразования пути, результирующее свойство будет содержать файлы с выбранным разделителем, например :

<fileset dir="C:/diff1" includes="**/*.html" id="diff">
 <different targetdir="C:/diff2"
   ignoreFileTimes="true"/>
</fileset>

<!-- one file one line -->
<pathconvert refid="diff" pathsep="${line.separator}" property="htmldiff"/>

<!-- blank as separator -->
<pathconvert refid="diff" pathsep=" " property="htmldiff"/>  

<echo file="C:/diff1/htmldiff.txt">${htmldiff}</echo>

для использования в exec со строкой arg, пробел в качестве разделителя кажется подходящим.

person Rebse    schedule 03.09.2015
comment
Спасибо за ответ. Мое решение - что-то в этом роде. - person ViToni; 04.09.2015

Основываясь на ответе, я смог придумать решение, которое работает аналогично интегрированной задаче <zip>. Использование <pathconvert> и одинарных кавычек помогает:

<macrodef name="sevenzip" description="Command line interface for 7zip">
    <attribute name="level"  default="5"/> <!-- will be ignored for now, just to make it compatible with normal ZIP task -->
    <attribute name="basedir"/>
    <attribute name="excludes" default=""/>
    <attribute name="includes" default="**/**"/>
    <attribute name="destfile"/>
    <attribute name="password" default=""/>

    <sequential>
        <description>7-Zip integration</description>
        <local name="passArg" />
        <if>
            <equals arg1="@{password}" arg2=""/>
            <then>
                <property name="passArg" value="" />
            </then>
            <else>
                <!-- p<SECRET> = set password of archive -->
                <property name="passArg" value='"-p@{password}"' />
            </else>
        </if>

        <fileset id="mask" dir="@{basedir}">
            <include name="@{includes}" />
            <exclude name="@{excludes}" />
        </fileset>

        <local name="mask" />
        <pathconvert property="mask" refid="mask" pathsep="' '" />
        <!-- the single quotes help to wrap file names containing spaces -->

        <exec executable="${7zip.cmd}" failonerror="true">
            <!-- command -->
            <arg value="a"/>                <!-- a : add files to archive -->
            <!-- arg value="u"/ -->             <!-- u : update files to archive -->

            <!-- switches -->
            <arg value="-bd"/>              <!-- -bd : disable percentage indicator -->
            <arg value="-tzip"/>            <!-- -t<type> : set type of archive -->             

            <arg value="${passArg}"/>       

            <arg value="--"/>               <!-- : Stop switches parsing -->

            <!-- archive file -->
            <arg value="@{destfile}"/>

            <!-- file names / wildcards -->
            <arg line="'${mask}'"/>
            <!-- 
                the single quotes are the outter wrapper for the single quotes
                from pathconvert, the line attribute add the result to the arguments
                but NOT as a single escaped string
            -->

        </exec>
    </sequential>
</macrodef>
person ViToni    schedule 04.09.2015