Search code examples
gradleant

How do I use ant-compress within Gradle (untar an xz tar file)?


I want to untar a file that is in .tar.xz format. Gradle's tarTree() does not support this format, so I need to unzip the .xz to a .tar, then I can make use of it.

According to the docs, i should be able to do something like this:

    ant.untar(src: myTarFile, compression: "xz", dest: extractDir)

However, I get an error:

Caused by: : xz is not a legal value for this attribute
    at org.apache.tools.ant.types.EnumeratedAttribute.setValue(EnumeratedAttribute.java:94)

This SO answer talks about using the Apache Ant Compress antlib within Maven. How can I achieve a similar result using Gradle?


Solution

  • Converting the Maven SO answer in your link would be something like:

    configurations {
       antCompress
    } 
    dependencies {
       antCompress 'org.apache.ant:ant-compress:1.4'
    }
    task untar {
       ext {
          xzFile = file('path/to/file.xz')
          outDir = "$buildDir/untar"
       } 
       inputs.file xzFile
       outputs.dir outDir
       doLast {
          ant.taskdef(
              resource:"org/apache/ant/compress/antlib.xml" 
              classpath: configurations.antCompress.asPath
          ) 
          ant.unxz(src:xzFile.absolutePath, dest:"$buildDir/unxz.tar" )
          copy {
             from tarTree("$buildDir/unxz.tar") 
             into outDir
          }   
       } 
    } 
    

    See https://docs.gradle.org/current/userguide/ant.html