13

I create .jar file and move it to dir, but I don't understand how I can change permission for this file after.

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                'Implementation-Version': version,
                'Main-Class':'com.asd.App',
                'Class-Path': 'com.asd'
    }
    baseName = project.name + '-all'
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    def file = file('/home/master/project/asd')
    fileMode = 755
    destinationDir = file
    with jar
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
dzrkot
  • 543
  • 2
  • 4
  • 23
  • Why not create the file with the correct mode in the first place? – Henry Sep 09 '16 at 09:27
  • Your fileMode is wrong. 755 is octal (base 8) but fileMode wants an integer (base 10). Did you try setting fileMode=493? – Alessandro Caproni May 11 '21 at 15:21
  • @Henry For me the mode defined in `jar {...}` is ignored. This can happen according to the docs: "It is dependent on the copy action implementation whether these permissions will actually be applied." – Joooeey Oct 10 '22 at 10:11

4 Answers4

11

If you want to embed it in an existing custom task, you use can Project.exec(Action<? super ExecSpec> action).

task changePermission {
  doLast {
    project.exec {
      commandLine('chmod',  '+x', '<fileLocation>')
    }
  }
}

The project is available in most task implementations because it comes from AbstractTask.getProject().

mkobit
  • 43,979
  • 12
  • 156
  • 150
7

Create an Exec Task to change file permission. Add this in your build.gradle file

task filepermission(type: Exec) {
    commandLine 'chmod', '700', '<file_path>'
}

Run this using a doLast block. Your final build.gradle will look like this:

task filepermission(type: Exec) {
    commandLine 'chmod', '700', '<file_path>'
}

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                'Implementation-Version': version,
                'Main-Class':'com.asd.App',
                'Class-Path': 'com.asd'
    }
    baseName = project.name + '-all'
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    def file = file('/home/master/project/asd')
    fileMode = 755
    destinationDir = file
    with jar
    doLast {
        filepermission.execute()
    }
}

now running gradle fatJar should change the file permission. Make sure you set proper path in the filePermission task

mkobit
  • 43,979
  • 12
  • 156
  • 150
Ayon Nahiyan
  • 2,140
  • 15
  • 23
  • He wants to create the jar seomewhere then move to dir changing its permissions. I would use one task to create the jar, then a copy task (depending on the one that created the jar) to copy the jar in the dir and sets the proper pemissions by setting fileMode properly. As I wrote above 755 is wrong and should be replaced by the int (base 10) 493. – Alessandro Caproni May 11 '21 at 15:23
6

Most of the solutions above are unnecessarily convolurted. Posting this so I can find it next time I look for it:

distributions {
    main {
        contents {
            from fileTree('src/main/scripts'), {
                filesMatching('myscript') { mode = 0744 }
            }
        }
    }
}
ddimitrov
  • 3,293
  • 3
  • 31
  • 46
2

Looking at the solutions here, they are only catering to unix-esque operating systems like Linux and MacOS. For Windows, the Exec task within a gradle.kts file would look thus:

tasks {
    register<Exec>("makeFileExecutable") {
        workingDir(rootDir)
        commandLine("cmd", "755", "path/to/folderorfile")
    }
}

This is what worked for me.

Shayne3000
  • 607
  • 8
  • 5