如何使用gradle正确添加依赖项到另一个项目?

时间:2023-01-25 11:39:17

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.

你好我是gradle的新手,对我来说有点困惑。我应该如何在gradle配置中添加依赖项以访问projectA1中的B1.java?项目B是gradle项目,项目A只是一个包含另一个gradle项目的文件夹。

Here is my structure:

这是我的结构:

  1. Workspace:
    • ProjectA
      • projectA1
        • ...
        • ...
        • here I want to have access to B1.java
        • 在这里,我想访问B1.java
        • build.gradle
        • 的build.gradle
      • projectA1 ...在这里我想访问B1.java build.gradle
      • projectA2
        • ...
        • ...
        • build.gradle
        • 的build.gradle
      • projectA2 ... build.gradle
    • ProjectA projectA1 ...在这里我想访问B1.java build.gradle projectA2 ... build.gradle
    • ProjectB
      • projectB1
        • B1.java
        • B1.java
        • ...
        • ...
        • build.gradle
        • 的build.gradle
      • projectB1 B1.java ... build.gradle
      • projectB2
        • ...
        • ...
        • build.gradle
        • 的build.gradle
      • projectB2 ... build.gradle
      • build.gradle
      • 的build.gradle
    • ProjectB projectB1 B1.java ... build.gradle projectB2 ... build.gradle build.gradle
  2. 工作区:ProjectA projectA1 ...在这里我想访问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!

我试图阅读gradle文档,但对我来说并不清楚。任何帮助赞赏。谢谢!

2 个解决方案

#1


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.

除非项目A1和B1存在于同一个源存储库中并且一起签出和签入,否则您实际上应该依赖项目B1作为外部依赖项。

in Project A1 build.gradle:

在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.

当然,为了实现这一点,您必须首先将B1二进制文件发布到可从Project A1访问的存储库。这可以是外部nexus / artifactory类型的maven存储库,但也可以是本地maven .m2缓存,甚至是普通的旧文件系统。对于maven发布,请参阅maven或'maven-publish`插件。


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.

如果两个项目都位于同一个源代码库中,则应将ProjectA和ProjectB组织为根“容器”项目下的子项目。根项目不需要拥有自己的源代码。

Read about organizing multi-project builds in gradle here.

阅读有关在gradle中组织多项目构建的信息。

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:

如果根项目具有包含项目B1的包含行的settings.gradle,则可以引用根项目下的任何项目,如下所示:

project(':B1')

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

所以,在A1的build.gradle中添加B1作为A1的依赖项:

compile project('B1')

#2


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

1)在项目projectA / settings.gradle中

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

2) Add dependency in build.gradle of projectA1 module

2)在projectA1模块的build.gradle中添加依赖项

dependencies {
    compile project(':projectB1')
}

#1


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.

除非项目A1和B1存在于同一个源存储库中并且一起签出和签入,否则您实际上应该依赖项目B1作为外部依赖项。

in Project A1 build.gradle:

在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.

当然,为了实现这一点,您必须首先将B1二进制文件发布到可从Project A1访问的存储库。这可以是外部nexus / artifactory类型的maven存储库,但也可以是本地maven .m2缓存,甚至是普通的旧文件系统。对于maven发布,请参阅maven或'maven-publish`插件。


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.

如果两个项目都位于同一个源代码库中,则应将ProjectA和ProjectB组织为根“容器”项目下的子项目。根项目不需要拥有自己的源代码。

Read about organizing multi-project builds in gradle here.

阅读有关在gradle中组织多项目构建的信息。

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:

如果根项目具有包含项目B1的包含行的settings.gradle,则可以引用根项目下的任何项目,如下所示:

project(':B1')

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

所以,在A1的build.gradle中添加B1作为A1的依赖项:

compile project('B1')

#2


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

1)在项目projectA / settings.gradle中

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

2) Add dependency in build.gradle of projectA1 module

2)在projectA1模块的build.gradle中添加依赖项

dependencies {
    compile project(':projectB1')
}