How to obtain reference to Settings object

(Eric Deandrea) #1

I can’t seem to find out how to obtain a reference to the Settings object. I’d like to be able to query it to see if certain sub-projects exist before I perform some logic. There doesn’t seem to be a “getSettings()” method on the Project object or anything.

I can see that there is a Settings object at runtime:

logger.lifecycle("Is there a settings? " + (Settings != null))
        if (Settings != null) {
    logger.lifecycle("Rootdir = " + Settings.getRootDir())
    logger.lifecycle("Root project = " + Settings.getRootProject().name)

When I just run logger.lifecycle("Is there a settings? " + (Settings != null)) inside a doLast of a task, I see the message “Is there a settings? true”, but when I add the other lines inside the if statement I see this output:

Is there a settings? true
  FAILURE: Build failed with an exception.
  * Where:
Build file 'C:\Projects1\master\new-project-setup.gradle' line: 54
  * What went wrong:
Execution failed for task ':addNewProject'.
> No signature of method: static org.gradle.api.initialization.Settings.getRootDir() is applicable for argument types: (
) values: []
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

(Peter Niederwieser) #2

You are referencing the ‘Settings’ class here, not a ‘settings’ property. I’m not aware of a way to get to the ‘Settings’ object from a build script, but ‘allprojects’ should give you all information you need. To learn more about this and related methods, see the DSL reference for the Project class.

(Eric Deandrea) #3

So if there isn’t a way to get to the settings object, why is there even documentation about it in the gradle DSL reference & API docs? If I have no way to use it or interact with it then why is it documented at all?

Oh and by the way - I’m looking forward to meeting you next week in Charlotte - I believe you are going to be the instructor for our training class (at least thats what I was told - I apologize if I’m incorrect).

(Peter Niederwieser) #4

The ‘Settings’ object is what you configure in ‘settings.gradle.’ It gets used in an earlier phase of the build (initialization phase) than regular build scripts.

See you in Charlotte. :slight_smile:

(Eric Deandrea) #5

What I’m trying to do is to grab hold of the settings object, add some things to the include list, and then have gradle write out the changes of the object to the settings.gradle file. Basically what I’m trying to accomplish is to write a task which asks for user input and automatically creates a new sub-project in a multi-project build, automatically brings in some project-specific things, and then updates the settings.gradle file. I thought it would be nice to be able to use the Settings object, but I guess I’ll have to read in the settings.gradle manually and write out to it manually.

Thanks for your help & I’ll see you next week.