开发者

ant tar task: procedure for enabling "tar append"

开发者 https://www.devze.com 2023-03-31 14:37 出处:网络
Goal: use ant to create a single tar archive for any combination of (say) 5 folders (all folders, or any 3 folders or any 4 folders

Goal: use ant to create a single tar archive for any combination of (say) 5 folders (all folders, or any 3 folders or any 4 folders ...). I prefer not using temp dirs and the copy command.

This should be simple if the ant tar task supports append.

A search lead me to:

Setting mode="update" in the compress antlib's tar task should do what is requested.

from: https://issues.apache.org/bugzilla/show_bug.cgi?id=39617

How do I know if 'antlib' is installed, or how do I install it. How does one set mode="update" - which file is chgd?.

What is the best way to enable "tar append" in ant's tar task?

GNU tar -r or -u options fail unless the tar file already exists - any ideas on how to deal w/that in ant?

--

thank开发者_JAVA技巧s

PS

The below example is a work around that forks a shell, then checks to see if the tarfile exists, and runs tar w/append option if it does:

<target name="package-blah" description="generate tar file" depends="clean,init">
  <exec executable="sh" >
      <arg value="-xc"/>
      <arg value="
        [[ -f ${tarfile} ]] &amp;&amp; C=r || C=c;
        tar ${C}vf ${tarfile} --exclude '*/.svn' ${src}/main/blah
      "/>
  </exec>
</target>

I would prefer not to use the above approach, but it works.


Two items are needed to use the Compress Antlib:

  • The Compress Antlib itself, which can be found here.
    If you already have this it will probably be named ant-compress-1.0.jar.
  • Apache Commons Compress, which can be found here.
    If you have this, it is likely to be called commons-compress-1.2.jar.

You could choose to install these with your Ant installation, but perhaps its neater to include them with your build.

Here's an example Ant buildfile that uses the two downloads, with them placed in a directory named external under the basedir for the build. This creates my.tar using files in directory dir_one then updates that tar with files added from dir_two.

<project basedir="." xmlns:compress="antlib:org.apache.ant.compress">

<taskdef uri="antlib:org.apache.ant.compress"
            resource="org/apache/ant/compress/antlib.xml"
            classpath="external/ant-compress-1.0.jar:external/commons-compress-1.2.jar"/>

<delete file="my.tar" />

<compress:tar dest="my.tar" mode="update">
   <fileset dir="dir_one" includes="*.txt" />
</compress:tar>

<compress:tar dest="my.tar" mode="update">
   <fileset dir="dir_two" includes="*.txt" />
</compress:tar>

</project>

Note the use of a namespace to disambiguate with the standard Ant tar task. The taskdef is needed to make Ant aware of the Antlib.

Care should be taken when merging this way - strange things may appear to happen if files with the same name are merged. Note that behind the scenes, Commons Compress is creating a temporary copy of the archive anyway, so this doesn't avoid that, it just does it for you.

If you want to explore further options for the tar task see the superclass ArchiveBase.


Rather than updating an existing archive, why don't you create the archive with multiple source folders in the first place?

<tar destfile="${target}/mytar.tar">
    <tarfileset dir="srcdir1">
        <include name="**/*" />
        <exclude name="**/*.sh" />
    </tarfileset>
    <tarfileset dir="srcdir2">
        <include name="**/*.sh" />
    </tarfileset>
    <tarfileset dir="srcdir3">
        <include name="**/*" />
    </tarfileset>
</tar>
0

精彩评论

暂无评论...
验证码 换一张
取 消