Manos Ntoulias
Manos Ntoulias

Reputation: 543

Gradle fatjar from directory

I have a folder ($buildDir/some/lib) that has a lot of jars inside. I want to create a jar that has all of those jars inside but unzipped. I've tried numerous things but all of them failed including the one below:

task fatJar(type: Jar, dependsOn: 'someTask') {
    manifest {
        attributes 'Main-Class': 'some.Main'
    }
    baseName = project.name + '-all'
    from ("$buildDir/some/lib") {
        eachFile { it.isDirectory() ? it : zipTree(it)  } }

    with jar
}

In this case the error is:

Cannot convert the provided notation to a File or URI: file '/path/to/the/buidDir/above/someJar.jar'.

Any help?

EDIT: I changed it to this but it still doesn't work.. jars are copied but they aren't unpacked.. help! why is this so hard?

task fatJar(....) {
    manifest {
        .....
    }
    baseName = project.name + '-all'
    FileCollection collection = files("$buildDir/some/lib")
    from collection.collect { it.isDirectory() ? it : zipTree(it) }

    with jar
}

Upvotes: 2

Views: 795

Answers (1)

M.Ricciuti
M.Ricciuti

Reputation: 12086

You can do the following:

task fatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'some.Main'
    }
    baseName = project.name + '-all'
    project.fileTree("$buildDir/some/lib").forEach { jarFile ->
        from zipTree(jarFile )
    }
    with jar
}

Note

In your first solution you get error "Cannot convert the provided notation to a File or URI..." because in the eachFile closure (see CopySpec.eachFile() you manipulate FileCopyDetails objects, while zipTree method expects a Zip Path. One solution would be to use getPath() method: eachFile { it.isDirectory() ? it : zipTree(it.getPath()) } }

Upvotes: 1

Related Questions