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:
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
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());