Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在Gradle中检查文件的当前内容_File_Gradle_Order Of Execution - Fatal编程技术网

File 如何在Gradle中检查文件的当前内容

File 如何在Gradle中检查文件的当前内容,file,gradle,order-of-execution,File,Gradle,Order Of Execution,首先也是最前面。。。我是格拉德尔的新手。话虽如此,我还是喜欢它。不幸的是,我遇到了一个障碍。我有一系列任务是部署过程的一部分。一个构建项目调用一个shell脚本,作为其过程的一部分,该脚本将用新版本更新修订文件。然后调用deployToRemote任务将最新版本部署到服务器。它调用getCurrentVersion从修订文件中读取最新版本。所有这些任务概述如下。问题在于,尽管有适当的mustRunAfter语句,但getLatestVersion似乎是首先被调用的,因为它总是在修订文件中列出的预

首先也是最前面。。。我是格拉德尔的新手。话虽如此,我还是喜欢它。不幸的是,我遇到了一个障碍。我有一系列任务是部署过程的一部分。一个构建项目调用一个shell脚本,作为其过程的一部分,该脚本将用新版本更新修订文件。然后调用deployToRemote任务将最新版本部署到服务器。它调用getCurrentVersion从修订文件中读取最新版本。所有这些任务概述如下。问题在于,尽管有适当的mustRunAfter语句,但getLatestVersion似乎是首先被调用的,因为它总是在修订文件中列出的预构建项目版本中读取。如何确保getLatestVersion在buildProject运行后读取文件

以下是任务:

建筑项目:

task buildProject(type:Exec) {
  def command = ['./make-release', '-f']
  if (deployEnvironment != 'stage') {
    command = ['./make-release', "-e ${deployEnvironment}"]
  }

  commandLine command
}
部署远程

task deployToRemote(dependsOn: 'getCurrentVersion') {
  doLast {
    def version = tasks.getCurrentVersion.hash()
    println "Using version ${version}"
    println "Using user ${webhostUser}"
    println "Using host ${webhostUrl}"
    ssh.run {
      session(remotes.webhost) {
        put from: "dist/project-${version}.tar.gz", into: '/srv/staging/'
        execute "cd /srv/staging; ./manual_install.sh ${version}"
      }
    }
  }
}
getCurrentVersion

task getCurrentVersion {
  def line
  new File("REVISION").withReader { line = it.readLine() }
  ext.hash = {
    line
  }
}
我的build.gradle文件末尾有以下内容:

deployToRemote.mustRunAfter buildProject
getCurrentVersion.mustRunAfter buildProject
修订文件如下所示

1196.dev10
919b642fd5ca5037a437dac28e2cfac0ea18ceed
dev
Gradle构建包括:初始化、配置和执行

您面临的问题是getCurrentVersion中的代码是在配置阶段执行的。在配置阶段,任务中的代码按照其定义的顺序执行,并且不考虑依赖关系

考虑这个例子:

task second(dependsOn: 'first') {
    println 'second: this is executed during the configuration phase.'
    doLast {
      println 'second: This is executed during the execution phase.'
    }
}

task first {
    println 'first: this is executed during the configuration phase.'
    doLast {
      println 'first: This is executed during the execution phase.'
    }
}

second.mustRunAfter first
如果执行gradle-q second,您将得到:

second: this is executed during the configuration phase.
first: this is executed during the configuration phase.
first: This is executed during the execution phase.
second: This is executed during the execution phase.
要修复脚本,需要将代码放入doLast,如下所示:

task getCurrentVersion {
  doLast {
     def line
     new File("REVISION").withReader { line = it.readLine() }
     ext.hash = {
       line
     }
  }
}

当然如此明显,如此简单,如此完美。谢谢@roman konaval!