xrabbit
xrabbit

Reputation: 735

Get dependencies from subproject within root project

Can't get the subproject dependencies in root build file. They isn't initialized for some reasons. Is there a possibility to get list of dependencies from project1 for example inside root ?

Pls see copyToLib task. It should return values from dependencies section of project1, but actually it's not.

Gradle version is 2.0

Root
|
|-project1
|    |-build.gradle
|-project2
|    |-build.gradle
|-core1
|    |-build.gradle
|-core2
|    |-build.gradle
|-build.gradle
|-settings.gradle

settings.gradle

include 'project1','project2','core1','core2'
rootProject.name="Root"

project1:build.gradle

version = '0.1'
dependencies {
    compile project(':core1'),
            project(':core2'),
            'org.springframework:spring-web:4.0.6.RELEASE'
}

root:build.gradle

subprojects{
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply from: "${rootProject.projectDir}/eclipse.gradle"
    sourceCompatibility = 1.7
    repositories {
         mavenCentral()
    }
    task copyToLib(type: Copy) {
        def deps = project.configurations.compile.getAllDependencies().withType(ProjectDependency)
        for ( dep in deps ) {
            println dep.getDependencyProject().projectDir
        }
    }
    build.dependsOn(copyToLib)
}

Upvotes: 3

Views: 2999

Answers (1)

Opal
Opal

Reputation: 84756

When project variable is used in subprojects it refers to the current project - in this particular case - root.

You need to remove adding the task from subprojects closure and add the following piece of code:

subprojects.each { p ->
    configure(p) {
     task copyToLib << {
       def deps = p.configurations.compile.allDependencies.withType(ProjectDependency)
         for ( dep in deps ) {
           println dep.getDependencyProject().projectDir
         }
        }
        p.build.dependsOn(copyToLib)
    }
}

Have simplified the task (it's not of type Copy) - You need to differentiate task action from configuration.

Upvotes: 2

Related Questions