aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Is there something similar to Maven's profile in Gradle? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Is there something similar to Maven Watch "Is there something similar to Maven New topic
Author

Is there something similar to Maven's profile in Gradle?

Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

The question subject says it all. I know we can use different properties file to be passed to Gradle, however Maven's profile is more then just different set of variables to be used. It is useful for grouping different logic for different profiles, so we do not end up with tons of switch/if..else all over the place.

Is there a similar feature in Gradle? If not, what is the best way to handle situation where we might have different set of task for different environments?
Hubert Klein Ikkink
author
Greenhorn

Joined: Jan 10, 2013
Posts: 13

Hi Wirianto,

you can use the apply from: <scriptfile> feature from Gradle to achieve this.

You can define for each profile a separate scriptfile with all the information needed for the profile. This is just another Gradle build file, so you can use what you know from Gradle. Define dependencies, task, extra plugins, etc. For example if you have a dev and test profile you can create the files dev-profile.gradle and test-profile.gradle.

In your "main" build file you define everything that is common for all profile and build needs, plus you add the following line:
apply from: "${profile}-profile.gradle"

When you run Gradle you can pass the name of the profile using the -P option:
$ gradle -Pprofile=dev tasks
or
$ gradle -Pprofile=test tasks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Is there something similar to Maven's profile in Gradle?