12

Of course it can be done using the exec task, but my question is:

Is it possible to do it using the tar task?

3 Answers 3

16

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.

15

It is impossible. 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.

5
  • Why was this downvoted? Perfectly fine solution to this issue - upvoting Nov 17, 2010 at 18:09
  • 4
    It was most likely downvoted because the answerer did not read the question: "Of course it can be done using the exec task"
    – three-cups
    Mar 30, 2011 at 21:11
  • 1
    ant tar is completely useless. I understand why (java cant read file permissions or some garbage), but that doesn't make it usefull to have all my permissions rewritten just because I want to tar. This should be the right answer.
    – Chris
    May 23, 2011 at 17:15
  • 2
    this is not really a sollution will only work on Os with tar bins available what if I want to be able to build on any platform ? Mar 3, 2016 at 19:29
  • I've read the question, but since it is impossible, at least people will stop searching and use a nice solution.
    – neves
    Mar 29, 2022 at 21:03
8

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.

2
  • Let me guess: ivy + git repositories? Because that's what brought me to this question.
    – bassim
    Oct 12, 2014 at 12:36
  • this should be the answer ! this is the right way to do it Mar 3, 2016 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.