Have init.gradle script read property overrides

Hi,

I’m investigating the use of init.d scripts but I’m not sure they are powerful enough to do what I need. One requirement is the ability to override properties that the init script sets up. For example:

init.gradle:

buildscript {
    repositoties {
        def buildscriptArtifactPattern = "[organisation]/[module]/[revision]/[type]/[artifact]-[revision](-[classifier]).[ext]";
        def buildscriptIvyPattern = "[organisation]/[module]/[revision]/[artifact]-[revision].[ext]";
        ivy {
           name "local-bootstrap"
           url ((hasProperty("rootDirOverride") ? "${rootDirOverride}/.." : "${rootDir}/..") + "/.ivy2/local")
           layout "pattern", { artifact "$buildscriptArtifactPattern";  ivy "$buildscriptIvyPattern"}
       }
   }
}

In that example I would like the ability for projects and command line arguments to be able to override the location of the local ivy file repository. I would expect to be able to read it from the command line via -D and from the file system if it is set in gradle.properties (since settings.properties is read during the initialization phase). Neither is working.

Printing all system properties, I see this:
sun.java.command=org.gradle.launcher.GradleMain -DrootDirOverride=…/… --init-script Test\init.gradle build

What am I missing?

I found a way to do what I want. I don’t know how ideal it is but here it is:

To access command line properties from init.gradle script:

println gradle.getStartParameter().getSystemPropertiesArgs().get('rootDirOverride');

To access settings.gradle properties from init.gradle script:

// org.gradle.initialization.SettingsLocation
// org.gradle.initialization.DefaultSettingsFinder
// org.gradle.initialization.layout.BuildLayoutFactory

SettingsLocation settingsLocation = new DefaultSettingsFinder(new BuildLayoutFactory()).find(gradle.getStartParameter());

File rootPropertiesFile = new File(settingsLocation.getSettingsDir(), "gradle.properties");
if (rootPropertiesFile .exists()) {
    // Note this will not evaluate the settings.properties file... it will only load properties line by line with '=' as a separator
    Properties p = GUtil.loadProperties(rootPropertiesFile );
    println p.get('rootDirOverride');
}

Combining them yields:

import org.gradle.initialization.SettingsLocation;
import org.gradle.initialization.DefaultSettingsFinder;
import org.gradle.initialization.layout.BuildLayoutFactory;

SettingsLocation settingsLocation = new DefaultSettingsFinder(new BuildLayoutFactory()).find(gradle.getStartParameter());

File rootPropertiesFile = new File(settingsLocation.getSettingsDir(), "gradle.properties");
Properties settingsProp = new Properties();
if (rootPropertiesFile .exists()) {
	settingsProp = GUtil.loadProperties(settingsFile);
}

// CLI arguments will override settings.properties
settingsProp.putAll(gradle.getStartParameter().getSystemPropertiesArgs());