gavenkoa
gavenkoa

Reputation: 48883

gradle.settings multiline syntax

I have very long include in settings.gradle:

include 'project-subproj1', 'project-subproj2', 'project-subproj3', 'project-subproj4'

What proper syntax to split long lines in settings.gradle?

I think about:

include 'project-subproj1', \
        'project-subproj2', \
        'project-subproj3', \
        'project-subproj4'

but seems that

include 'project-subproj1',
        'project-subproj2',
        'project-subproj3',
        'project-subproj4'

also works.

Is settings.gradle a regular Groovy script?

Upvotes: 4

Views: 1902

Answers (3)

LazerBanana
LazerBanana

Reputation: 7221

Yes, settings.gradle is a Groovy script, like other Gradle scripts. settings.gradle is executed in the initialization phase, therefore, it is the multi projects essential file to specify the subprojects, which you can apply in few ways.

You can include each project separately as well, the same effect at the end.

include 'project-subproj1'
include 'project-subproj2'
include 'project-subproj3'
include 'project-subproj4'

Can iterate through the list.

['project-subproj1',
'project-subproj2',
'project-subproj3',
'project-subproj4'].each({include it})

Depends on what you feel is more readable/clean for you.

Upvotes: 2

dsharew
dsharew

Reputation: 10665

Is settings.gradle a regular Groovy script?
Yes, it is.

Just to explain a bit, Gradle reads the settings.gradle file and it actually creates an instance of Setting class.

If you see the include method spec on the Setting class it is like this:

void include(java.lang.String[] strings);

So the method accepts array of String class as argument. In groovy you can call this method in different ways:

include "project1", "project2"

include "project1",  
    "project2"

include (['project1',
    'project2'] as String[])

include ('project1',
     'project2')

Upvotes: 7

Lukas Körfer
Lukas Körfer

Reputation: 14523

Yes, in Gradle, each file that ends with .gradle is a regular Groovy script. Unscripted files in Gradle are indicated by other file extensions (e.g. .properties).

There are several conventions that define special groovy scripts:

  • build.gradle for the build configuration script
  • settings.gradle as build setup script
  • init.gradle in USER_HOME/.gradle/ for a global initialization script

You can create additional .gradle Groovy scripts and use them via apply from: or place them at special locations (e.g. USER_HOME/.gradle/init.d/ to use them as initialization scripts).

The main difference between all these scripts is the context they are applied on. All scripts first implement the Script interface and a second (role-specific) interface. The Gradle docs call this behaviour "attaching a delegate object".

  • build.gradle scripts are applied on a Project object
  • settings.gradle scripts are applied on a Settings object
  • initialization scripts are applied on a Gradle object

Now, we can even understand the mentioned code example:

include 'project-subproj1',
    'project-subproj2',
    'project-subproj3',
    'project-subproj4'

The Settings interface defines the include method, which is called in Groovy style: without brackets and across lines. You could also apply logic in your settings.gradle file, e.g. to require a condition before including a specific project.

Upvotes: 4

Related Questions