4

I have a situation where I have individual projects PROJECT_A and PROJECT_B under a common directory say projects. My Project folder looks like

Project

 - PROJECT_A 
 - PROJECT_B

Now is it possible for me to have a single build.gradle file for building both projects.

NOTE: I don't want to have individual build.gradle files for PROJECT_A and PROJECT_B, but the gradle file can have different task for building each projects

Delon
  • 741
  • 1
  • 6
  • 14

2 Answers2

2

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
Alex Klibisz
  • 1,313
  • 1
  • 14
  • 21
1

Multi-project tree - water (Main Project), (sub projects )bluewhale & krill projects.

Build layout

water/
   build.gradle
   settings.gradle
   bluewhale/
   krill/

settings.gradle

   include 'bluewhale', 'krill'

Now we rewrite the water build script and boil it down to a single line.

for more detail about multiple project build by gradle use this link as Reference here

Bhagirathsinh Gohil
  • 673
  • 1
  • 9
  • 25
  • 1
    Do I need to have build.gradle for 'bluewhale' and 'krill' ? – Delon Sep 24 '14 at 09:39
  • Downvoted because this doesn't explain how you'd handle dependencies and other configurations for each of the subprojects. I also don't think the official docs do a good job of this either. I was able to figure it out only after some trial and error. – Alex Klibisz Aug 08 '20 at 20:55