Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle.settings multiline syntax

Tags:

gradle

groovy

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?

like image 221
gavenkoa Avatar asked Dec 10 '22 11:12

gavenkoa


2 Answers

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')
like image 159
dsharew Avatar answered Feb 24 '23 03:02

dsharew


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.

like image 41
Lukas Körfer Avatar answered Feb 24 '23 01:02

Lukas Körfer