Gradle多重项目在项目中给出“Could not find property”sourceSets'错误 [英] Gradle multiproject gives "Could not find property 'sourceSets' on project" error

查看:126
本文介绍了Gradle多重项目在项目中给出“Could not find property”sourceSets'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很好的gradle配置,这一切都很好。但是我的多项目构建项目中的其中一个很大程度上源自其余的项目,我很乐意将它移动到另一个git仓库并配置子模块来处理它。



<首先,我将Project及其资源移至子文件夹 Libraries / MovedProject 。改变gradle配置中的一些行后,它工作正常。但是之后我决定为这个项目编写一个新的build.gradle,并将所有的配置从主目录中移出。



这就是一切停止工作的地方。当我尝试调用任何任务时,它总是以结束
。无法在项目中找到属性'sourceSets':Libraries / MovedProject'
。对此负责的行是:

 依赖关系{
...
if(noEclipseTask){
testCompile项目(':Libraries / MovedLibrary')。sourceSets.test.output
}
}

我用它来运行测试,其中我使用其他项目的类。如果我删除该行,那么构建只有在达到使用MovedProject的项目的compileTestJava任务时才会失败。如果我删除该行并调用 gradle:Libraries / MovedLibrary:properties 我可以看到:

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

while gradle:Libraries / MovedLibrary:build builds。



目前我已经设置了一切如下:


  1. 目录:




    • / SomeMainProject1

    • / SomeMainProject2

    • / SomeMainProject3

    • /图书馆

      • / MovedProject

        • build.gradle

        • dependencies.gradle

        • project.gradle

        • tasks.gradle
        • li>


    • / Builder

      • .gradle

      • project.gradle

      • tasks.gradle


      • build.gradle

      • settings.gradle


    • settings.gradle

        include Libraries / MovedProject,
      SomeMainProject1,
      SomeMainProject2,
      SomeMainProject3


    • MovedProject的sourceSets在 Libraries / MovedProject / project.gradle

        sourceSets {
      main {
      java {
      srcDir'src'
      srcDir'resources'
      }
      resources {srcDir'resources'}
      }
      test {java {
      srcDir'test / unit'
      }}
      }


    • 使用sourceSets.test.output存储在 Builder / dependancies.gradle 中,并为每个需要MovedProject运行测试的项目设置:

       项目(':SomeMainProject1'){
      依赖关系{
      ...

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


最简单的方法是摆脱这种错误并使用当前目录结构进行gradle构建项目?我想了解为什么gradle不能看到这个属性。

解决方案

有问题的行是有问题的,因为它使得项目:Libraries / MovedLibrary 在当前项目之前被评估(未执行),可能并非如此。如果不是,那么另一个项目的源代码集就不会被配置。 (甚至不会有 sourceSets 属性,因为 java-base 插件尚未应用。)

一般来说,最好不要接触其他项目的项目模型,特别是如果他们不是当前项目的子项目。在项目A使用项目B的测试代码的情况下,推荐的解决方案是让项目B公开一个测试Jar(通过 artifacts {} 块),然后由项目A。

如果你想保持原样,你可以通过使用 gradle.projectsEvaluated {}来解决这个问题。 project.evaluationDependsOn()。有关更多信息,请参阅 Gradle Build Language Reference


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.

解决方案

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.

这篇关于Gradle多重项目在项目中给出“Could not find property”sourceSets'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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