Java 具有共享依赖项的多模块项目的 Gradle 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20872410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:55:28  来源:igfitidea点击:

Gradle configuration for multi-module project with shared dependencies

javagradle

提问by Aliaksei Lithium

Make a first project with gradle, so i look at spring, gradle, hibernate projects how they organize gradle files, and start make my own. But, can't find mistake, why my configuration doesn't work. (sub-projects cant resolve dependency) So project tree:

使用 gradle 创建第一个项目,所以我查看了 spring、gradle、hibernate 项目如何组织 gradle 文件,然后开始制作我自己的项目。但是,找不到错误,为什么我的配置不起作用。(子项目无法解决依赖关系)所以项目树

Root project 'foobar'
+--- Project ':foobar-app'
| +--- Project ':foobar-app:people'
| | +--- Project ':foobar-app:people:people-api'
| | +--- Project ':foobar-app:people:people-core'
| | +--- Project ':foobar-app:people:people-data'
| | \--- Project ':foobar-app:people:people-rest'
| \--- Project ':foobar-app:realtor'
+--- Project ':foobar-starter'
\--- Project ':foobar-system'
+--- Project ':foobar-system:foobar-system-jpa'
+--- Project ':foobar-system:foobar-system-neo4j'
+--- Project ':foobar-system:foobar-system-persistence'
+--- Project ':foobar-system:foobar-system-repository'
\--- Project ':foobar-system:foobar-system-tx'

The problem, when i make foobar-system-jpa.gradle file with set of dependences, etc, it's not work - no dependecies can be reach for this module. But when in root 'foobar' build.gradle make a set of deps for 'foobar-system-jpa' subproject - it' work fine. Why?

问题是,当我使用一组依赖项等制作 foobar-system-jpa.gradle 文件时,它不起作用 - 此模块无法访问任何依赖项。但是当在 root 'foobar' build.gradle 中为 'foobar-system-jpa' 子项目创建一组 deps - 它'工作正常。为什么?

My root build.gradle:

我的根build.gradle

configurations.all {
    // check for updates every build
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

configure(allprojects) { project ->
    group = 'com.foobar'
    version = '0.0.1-SNAPSHOT'

    apply from: "${rootDir}/dependencies.gradle"

    apply plugin: 'maven'
    apply plugin: 'idea'

    ext.gradleScriptDir = "${rootProject.projectDir}/gradle"

    apply from: "${gradleScriptDir}/task.gradle"

}

configure(subprojects) { subproject ->
    apply plugin: 'java'

    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    apply from: "${rootDir}/dependencies.gradle"

    repositories {
        mavenCentral()

        maven { url "http://m2.neo4j.org/" }
        maven { url "http://repo.maven.apache.org/maven2" }
    }


    dependencies {
        testCompile (libs.junit)
        testCompile (libs.mockito)
        testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
    }

}

my dependecies.gradle

我的依赖.gradle

ext {
    springVersion = "4.0.0.RELEASE"
    jUnitVersion = "4.11"
    hibernateVersion = "4.2.1.Final"

    libs = [
            //springframework
            spring_core: "org.springframework:spring-core:$springVersion",
            spring_orm: "org.springframework:spring-orm:$springVersion",
          ......
            hibernate_entitymanager: "org.hibernate:hibernate-entitymanager:$hibernateVersion",
            hibernate_persistence: "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final",
            //database
            postgresql : "org.postgresql:postgresql:9.3-1100-jdbc41",
            commons_dbcp : "commons-dbcp:commons-dbcp:1.4",
           .....

    ]
}

and my settings.gradle

和我的settings.gradle

rootProject.name = 'foobar'
include ':foobar-system:foobar-system-jpa'
....
include ':foobar-app:people:people-rest'
include ':foobar-app:people:people-core'
....
include ':foobar-starter'

project(':foobar-system:foobar-system-jpa').projectDir = "$rootDir/foobar-system/foobar-system-jpa" as File
project(':foobar-system:foobar-system-repository').projectDir = "$rootDir/foobar-system/foobar-system-repository" as File
.....
project(':foobar-app').projectDir = "$rootDir/foobar-app" as File
project(':foobar-starter').projectDir = "$rootDir/foobar-starter" as File

rootProject.children.each { project ->
    project.buildFileName = "${project.name}.gradle"
    assert project.projectDir.isDirectory()
    assert project.buildFile.exists()
    assert project.buildFile.isFile()
}

So, foobar-system-jpa.gradle that NOT work(module can not compile, cause not found any dependency)

因此, foobar-system-jpa.gradle不起作用(模块无法编译,导致未找到任何依赖项)

dependencies {
    compile libs.spring_orm
    compile (libs.spring_data_jpa) {
        exclude(module: 'spring-jdbc')
        exclude(module: 'spring-orm')
    }
    compile libs.hibernate_entitymanager
    compile libs.postgresql
    compile libs.commons_dbcp
    compile project(':foobar-system:foobar-system-tx')
}

Thank you for reply. For inspiration a take a look for this project https://github.com/hibernate/hibernate-orm/blob/master/hibernate-entitymanager/hibernate-entitymanager.gradle

谢谢你的回复。如需灵感,请查看此项目https://github.com/hibernate/hibernate-orm/blob/master/hibernate-entitymanager/hibernate-entitymanager.gradle

UPD: remove some not necessary code

UPD:删除一些不需要的代码

采纳答案by Peter Niederwieser

There is a problem with your settings.gradle. It is configuring buildFileNamefor rootProject.children, which are the immediatechildren of the root project. As a result, all build scripts for projects that are more than one level deep will go unnoticed.

您的settings.gradle. 它正在配置buildFileNamefor rootProject.children,它们是根项目的直接子项。因此,所有深度超过一层的项目的构建脚本都不会被注意到。

To solve this problem, you can either set build script names explicitly, or write a method that sets them recursively.

要解决此问题,您可以显式设置构建脚本名称,也可以编写递归设置它们的方法。

PS: Always try to come up with a minimal failing example, rather than posting pages of code.

PS:总是尝试提出一个最小的失败示例,而不是发布代码页面。