繁体   English   中英

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

[英]Gradle configuration for multi-module project with shared dependencies

使用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'

问题是,当我用一组依赖项等制作foobar-system-jpa.gradle文件时,它不起作用-此模块无法达到依赖项。 但是,当在根目录“ foobar”中,build.gradle为“ foobar-system-jpa”子项目设置一组deps时,它就可以正常工作。 为什么?

我的根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'
    }

}

我的dependecies.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",
           .....

    ]
}

和我的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()
}

因此,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')
}

谢谢您的回复。 为了获得灵感,请看一下这个项目https://github.com/hibernate/hibernate-orm/blob/master/hibernate-entitymanager/hibernate-entitymanager.gradle

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

您的settings.gradle有问题。 它正在为rootProject.children配置buildFileName ,它们是根项目的直接子代。 结果,所有超过一个级别的项目的构建脚本都不会被注意到。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM