Delon
Delon

Reputation: 741

Gradle build on multiple projects using single build.gradle

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

Upvotes: 4

Views: 2663

Answers (2)

Alex Klibisz
Alex Klibisz

Reputation: 1323

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

Upvotes: 2

Bhagirathsinh Gohil
Bhagirathsinh Gohil

Reputation: 673

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

Upvotes: 1

Related Questions