Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running "./gradlew <task>" in command line, what file is read?

I have done some digging, but have not found much information.

My best guess would be the build.gradle file is the default, but I was curious on the behavior and what files are read.

like image 766
Dakota Maker Avatar asked Dec 14 '22 19:12

Dakota Maker


1 Answers

While the other answer covers 99.99% of all builds, it is not complete.

The full command is ./gradlew [-b / --build-file <build.file>] [-c / --settings-file <settings.file>]. And when you run it, those specified <build.file> and <settings.file> will be used to configure the build and it's settings correspondingly. Settings file is special: it configures your Project objects on the very early stages of your build and it can actually override it's build file.

Here are the examples (sorry for the deprecated <<, it is used just to shorten the code):

Default build.gradle, no settings.gradle:

build.gradle:

task hello << { println "Hello" }

Results:

$ ./gradlew hello
:hello
Hello

Custom build.gradle, no settings.gradle:

custom.gradle:

task hello << { println "Hi!" }

Results:

$ ./gradlew -b custom.gradle hello
:hello
Hi!

Custom build.gradle, configured in settings.gradle:

custom.gradle:

task hello << { println "Konnichi wa" }

settings.gradle:

rootProject.buildFileName = 'custom.gradle'

Results:

./gradlew hello # note that we don't need any flags here as with a default build
:hello
Konnichi wa

Custom build.gradle, configured in custom settings.gradle:

custom.gradle:

task hello << { println "Aloha!" }

settings.custom:

rootProject.buildFileName = 'custom.gradle'

Results:

./gradlew -c settings.custom hello # note how we pass custom settings file which, in turn, specifies custom `build.gradle`
:hello
Aloha!

That is all mostly fancy and not practical, but you can combine these approaches to store, for example, all you build files in one separate directory (overriding buildFileName). Or you can have something like "build profiles" with multiple settings.gradle, with different set of includes (settings are also used to include projects into you build, so you can have profiles like "full build", "ui", "external clients" and so on).

The only limit is your imagination.

like image 53
madhead - StandWithUkraine Avatar answered Dec 16 '22 08:12

madhead - StandWithUkraine