Transfering part of one ZIP archive to another - path resolution


(Ivan Šarić) #1

Hello all,

Gradle newbie here. I read the Project and Zip documentation before posting but I can’t figure out what is the issue.

Intention: we are testing a custom gradle plugin and one of the tasks calls Ant Production which generates a Zip archive. Our task then proceeds to grab a folder from that zip file and package it into another zip file.

Problem: The task works fine when gradle is called from the project directory but it fails when gradle is called from outside the project with the path of the build.gradle file specified. Why is this important: We want to write tests for the plugin using GradleRunner but because GradleRunner is executing from one folder above the test fixture, the new Zip archive is never generated. The task reports success but the archive is not created. The issue should be in how the paths are constructed and I believed I could solve everything by prepending ${project.projectDir} in front of the relevant paths but it is not working.

Please have a look at the relevant code snippet that prepares the gradle zip task and let me know if I missed something.

class PackageService {
void prepareExtensionPackage(Project project, List extensionList, Task packageExtensionTask) {
extensionName = project.hasProperty("extn") ?
                project.ext.get("extn") : extensionList.get(0)
if (extensionList.contains(extensionName)) {
        def extDir = "inclusion/path/${extensionName}"
        def zipFile = project.file("relative/path/to/antProduction.zip")
        packageExtensionTask.destinationDir(new File("${project.projectDir}/build/tmp"))
        packageExtensionTask.from(project.zipTree(zipFile))
        packageExtensionTask.include("${extDir}/**")
        packageExtensionTask.into("${extensionName}")
        packageExtensionTask.eachFile { it ->
            it.path = it.path.replaceFirst("${extDir}", '')
        }
        packageExtensionTask.includeEmptyDirs(false)
        packageExtensionTask.baseName("${extensionName}")
    } else {
        println "Extension ${extensionName} is not configured for package!"
    }

(uklance) #2

Don’t use new File('someRelativePath') as this will be relative to the working dir of the Gradle process. Use file('someRelativePath') instead.

See project.file(Object)


(Ivan Šarić) #3

Right, but the only place where we are using this is
packageExtensionTask.destinationDir(new File("${project.projectDir}/build/tmp"))
and we are injecting ${project.projectDir} in there so it should resolve to the same.