【问题标题】:change file permission by gradle通过 gradle 更改文件权限
【发布时间】:2016-09-09 08:09:46
【问题描述】:

我创建 .jar 文件并将其移至目录,但我不明白之后如何更改此文件的权限。

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
}

【问题讨论】:

  • 为什么不首先使用正确的模式创建文件?
  • 您的文件模式错误。 755 是八进制(以 8 为底),但 fileMode 需要一个整数(以 10 为底)。您是否尝试设置 fileMode=493?

标签: gradle build.gradle


【解决方案1】:

如果您想将其嵌入到现有的自定义任务中,您可以使用 Project.exec(Action<? super ExecSpec> action)

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

project 在大多数任务实现中都可用,因为它来自 AbstractTask.getProject()

【讨论】:

    【解决方案2】:

    创建一个Exec 任务来更改文件权限。将此添加到您的 build.gradle 文件中

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

    使用doLast 块运行它。您的最终build.gradle 将如下所示:

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

    现在运行gradle fatJar 应该更改文件权限。确保在filePermission 任务中设置正确的路径

    【讨论】:

    • 他想在某个地方创建 jar,然后移动到 dir 更改其权限。我将使用一项任务来创建 jar,然后使用复制任务(取决于创建 jar 的任务)将 jar 复制到目录中,并通过正确设置 fileMode 来设置正确的权限。正如我上面写的 755 是错误的,应该用 int (base 10) 493 替换。
    【解决方案3】:

    上面的大多数解决方案都是不必要的复杂化。 发布此内容,以便我下次查找时可以找到它:

    distributions {
        main {
            contents {
                from fileTree('src/main/scripts'), {
                    filesMatching('myscript') { mode = 0744 }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      查看此处的解决方案,它们仅适用于 Linux 和 MacOS 等 unix 式操作系统。对于 Windows,gradle.kts 文件中的 Exec 任务如下所示:

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

      这对我有用。

      【讨论】:

        猜你喜欢
        • 2012-11-14
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 2011-10-16
        • 2016-02-22
        • 2017-06-15
        • 2012-02-16
        • 2013-06-27
        相关资源
        最近更新 更多