1

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?

John
  • 10,837
  • 17
  • 78
  • 141

2 Answers2

2

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

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Hi, do you know if it's possible to pipe outputs to avoid intermediary files ?, I.e. currently I'm creating intermediary files via `ant.unar(src: debFile.absolutePath, dest: "folder"); ant.unxz(src: "folder/data.tar.xz", dest: "folder/data.tar");` – bric3 Jan 13 '21 at 16:54
1

Here's my solution that involves command line utilities.

task untar() {
    inputs.property('archiveFile', 'path/to/file.xz')
    inputs.property('dest', 'path/to/file.xz')
    outputs.dir("${buildDir}/destination")
    doLast {
        mkdir("${buildDir}/destination")
        exec {
            commandLine('tar', 'xJf', inputs.properties.archiveFile, '-C', "${buildDir}/destination")
        }
    }
}
bric3
  • 40,072
  • 9
  • 91
  • 111