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

2

23

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.

Rea answered 11/3, 2013 at 19:52 Comment(0)
H
26

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.

Humidify answered 12/3, 2013 at 12:48 Comment(1)
Great thanks! Now everything builds. Used project.evaluationDependsOn() property. I guess that solution is dirty, but I have to stick to it for a while.Rea
K
0

I had a similar error happen to me in a multimodule project, but for me the cause was as simple as I had forgotten to apply the java-library plugin within the configurations, I only had maven-publish plugin in use.

Once I added the plugin, sourceSets was found normally:

configure(subprojects) {
apply plugin: 'maven-publish'
apply plugin: 'java-library'
....
Kimberliekimberlin answered 5/10, 2022 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.