How can I use the Ant tar task and preserve file permissions?
-
19-09-2019 - |
Question
Of course it can be done using the exec task
, but my question is:
Is it possible to do it using the tar task?
Solution
I don't think there is a way to retain existing permissions, per this note from the copy task:
Unix Note: File permissions are not retained when files are copied; they end up with the default UMASK permissions instead. This is caused by the lack of any means to query or set file permissions in the current Java runtimes. If you need a permission-preserving copy function, use
<exec executable="cp" ... >
instead.
However the tar task can take one or more tarfileset
elements. The tarfileset
can be defined with a filemode
and/or dirmode
attribute to specify the unix permissions. If you specify multiple includes matching only those files to get each set of required permissions, the files in that set will be included with those permissions.
OTHER TIPS
This lack of permission makes ant tar task almost useless for me. There's no way to do it without executing the operating system tar with the exec task:
<exec executable="tar" output="/dev/null" os="Linux">
<arg value="--exclude-from=files_to_exclude.txt"/>
<arg value="-cvz"/>
<arg value="--file=${file.tar}"/>
<arg value="."/>
</exec>
There are gnu tar binaries for almost all operating systems known to man. Put one of them in your version control system and use it depending in your operating system. Yes, Ant will need to fork a process every time it is run.
Using tarfileset worked for our project. Here's a working example in case someone needs it:
<tar destfile="${dist}/${module.name}-${version}.tar">
<tarfileset dir="${package.dir}" filemode="550" includesfile="${home.dir}/includelist.txt">
<include name="*.sh"/>
</tarfileset>
</tar>
In this example, includelist.txt is used to tell which files to include in the tar file. All the files with *.sh extension will have Read and Execute permission (550) for the user and the group.
Hope this helps someone.