Daniel Ditgens
Daniel Ditgens

Reputation: 133

Accessing variables in build.gradle's section plugins

I want to use variables in section Plugins (from gradle.properties)

plugins {
    id 'java'
    id 'war'
    id 'eclipse-wtp'
    id "com.moowork.grunt" version $grunt_version
    id "org.akhikhl.gretty" version $gretty_version
}

but this won't work. Any suggestions?

Upvotes: 6

Views: 1579

Answers (2)

Daniel Ditgens
Daniel Ditgens

Reputation: 133

By digging deeper in the gradle docs I found this:

The form is:

plugins {
    id «plugin id» version «plugin version»
}

Where «plugin version» and «plugin id» must be constant, literal, strings. No other statements are allowed; their presence will cause a compilation error.

The plugins {} block must also be a top level statement in the buildscript. It cannot be nested inside another construct (e.g. an if-statement or for-loop).

...

If the restrictions of the new syntax are prohibitive, the recommended approach is to apply plugins using the buildscript {} block.

I think I'll give it a try...

Upvotes: 5

Jolta
Jolta

Reputation: 2725

Generally, dollars are only used inside double quoted strings, to expand a variable name to its value. Like Opal indicates in their comment, you can call a variable by simply its name in most places. Depending on the variable's scope, it may be appropriate to call it as variableName, project.variableName, etc, depending on which object the variable belongs to.

For instance:

def foo = "boo"

The following two are equivalent:

def moreOos = foo + "ooo"
def alsoMoreOos = "${foo}ooo"

Upvotes: 0

Related Questions