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

巧了我就是萌 提交于 2020-12-29 05:38:22

问题


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!


回答1:


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')
}



回答2:


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')



回答3:


  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:

  • You can this setting.gradle in spring project

  • Official documentation : Declaring Dependencies between Subprojects

  • Official documentation : setting.gradle and demo



来源:https://stackoverflow.com/questions/35606790/how-to-add-a-dependency-to-another-project-properly-using-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!