如何在父文件夹的多模块Gradle项目中声明公共依赖项

如何在父文件夹的多模块Gradle项目中声明公共依赖项

问题描述:

我有多模块gradle项目.假设项目 parent 具有模块 module1 module2 .

I've multi-module gradle project. Let's say project parent has modules module1 and module2.

我也在父项目的 settings.gradle 中同时包含了这两个模块.

I have included both the modules in settings.gradle in the parent project as well.

当我在父项目的 build.gradle 中声明公共依赖项时,两个项目都没有编译,但是当我在每个模块的 build.gradle 中添加依赖项时则模块编译成功.

When I declare the common dependencies in build.gradle of parent project, both the project is not compiling but when I add the dependencies in build.gradle of each module then the modules are compiling successfully.

任何想法我该怎么做.

您可以使用父模块中的allprojects语句为项目中的所有模块声明依赖项.这是在父build.gradle文件中执行此操作的基本方法:

You can declare dependencies for all modules in your project using the allprojects statement in your parent module. This is the essential way of doing so in your parent build.gradle file:

allprojects {
    // Plugins can go here, example:
    apply plugin: 'java'

    dependencies {
        // Dependencies go here, example:
        compile group: 'org.apache.shiro', name: 'shiro-core', version: '1.4.0'
    }
}

检查答案,以更深入地了解多项目的配置点.
还可以查看有关该主题的Gradle 文档进行深入了解

Check this and this answer for more insight on the configuration point of a multi project.
Also have a look at the Gradle documentation about this topic for a deep dive.