change file permission by gradle

安稳与你 提交于 2019-12-05 06:03:19
Ayon Nahiyan

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

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

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 }
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!