Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle run configurations with different properties

Tags:

java

gradle

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?

like image 644
Oleksii Duzhyi Avatar asked Jan 21 '15 16:01

Oleksii Duzhyi


People also ask

How do you pass properties in Gradle?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.

How do I run Gradle tasks in parallel?

Parallel execution Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.

Can we have multiple build Gradle?

A multi-project build in Gradle consists of one root project, and one or more subprojects. This is the recommended project structure for starting any Gradle project. The build init plugin also generates skeleton projects that follow this structure - a root project with a single subproject.


1 Answers

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

like image 176
gknicker Avatar answered Nov 05 '22 22:11

gknicker