Peters_
Peters_

Reputation: 647

How to add a dependency to another project properly using gradle?

Hello I am new to gradle and it is a little bit confusing for me. How should I add a dependency in my gradle configuration to have access to B1.java in projectA1? Project B is gradle project and project A is just a folder with another gradle projects.

Here is my structure:

  1. Workspace:
    • ProjectA
      • projectA1
        • ...
        • here I want to have access to B1.java
        • build.gradle
      • projectA2
        • ...
        • build.gradle
    • ProjectB
      • projectB1
        • B1.java
        • ...
        • build.gradle
      • projectB2
        • ...
        • build.gradle
      • build.gradle

I tried to read gradle documentation, but it is not clear for me. Any help appreciated. Thanks!

Upvotes: 22

Views: 34494

Answers (3)

Jess
Jess

Reputation: 3715

  1. Edit setting.gradle in root path and include your sub projects, and then subproject will be able to see and depend on each other.
root
 |-- build.gradle << very simple bulid.grade , no special thing.
 |-- setting.gradle <<
rootProject.name = 'root'

file("${rootDir}/ProjectA").eachDirMatch(~/.*/) {
    include "ProjectA:${it.name}"
}

file("${rootDir}/ProjectB").eachDirMatch(~/.*/) {
    include "ProjectB:${it.name}"
}
  1. build.gradle in project
root
 |-- ProjectA
        |-- projectA1
            |-- build.gradle  <<
implementation project(':ProjectB:projectB1')

Ref:

Upvotes: 2

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363429

You should have a structure like this:

ProjectA
|--projectA1
|----build.gradle
|--projectA2
|----build.gradle
|--settings.gradle
|--build.gradle

ProjectB
|--projectB1
|----build.gradle
|--projectB2
|----build.gradle
|--settings.gradle
|--build.gradle

You can link an external module in your project.

1) In your project projectA/settings.gradle

include ':projectA1',':projectA2',':projectB1' 
project(':projectB1').projectDir = new File("/workspace/projectB/projectB1")

2) Add dependency in build.gradle of projectA1 module

dependencies {
    compile project(':projectB1')
}

Upvotes: 27

RaGe
RaGe

Reputation: 23637

Unless if projects A1 and B1 live in the same source repository and are checked-out and checked-in together, you really should depend on project B1 as an external dependency.

in Project A1 build.gradle:

dependencies{
    compile 'projectB1group:projectB1module:projectB1version'
}

Of course, for this to work, you will have had to publish B1 binaries to a repository that is accessible from Project A1 first. This can either be a external nexus/artifactory type maven repository, but can also be your local maven .m2 cache, or even a plain old file system. For maven publishing see maven or 'maven-publish` plugins.


If both projects live in the same source repo, you should organize ProjectA and ProjectB as subprojects under a root "container" project. The root project does not need to have source code of its own.

Read about organizing multi-project builds in gradle here.

If the root project has a settings.gradle with include lines that includes project B1, you can refer to any project under the root project like this:

project(':B1')

so, to add B1 as a dependency to A1, in A1's build.gradle:

compile project('B1')

Upvotes: 5

Related Questions