Here's an example of how you can have multiple subprojects with a single build.gradle.
The project structure has two subprojects, foo and bar, and each has exactly one java class Foo.java and Bar.java. Otherwise the directory just contains the default gradle directory and scripts:
├── bar
│ └── src
│ └── main
│ └── java
│ └── org
│ └── example
│ └── Bar.java
├── build.gradle
├── foo
│ └── src
│ └── main
│ └── java
│ └── org
│ └── example
│ └── Foo.java
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
The single build.gradle file looks like this. The comments should make it clear what's happening:
group 'org.example'
version '1.0-SNAPSHOT'
// These settings apply to all subprojects but not the root project.
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
// Get a variable for each project.
Project foo = project(':foo')
Project bar = project(':bar')
// Configure the foo project as you would in foo/build.gradle.
// Just using a misc. dependency for example purpose.
configure(foo, {
dependencies {
implementation 'software.amazon.awssdk:s3:2.13.71'
}
})
// Configure the bar project as you would in bar/build.gradle.
// Just making bar depend on foo for example purpose.
configure(bar, {
dependencies {
compile foo
}
})
Foo.java and Bar.java contain:
package org.example;
public class Foo {
public Foo() {
System.out.println("Hi, I'm Foo");
}
}
package org.example;
public class Bar {
public Bar() {
System.out.println("Hi, I'm Bar");
new Foo();
}
}
Then you can compile the full project:
$ ./gradlew compileJava
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 466ms
3 actionable tasks: 3 executed