具有共享依赖性的多模块项目的Gradle配置 [英] Gradle configuration for multi-module project with shared dependencies

查看:304
本文介绍了具有共享依赖性的多模块项目的Gradle配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 根项目' foob​​ar'
+ --- Project':foobar-app'
| + --- Project':foobar-app:people'
| | + ---项目':foobar-app:people:people-api'
| | + ---项目':foobar-app:people:people-core'
| | + ---项目':foobar-app:people:people-data'
| | \ --- Project':foobar-app:people:people-rest'
| \ --- Project':foobar-app:realtor'
+ ---项目':foobar-starter'
\ ---项目':foobar-system'
+ ---项目':foobar-system:foobar-system-jpa'
+ ---项目':foobar-system:foobar-system-neo4j'
+ ---项目':foobar-系统:foobar-system-persistence'
+ ---项目':foobar-system:foobar-system-repository'
\ ---项目':foobar-system:foobar-system-tx '

问题在于,当我使用foobar-system-jpa.gradle文件设置相关性时,等,这是行不通的 - 这个模块没有依赖。但是当在'foobar'build.gradle中为'foobar-system-jpa'子项目设置一组代码时 - 它工作正常。为什么?

我的根 build.gradle

  configurations.all {
//检查每个构建的更新
resolutionStrategy.cacheChangingModulesFor 0,'seconds'
}

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

适用于:$ {rootDir} /dependencies.gradle

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

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

apply from:$ {gradleScriptDir} /task.gradle

}

configure(subprojects){subproject - >
应用插件:'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7
适用于:$ {rootDir} /dependencies.gradle

存储库{
mavenCentral()

maven {urlhttp://m2.neo4j.org/}
maven {urlhttp:// repo。 maven.apache.org/maven2}
}


依赖关系{
testCompile(libs.junit)
testCompile(libs.mockito)
testCompile组:'org.hamcrest',名称:'hamcrest-library',版本:'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.springframewor k: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,
//数据库
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文件
项目(':foobar-system:foobar-system-repository')。projectDir =$ rootDir / foobar-system / foobar-system-jpa .projectDir =$ rootDir / foobar-system / foobar-system-repositoryas File
.....
project(':foobar-app')。projectDir =$ rootDir / foobar-应用程序作为文件
项目(':foobar-starter')。projectDir =$ rootDir / foobar-starter作为文件

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

所以,foobar-system-jpa.gradle (无法编译,导致未找到任何依赖项)

 依赖关系{
编译libs.spring_orm
compile(libs.spring_data_jpa){
exclude(module:'spring-jdbc')
exclude(module:'spring-orm')
}
编译libs.hibernate_entitymanager
编译libs.postgresql
编译libs.commonsmons_dbcp
编译项目(':foobar-system:foobar-system-tx')
}

感谢您的回复。为获得灵感,请查看此项目 https://github.com /html/hibernate/hibernate-orm/blob/master/hibernate-entitymanager/hibernate-entitymanager.gradle



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

解决方案

您的 settings.gradle 存在问题。它为 rootProject.children 配置 buildFileName ,它们是根目录的 immediate 子目录项目。因此,对于超过一级深度的项目,所有构建脚本都将被忽略。



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



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

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:

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?

My root 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

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

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)

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

UPD: remove some not necessary code

解决方案

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

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.

这篇关于具有共享依赖性的多模块项目的Gradle配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆