Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change file permission by gradle

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
}
like image 221
dzrkot Avatar asked Sep 09 '16 08:09

dzrkot


4 Answers

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().

like image 171
mkobit Avatar answered Oct 25 '22 00:10

mkobit


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.

like image 30
Shayne3000 Avatar answered Oct 01 '22 00:10

Shayne3000


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

like image 7
Ayon Nahiyan Avatar answered Oct 25 '22 02:10

Ayon Nahiyan


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 }
            }
        }
    }
}
like image 6
ddimitrov Avatar answered Oct 25 '22 01:10

ddimitrov