Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle configuration for multi-module project with shared dependencies

Tags:

java

gradle

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

like image 847
Aliaksei Lithium Avatar asked Jan 01 '14 19:01

Aliaksei Lithium


People also ask

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.


1 Answers

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.

like image 118
Peter Niederwieser Avatar answered Oct 17 '22 17:10

Peter Niederwieser