Gradle Local Project Dependency

2019-06-22 16:26发布

问题:

I have 2 Gradle projects both inside the same directory. The directory structure is as follows:

ParentDirectory\
    GradleProjectA\
        build.gradle
    GradleProjectB\
        settings.gradle
        build.gradle

I want to add GradleProjectA as a dependency to GradleProjectB. In the settings.gradle for GradleProjectB, I've tried adding include 'GradleProjectA' and then in build.gradle: compile project(':GradleProjectA') but that didn't work.

Any help would be greatly appreciated. Thanks.

回答1:

The way I did something like this is as follows:

GradleProjectB/settings.gradle:

include ':GradleProjectA'
project(':GradleProjectA').projectDir = new File('../GradleProjectA')

GradleProjectB/build.gradle:

compile project(":GradleProjectA")


回答2:

In the latest version of Gradle, you can use Composite Builds, like that:

In GradleProjectB's settings.gradle, add the following line:

includeBuild "../GradleProjectA"

It will automatically handle dependency collisions and all that stuff.



回答3:

The settings.gradle file needs to be in the parent directory specifying both.

Try the following format:

ParentDirectory\ 
    build.gradle
    settings.gradle <-- include 'GradleProjectA', 'GradleProjectB'
    GradleProjectA/
        build.gradle
    GradleProjectB/
        build.gradle

Edit: Ok if your Parent Directory is not a build directory then you can do the following:

in your gradle project b settings.gradle file try the following:

includeFlat("GradleProjectA") // Include the project

in your b build.gradle:

compile project(":GradleProjectA")

includeFlat reference



回答4:

If GradleProjectA and GradleProjectB are part of one multi-project Gradle build, then you probably want the settings.gradle in ParentDirectory and there include A and B. The way you use settings.gradle is not correct. Maybe you should re-read the section about multi-project gradle builds in the User Guide.

If the two projects are indiviual projects but you still want to depend on A from B, you might prefer to build A, release it to some repository (possibly the mavenLocal repository) and then depend on the built artifacts from project B.



回答5:

The following should do the trick:

root
+-- projectA
|   +-- build.gradle
|       => dependencies { compile project(':common') }
|   +-- settings.gradle 
|       => include ':common'
|          project(':common').projectDir = new File('../common')
+-- projectB
|   => same as projectA
+-- common
    +-- build.gradle
        => regular build, probably with a "jar" section


标签: java gradle