Oleksii Duzhyi
Oleksii Duzhyi

Reputation: 1223

Gradle run configurations with different properties

Here is my inputs:

Main.java

package com;

public class Main {

    public static void main(String[] args) {
        System.out.println(System.getProperty("greeting.language"));
    }
}

build.gradle

apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.7
version = '1.0'

mainClassName = "com.Main"

applicationDefaultJvmArgs = ["-Dgreeting.language=en"]

My question: How can I configure another run task configuration to use different jvm arguments, for example: -Dgreeting.language=ch.

I moved to gradle from maven, so for such case I would use maven profiles. Is there any strucrure in gradle which corresponds to profiles?

UPDATE 1

Let me clarify a task a little bit. My project is far more complex that example I gave above. I would like to have a set of gradle tasks(or profiles/or whatever it's called in gradle), each of the task should be self-contained with custom set of variables. As an output I would like to have set of gradle task:

gradle <enableAllFeatures>
gradle <disableFeatureA>
...

It is not a requirement for these variables to be JvmArgs. It could be constants(or whatever possible in gradle) but it is requirement to have different tasks, and each task should specify it's own set of variables

UPDATE 2

I was surfing internet and found out very interesting topic - gradle build variants, but it is seems to be applicable only to android projects. Is it possible to use build variants in java projects?

Upvotes: 6

Views: 6017

Answers (1)

gknicker
gknicker

Reputation: 5568

Parameterize the language like this:

applicationDefaultJvmArgs = ["-Dgreeting.language=${language}"]

Then when you run Gradle, pass the language as a parameter using the -P option:

$ gradle -Planguage=en tasks 

or

$ gradle -Planguage=ch tasks

References

Gradle Goodness: Using Properties for Multiple Environments or Profiles

Similar Maven profile Gradle

Upvotes: 2

Related Questions