简体   繁体   中英

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?

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

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")
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM