Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle multiproject gives "Could not find property 'sourceSets' on project" error

Tags:

I had quite good gradle configuration, that built everything just fine. But one of the projects of my multi-project build derived from the rest of them so much, that I would gladly move it to another git repo and configure submodules to handle it.

First, I moved Project and its resources to subfolder Libraries/MovedProject. After altering some lines in gradle configurations it worked fine. But then I decided to write a new build.gradle just for this project, and move all configurations there from the main one.

And this is where everything stopped working. When I try to call any task it always ends with Could not find property 'sourceSets' on project ':Libraries/MovedProject'. Line which is responsible for it is:

dependencies {     ...     if (noEclipseTask) {         testCompile project(':Libraries/MovedLibrary').sourceSets.test.output     } } 

which I use for running tests in which I use classes from other projects. If I remove that line, the build fails only when it reaches compileTestJava task of projects that make use of MovedProject. If I remove that line and call gradle :Libraries/MovedLibrary:properties I can see :

... sourceCompatibility: 1.7 sourceSets: [source set main, source set test] standardOutputCapture: org.gradle.logging.internal.DefaultLoggingManager@1e263938 ...   

while gradle :Libraries/MovedLibrary:build builds correctly.

Currently I've got everything set up as following:

  1. directories:

    • /SomeMainProject1
    • /SomeMainProject2
    • /SomeMainProject3
    • /Libraries
      • /MovedProject
        • build.gradle
        • dependencies.gradle
        • project.gradle
        • tasks.gradle
    • /Builder
      • dependencies.gradle
      • project.gradle
      • tasks.gradle
    • build.gradle
    • settings.gradle
  2. settings.gradle

    include Libraries/MovedProject,         SomeMainProject1,         SomeMainProject2,         SomeMainProject3 
  3. sourceSets for MovedProject are defined in Libraries/MovedProject/project.gradle:

    sourceSets {     main {         java {             srcDir 'src'             srcDir 'resources'         }         resources { srcDir 'resources' }     }     test { java {         srcDir 'test/unit'     } } } 
  4. dependencies that makes use of sourceSets.test.output are stored in Builder/dependancies.gradle, and set for each project that needs MovedProject to run tests:

    project(':SomeMainProject1') {     dependencies {         ...          if (noEclipseTask) {             testCompile project(':Libraries/net.jsdpu').sourceSets.test.output         }     } } 

What would be the easiest way to get rid of that error and make gradle build projects with current directory structure? I would like to understand why gradle cannot see that property.

like image 507
Mateusz Kubuszok Avatar asked Mar 11 '13 19:03

Mateusz Kubuszok


1 Answers

The line in question is problematic because it makes the assumption that project :Libraries/MovedLibrary is evaluated (not executed) before the current project, which may not be the case. And if it's not, the source sets of the other project will not have been configured yet. (There won't even be a sourceSets property because the java-base plugin hasn't been applied yet.)

In general, it's best not to reach out into project models of other projects, especially if they aren't children of the current project. In the case of project A using project B's test code, the recommended solution is to have project B expose a test Jar (via an artifacts {} block) that is then consumed by project A.

If you want to keep things as they are, you may be able to work around the problem by using gradle.projectsEvaluated {} or project.evaluationDependsOn(). See the Gradle Build Language Reference for more information.

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

Peter Niederwieser