我知道我可以<exec executable="cp" failonerror="true">,但是,我确实希望有一个任务,我可以从可以使用copy的所有(或至少大多数)属性的任何操作系统中调用该任务,但是不会吸走Unix上的权限。

我想知道是否已经有解决方案,还是我必须编写自己的copy2

令我有些恐惧的是,没有什么“现成的”。我们有这段代码,但是它仅处理目录到目录副本或文件到文件副本,具有自定义属性,不会执行copy所做的任何其他整洁的事情。

<!-- ==================================================================== -->
<!-- Copy files from A to B                                               -->
<!-- <copy> would do this job, if it weren't such a useless pile of fail  -->
<!-- and could manage to preserve execute bits on Linux                   -->
<!-- ==================================================================== -->
<macrodef name="internal-copydir">
    <attribute name="fromdir" default="NOT SET" />
    <attribute name="todir" default="NOT SET" />
    <sequential>
        <if>
            <os family="windows" />
            <then>
                <copy todir="@{todir}">
                    <fileset dir="@{fromdir}" />
                </copy>
            </then>
            <else>
                <exec executable="rsync" failonerror="true">
                    <arg value="-a" />
                    <arg value="@{fromdir}/" />
                    <arg value="@{todir}/" />
                </exec>
            </else>
        </if>
    </sequential>
</macrodef>

<!-- ==================================================================== -->
<!-- Copy file from A to B                                                -->
<!-- <copy> would do this job, if it weren't such a useless pile of fail  -->
<!-- and could manage to preserve execute bits on Linux                   -->
<!-- ==================================================================== -->
<macrodef name="internal-copyfile">
    <attribute name="file" default="NOT SET" />
    <attribute name="tofile" default="NOT SET" />
    <sequential>
        <if>
            <os family="windows" />
            <then>
                <copy file="@{file}" tofile="@{tofile}"/>
            </then>
            <else>
                <exec executable="cp" failonerror="true">
                    <arg value="@{file}" />
                    <arg value="@{tofile}" />
                </exec>
            </else>
        </if>
    </sequential>
</macrodef>


我也写了这个。

<!-- ==================================================================== -->
<!-- Copy file to a directory                                             -->
<!-- <copy> would do this job, if it weren't such a useless pile of fail  -->
<!-- and could manage to preserve execute bits on Linux                   -->
<!-- ==================================================================== -->
<macrodef name="internal-copyfiletodir">
    <attribute name="file" default="NOT SET" />
    <attribute name="todir" default="NOT SET" />
    <sequential>
        <if>
            <os family="windows" />
            <then>
                <copy file="@{file}" todir="@{todir}"/>
            </then>
            <else>
                <exec executable="cp" failonerror="true">
                    <arg value="@{file}" />
                    <arg value="@{todir}/" />
                </exec>
            </else>
        </if>
    </sequential>
</macrodef>

最佳答案

我不知道一个。如Ant文档中所述,这是因为在JRE中没有机制来操纵文件权限。 Ant Copy task

Java 7为这种事情带来了更好的支持,因此也许在将来的Ant版本中这将成为可能。这可能会花费很长时间,因为我认为Ant仅会针对1.9版迁移到Java 5。

我猜您可以通过基于OS有条件地运行特定于OS的命令来模拟正在寻找的行为?

07-24 09:49
查看更多