将内置 gradle 任务移至 doLast/内置任务快捷方式

发布于 2024-10-10 14:44:26 字数 927 浏览 6 评论 0原文

我想创建一个简单的同步任务,根据构建类型(例如调试/发布)稍微改变它的行为,并且我使用在 gradle.taskGraph.whenReady 中声明的布尔变量“虚拟”:

gradle.taskGraph.whenReady {taskGraph ->
  dummy = false
  if (taskGraph.hasTask(':dummybuild')) {
    dummy = true
  }
}

问题是通过以下方式配置的任务具有配置范围,即在whenReady之前,因此它无法访问“虚拟”变量:

task copySkins(type: Sync) {
  from skinsFrom
  into skinsInto
  rename skinsRename
  exclude symbianExclude
  if (!dummy) exclude dummyExclude
}

现在我正在使用此解决方法

task copySkins {
  inputs.dir skinsFrom
  outputs.dir skinsInto

  doLast {
    task skins(type: Sync) {
      from skinsFrom
      into skinsInto
      rename skinsRename
      exclude symbianExclude
      if (!dummy) exclude dummyExclude
    }
    skins.execute()
  }
}

除了whenReady之外,是否可以

  1. 在其他地方检测/设置一些构建属性
  2. 将同步任务工作移至doLast
  3. 或者至少有一些同步任务的快捷方式(.execute() 看起来很丑陋)

I want to create a simple sync task that slightly change it behaviour depending on build type (e.g. debug/release) and I use boolean variable 'dummy' decrared in gradle.taskGraph.whenReady:

gradle.taskGraph.whenReady {taskGraph ->
  dummy = false
  if (taskGraph.hasTask(':dummybuild')) {
    dummy = true
  }
}

The problem is that task configured by the following way has configuration scope, i.e. before whenReady so it doesn't have access to the 'dummy' variable:

task copySkins(type: Sync) {
  from skinsFrom
  into skinsInto
  rename skinsRename
  exclude symbianExclude
  if (!dummy) exclude dummyExclude
}

Right now I'm using this workaround

task copySkins {
  inputs.dir skinsFrom
  outputs.dir skinsInto

  doLast {
    task skins(type: Sync) {
      from skinsFrom
      into skinsInto
      rename skinsRename
      exclude symbianExclude
      if (!dummy) exclude dummyExclude
    }
    skins.execute()
  }
}

Is it possible to

  1. detect/setup some build properties in some other place except whenReady
  2. move sync task work to doLast
  3. or at least have some shortcut for sync task (.execute() looks quite ugly)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2024-10-17 14:44:26

1) whenReady 事件允许用户访问完全初始化的任务图:所有初始化已完成并且任务已准备好运行。当您需要在此处检测/设置构建属性时,唯一的情况是您需要内省当前构建设置。
如果您不需要此信息,您可以将初始化放在构建脚本中的任何位置。最后,这只不过是绝妙的脚本。

    apply plugin: 'java'
    def now = new Date()
    compileJava.doFirst {
      println "It is ${now}. We are starting to compile"
    }

2) 您不能将同步任务工作移至 doLast。但你总是可以将你的操作添加到 doFirst ;)我认为,这应该有效:

task copySkins(type: Sync) {
  from skinsFrom
  into skinsInto
  rename skinsRename
  exclude symbianExclude

  doFirst {
    if (!dummy) exclude dummyExclude
  }
}

3)正如之前所说,缺少同步任务快捷方式不应该那么痛苦

1) whenReady event allows user to access fully-initialized task graph: all initialization is finished and tasks are ready to run. The only situation, when you need to detect/setup build properties here, is when you need to introspect current build setup.
If you do not need this information, you can place your initialization anywhere in your build script. At the very end, it is nothing but groovy script.

    apply plugin: 'java'
    def now = new Date()
    compileJava.doFirst {
      println "It is ${now}. We are starting to compile"
    }

2) You can not move sync task work to doLast. But you can always add your actions to doFirst ;) I think, this should work:

task copySkins(type: Sync) {
  from skinsFrom
  into skinsInto
  rename skinsRename
  exclude symbianExclude

  doFirst {
    if (!dummy) exclude dummyExclude
  }
}

3) With all said before, missing sync task shortcut should not be that painfull

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文