如何为相关的Gradle任务设置属性 [英] How to set properties for a dependent gradle task

查看:161
本文介绍了如何为相关的Gradle任务设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个通用"任务,该任务将为许多平台构建应用程序捆绑包(它使用javafxpackager).任务的机制适用于创建所有平台包,但是区别在于任务使用的各种属性.

I want to set up a "generic" task which will build app bundles for a number of platforms (it uses javafxpackager). The mechanics of the task apply to creating all platform bundles, but the difference is in the various property used by the task.

我曾想过,我将为每个平台创建单独的更高级别的任务,在其中将设置平台的特定属性,然后调用/执行/(在此处替换正确的gradle术语)该通用任务.例如,

I had thought that I would create separate higher level tasks for each platform in which the platform specific properties would be set, and then call/execute/(substitute the correct gradle lingo here) the generic task. E.g.,

task buildMacBundle(dependsOn: ['macBundleConfig', 'buildAppBundle']) << {
  // set Mac-specific properties (project.ext properties?)
  // call/invoke/execute or whatever the mechanism is called, buildAppBundle task
    buildAppBundle.mustRunAfter macBundleConfig
    println "building a Mac app bundle"
}

task macBundleConfig << {
    println "executing macBundleConfig"
    ext {
        nativeType = "dmg"
        bundleAppName = 'My App'
    }
    delete ("${buildDir.name}/dist/${bundleAppName}.dmg")
}


task buildWindowsBundle << {
  // omitted for brevity, but just like buildMacBundle except for property values
}

task buildAppBundle << {

    println "nativeType: ${project.ext.nativeType}" // it stumbles here!

    def cmd = [
        "${javapackager}",
        "-deploy",
        "-native", "${project.ext.nativeType}",
        "-name",
        "${project.bundleAppName}",
        "-outdir",
        "${buildDir.name}${File.separator}dist",
        "-outfile",
        "MyApp",
        "-srcdir",
        "${buildDir.name}${File.separator}${libsDir.name}",
        "-appclass",
        "org.pf.app.MyApp"
    ]

    println cmd.join(" ")

    def javapackager = exec {
        workingDir "${project.projectDir.absolutePath}"
        commandLine cmd
    }
}

但是当我运行"buildMacBundle"时,我得到了

But when I run "buildMacBundle", I get

* What went wrong:
Execution failed for task ':buildAppBundle'.
> cannot get property 'nativeType' on extra properties extension as it does not exist

如何在特定任务中定义属性,然后再调用通用任务?

How do I define the properties in the specific task which then invokes the generic task?

推荐答案

我遇到了类似的问题,我发现如果您切换

I faced a similar problem, and I found that if you switch

ext {
    nativeType = "dmg"
    bundleAppName = 'My App'
}

使用

project.ext {
    nativeType = "dmg"
    bundleAppName = 'My App'
}

它应该工作. 据我了解,这是一个范围界定问题-第一种情况是为Task设置属性,第二种情况是为Project设置属性.

it should work. From what I gather, it's a scoping problem - in the first case you set the property for the Task, and in the second case, for the Project.

顺便说一句,我认为buildAppBundle.mustRunAfter macBundleConfig对于您来说运行太迟了,因为它是buildMacBundle任务的一部分,因此运行顺序为:

By the way, I think that buildAppBundle.mustRunAfter macBundleConfig will run too late for you, as it is part of the buildMacBundle task, so the order of running will be:

  1. macBundleConfig
  2. buildAppBundle
  3. buildMacBundle

,只有在3期间,buildAppBundle的配置mustRunAfter才会更改.

and only during 3 will the configuration mustRunAfter of buildAppBundle will be changed.

这篇关于如何为相关的Gradle任务设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆