Using both -b and -p options with gradle command

How can I run the gradle command and specify a build file with the -b option and the project to build in that file with -p? It seems that I can’t use both of them together.

For example if I run “gradle build -b /foo/bar/build.gradle -p project1” it builds the whole file and not just project1 in the build file.

‘-b’ is only meant to be used for single-project builds. I’ve never used ‘-p’ before, but apparently it sets the Gradle start directory, not the project to build.

So what you are saying is if I am in the directory of the build file and run gradle, it will find the build.gradle file and it can have multiple projects? But if I want to run a specific file it can’t be a multiple project build file?

That doesn’t sound right. Because if I run “gradle -b /foo/bar/build.gradle” it will build all the projects in the file.

And -p is to set the gradle start directory. I have a gradle build file at /foo/bar/build.gradle, and I want it to build the dir /foo/bar/project1.

For some reason I can’t do both at the same time.

What I’m saying is that with ‘-b’, you are instructing Gradle to use a single build script (which is expected to be self-contained), rather than applying the usual search algorithm for finding settings and build scripts. Not sure what you mean by “I want it to build the dir”. Can you elaborate what problem you are trying to solve?

So I have a gradle build file /foo/bar/build.gradle. it has multiple projects in the file, like so:

project(':project1') {
 .....
}
  project(':project2') {
 .....
}
  project(':project3') {
 .....
}

Now when I am in the directory /foo/bar and run the command “gradle build -p project1” it only builds that project, or as it states has a starting directory of /foo/bar/project1.

Then if I run the command “gradle build -b /foo/bar/build.gradle -p project1” it builds all three projects not just project1.

Why will it do what I want it to do when I run the command from the /foo/bar directory, but not when I specify the build file with the -b option? How are those different?

In the second case, I assume that you are not in directory ‘/foo/bar’, and therefore ‘-p project1’ doesn’t seem like the correct (relative) path anymore. Can you try with an absolute path?

The typical way to run a subproject task without cd’ing into the subproject’s directory is ‘gradle :project1:build’.

That seemed to work. I was able to run “gradle :project1:build -b /foo/bar/build.gradle”. That seems to do exactly what I want.

Thanks for the help.