How change project name when using non- "build.gradle" build file?

My “settings.gradle” contains only: rootProject.name = ‘rpn’

If I run “build -b build.gradle tasks”, the project name is reported as “rpn”.

If I move my “build.gradle” then apparently, though projects are directory-based, Gradle seems to run the build file with some new, non-root project, since the rootProject.name assignment runs but no longer has any effect.

Copy and paste showing that settings. gradle works to change project name, but only if it’s named “build.gradle”:

s5ubis@s5ubis-pc$ gradle --no-color -b build.gradle tasks | grep dependenc
dependencies - Displays the dependencies of root project 'rpn'.
s5ubis@s5ubis-pc$ mv -v build.gradle moved.gradle
'build.gradle' -> 'moved.gradle'
s5ubis@s5ubis-pc$ gradle --no-color -b moved.gradle tasks | grep dependenc
dependencies - Displays the dependencies of root project 'gra2'.
s5ubis@s5ubis-pc$

This is important for me. Due to multiple limitations of Gradle that I am working around, I have to use different build files for different usage scenarios, and I need the project name to be correct.

You can workaround the behavior by adding this block to your settings.gradle. This makes an assumption that you only pass in file names on the -b flag and not paths to a file in a different directory.

if (startParameter.buildFile) {
 rootProject.buildFileName = startParameter.buildFile.name
}

The issue has to do with the findAndLoadSettings method on the org.gradle.initialization.SettingsHandler class. Anytime that you change the build file to use through the CLI, it’s going to ignore your settings unless they set the build file to the same thing you specified on the CLI. It does, however, parse the settings, so you will see output if you throw some printlns in there.

Oddly, this seems to work fine if you change the build file in the settings.gradle, but without specifying it on the CLI. So something like this is also an option, but not as nice:

settings.gradle

rootProject.buildFileName = "${buildFile}"

CLI:

gradle -PbuildFile=moved.gradle tasks

At the very least it seems like it would be nice for the build file specified in the StartParameter to be the default on the Settings object, so you don’t need the workaround.

You work-around works great. Thanks.