Set file permission in Copy/Tar tasks

I need to create Tar archive and want to make ONE file executable (file mode: 755) and leave permissions for other files as is. When I do it in the following way this permission (755) is applied to each file in the archive. What do I wrong?

task myArchive(type: Tar) {

baseName = “archive”

compression = Compression.GZIP

extension = “tar.gz”

into (“archive”) {

from(“src”) {

include “start”

fileMode 0755

}

}

into (“archive/lib”) {

from(configurations.runtime)

}

}

The ugly workaround of course is to use ant.tar like this:

ant.tar(destfile:‘build/libs/server.tar.gz’, compression: ‘gzip’, longfile:‘gnu’) {

include(name:‘server/**’)

tarfileset(dir:‘build/tmp/tarball/’) {

include(name:‘server/**’)

exclude(name:‘server/bin/*.sh’)

}

tarfileset(dir:‘build/tmp/tarball/’, filemode:‘755’) {

include(name:‘server/bin/*.sh’)

}

}

But really there should be a way to do this natively within gradle… but I don’t know what it is.

What I’d really like to do is get this working for the distribution plugin…

But the following doesn’t work:

ext {

tarballDir = new File(‘build/tarball’)

}

distributions {

ui {

baseName = ‘ui’

contents {

from {

tarballDir

exclude (’**/bin/*.sh’) // 1

}

from {

tarballDir

include (’**/bin/*.sh’)

fileMode 755

}

}

}

This idea came from http://gradle.1045684.n5.nabble.com/how-do-I-preserve-or-even-set-file-permissions-in-Tar-task-td4892337.html which might be a solution to the original question (haven’t tested it directly yet), but I’m baffled as to why the above doesn’t work.

When I do the above I get:

Converting class org.gradle.api.internal.file.copy.DefaultCopySpec_Decorated to File using toString() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use java.io.File, java.lang.String, java.net.URL, or java.net.URI instead.

:uiDistTar UP-TO-DATE

And the tar is never built if I remove the excludes marked // 1 and the second from the tarball builds just fine.

Ah found it…

from( tarballDir ) {

exclude (’**/bin/*.sh’)

}

from ( tarballDir ) {

include (’**/bin/*.sh’)

fileMode 0755

}

That works! (watch out for 0755 vs 755… 755 does not do what you expect!)

2 Likes