Search code examples
gradlegroovyant

Replacement in Gradle for Ant's zipfileset


I am trying to migrate Ant targets to Gradle tasks, but I'm having an issue. In Ant, zipfileset allows us to specify a prefix folder to all files inside jar or zip. How can we achieve this in Gradle?

How can we replace the Ant code below in Gradle?

<jar destfile="${jarDestination}">
  <zipfileset dir="${dir}" prefix="${prefix}"/>
</jar>

Notice: I know that we can use ant inside Gradle. I seeking for clear gradle/groovy solution.


Solution

  • apply plugin: 'java'
    
    def dir = 'src/myJar/resources'
    def prefix = 'foo'
    File jarDestination = file("$buildDir/myJar/myJar.jar")
    
    task myJar(type: Zip) {
       from dir {
          into prefix
       }
       destinationDir = jarDestination.parent
       archiveName = jarDestination.name
    }
    assemble.dependsOn myJar