How to set multiproject with customized build script ("-b")?

From the User Guide 11.5: When you run the gradle command, it looks for a build file in the current directory. You can use the -b option to select another build file. If you use -b option then settings.gradle file is not used.

But I have to have a settings.gradle to set a multiproject project. I tried to set it using “-c” and it was still ignored. It seems to be in the code here in org.gradle.initialization.SettingsHandler

public SettingsInternal findAndLoadSettings(GradleInternal gradle) {
        StartParameter startParameter = gradle.getStartParameter();
        SettingsInternal settings = findSettingsAndLoadIfAppropriate(gradle, startParameter);
          ProjectSpec spec = ProjectSpecs.forStartParameter(startParameter);
          if (!spec.containsProject(settings.getProjectRegistry())) {
            // The settings we found did not include the desired default project
            if (startParameter.getProjectDir() == null && spec.isCorresponding(settings.getSettingsDir())) {
                // The desired default project is the settings directory
                // So execute the root project instead.
                settings.setDefaultProject(settings.getRootProject());
            } else {
                // Try again with an empty settings file.
                StartParameter noSearchParameter = startParameter.newInstance();
                noSearchParameter.useEmptySettings();
                settings = findSettingsAndLoadIfAppropriate(gradle, noSearchParameter);
                if (settings == null) { // not using an assert to make sure it is not disabled
                    throw new InternalError("Empty settings file does not contain expected project.");
                }
                  // Set explicit build file, if required
                if (noSearchParameter.getBuildFile() != null) {
                    ProjectDescriptor rootProject = settings.getRootProject();
                    rootProject.setBuildFileName(noSearchParameter.getBuildFile().getName());
                }
                setDefaultProject(spec, settings);
            }
        } else {
            setDefaultProject(spec, settings);
        }
          return settings;
    }

What do I do?

‘-b’ cannot be used in a multiproject build. It’s mostly meant for experimentation. In a real-world build, users shouldn’t be required to frequently use command line parameters such as ‘-b’.

Thank you very much. Is there any inherit difficulty to allow the alternative script in multiproject one? We do need to fuddle with it sometimes.

It’s not clear to me what the meaning/purpose would be. Use a different root build script but keep the same subproject build scripts?

Devstack.org

We’d like to run Sonar at the top level on branches, but at the sub-project level on Master. One way to do that would be via the -b option with an alternative build script that just runs Sonar at the top level.

Is there any solution for this please?

I want to use the custom build.gradle and settings.gradle files (multi project)…please let me know if there is any solution. I tried with -b and -c options it does not work.

‘-b’ can’t be used for multi-project builds (see my previous answers in this thread). If you elaborate on your problem, perhaps we can find a different solution. For example, you could have different ‘settings.gradle’ files (selected via ‘-c’), each of which points to a different ‘build.gradle’ file.

For this use case (and possibly other releated onese) you can work around it by using a system property and check for it within a single build file.

e.g.

if(System.getProperty(“sonar”)==‘topLevel’)

apply from: ‘sonar-config.gradle’

subprojects {

… if(System.getProperty(“sonar”)!=‘topLevel’)

apply from: ‘sonar-config.gradle’

… }

then

gradle sonarRunner -Dsonar=topLevel

will run it at the top level for the project

and

gradle sonarRunner

will run it per sub-project.

But I think a separate build file would be cleaner.

Here is my problem: these are all under same root project called “mtest”

ear1 war1 ear2 war2 lib1 lib2 lib3

i have one gradle file for ear1 and another one for ear2…but i want to keep them in same root project.

build_ear1.gradle settings_ear1.gradle build_ear2.gradle settings_ear2.gradle

as you said, it is ignoring the settings.gradle file though i am using with -c option

If you truly need to select both settings script and build script(s) via command line arguments, you can use ‘-c’ to select a settings script, and that settings script can configure build script path(s) based on some system property (passed via ‘-D’). The latter can also act as a replacement for ‘-b’ in multi-project builds.