Source code on github:


Clone the project demo002 into demo007

Profiles in gradle can be simulated in various way

First we should have a way to define a default profile. Here an environment variable is used. Simply add it to the main build.gradle. Remember that we are using a real programming language, not a bunch of xml files!

if (!hasProperty('buildProfile')) ext.buildProfile = 'default'

Properties for the build script

Now a new directory demo007/profiles is created containing two files

We can read the content of these into gradle's ext variable. Hem.. had you noticed some plain Java?? Here we are loading inside the project "ext" variable the properties that are needed by the build

Properties props = new Properties()
props.load(new FileInputStream(project.rootDir.path+'/profiles/'+ext.buildProfile+'.properties'))

And of course use it inside all our scripts

println 'Deployment Target: ' + ext.deploymentServer

Java properties files

It is also possible to use specific property files to include in the various projects. To give an example you can create the directories demo007/profiles/default and demo007/profiles/prod containing the environment specific properties for the project

At first is needed a variable containing the demo007 path, this must be set inside the main build.gradle and used by all the subprojects


Then inside the subprojects we can add the specific files. For example in startup/build.gradle. Note that we are using the "ext" variables defined inside the root project!

sourceSets {
    main {
            resources {
                srcDir 'file://'+rootDir+'/profiles/'+buildProfile
                include ''

Then changing the main class to the following, it would be possible to print the content of the resource!

package org.kendar;

import java.util.Properties;

public class HelloWorld {
    public static void main(String[] args) {
        HelloWorldService service = new HelloWorldService();

        Properties props = new Properties();
        } catch (IOException e) {
        System.out.println("I use maxThreads: " + props.getProperty("maxThreads"));

Running "gradle run" will result in the following

gradle init

While adding a parameter

gradle run -PbuildProfile=prod

gradle init

Notice the "maxThreads" and Deplyoment target values!!

008-Default variables

${projectDir}: The project directory ${buildDir}: The target build directory

Last modified on: February 18, 2020