How to add other projects in current project in Gradle?

烂漫一生 提交于 2021-01-29 17:57:00

问题


I have two projects ProjA and ProjB, here ProjB is depends on ProjA. So, how to include ProjA in ProjB? And please let me know in case change in the ProjA build.gradle file?


回答1:


Okay, so you have two projects, whereas ProjA depends on ProjB.

If you have already set up a gradle build for those, both of them will have a file settings.gradle, which at least defines the project name and build.gradle, where you can specify the dependencies.

Example: settings.gradle

rootProject.name = 'ProjA'

build.gradle

group = 'mygroup'
version = '1.0'

By further adding one of Gradle's publishing plugins (see Maven publishing or Ivy publishing), you can configure a publication. A publication in turn contains artifacts, that is the files you want to upload (and usually some repositories). Example for a minimal Maven publication:

apply plugin: 'maven-publish'
publishing {
    publications {
        core(MavenPublication) {
            from components.java
        }
    }
}

The maven-publish plugin will add the task publishToMavenLocal (among others) to your project which installs the jar somewhere to ~/.gradle/caches/.

In build.gradle of ProjB you can then define a compile time dependency on ProjA like so:

dependencies {
    compile(group: 'mygroup', name: 'ProjA', version: '1.0')
}



回答2:


If your projects are co-located, you could have a multi build project.

Say you have a root folder with these contents:

  • some root directory
    • project-a
      • project-a.gradle
      • src
    • project-b
      • project-b.gradle
      • src
    • build.gradle
    • settings.gradle

settings.gradle contains:

include 'project-a', 'project-b'

project-b.gradle then depends on project A:

dependencies {
   compile project(':project-a')
}

After this, project a will always compile and assemble before project-b and the jar file for project-a jar will be on the classpath when compiling project-b.



来源:https://stackoverflow.com/questions/21305091/how-to-add-other-projects-in-current-project-in-gradle

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