How to set properties for a dependent gradle task

gradle

Solution

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

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

with

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

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`.

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:

- macBundleConfig

- buildAppBundle

- buildMacBundle

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

Problem

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. 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 } } ``` 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?

Original source