简体   繁体   中英

Is there an ant task that can copy without losing permissions

I'm aware that I can <exec executable="cp" failonerror="true"> , however, I'd really rather have a task that I can call from any OS that can use all (or at least most) of the attributes to copy , but which doesn't suck away the permissions on unix.

I am wondering if there is already a solution out there, or will I have to write my own copy2 .

As I was kind of afraid, there's nothing "off the shelf". We have this code, but it only handles directory to directory copy or file to file copy, has custom attributes, and doesn't do any of the other neat things copy does.

<!-- ==================================================================== -->
<!-- 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>

I wrote this one too.

<!-- ==================================================================== -->
<!-- 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>

I'm not aware of one. As mentioned in the Ant documentation, this is because there was no mechanism to manipulate file permissions in the JRE. Ant Copy task

Java 7 brings much better support for this sort of thing, so perhaps in a future version of Ant this will be possible. This will likely take a very long time, as I think Ant is only moving to Java 5 for version 1.9.

I guess you might be able to simulate the behavior you are looking for by conditionally running OS specific commands based on the OS?

It's not ideal, but there is the "chmod" task that you could use to set the permissions on each file after a "copy" or "copydir":

http://ant.apache.org/manual/Tasks/chmod.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM