简体   繁体   English

试图将Gradle项目包含为依赖项,但include()无法正常工作

[英]Trying to include Gradle project as dependency, but include() not working

I have a Gradle project which depends on another Gradle project. 我有一个Gradle项目,它依赖于另一个Gradle项目。 My project structure is like this: 我的项目结构如下:

  • project1 专案1
    • build.gradle build.gradle
    • settings.gradle settings.gradle
    • src/ src /
  • project2 项目2
    • build.gradle build.gradle
    • settings.gradle settings.gradle
    • src/ src /

in project1/build.gradle I want to add project2 as a dependency: project1/build.gradle我想添加project2作为依赖项:

version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDirs = [ 'src' ]
        }
    }
}

include ':project2'
project(':project2').projectDir = new File("../project2")

dependencies {
    compile project(':project2')
}

Unfortunately, I'm always getting this error message: 不幸的是,我总是收到以下错误消息:

Error:(21, 0) Could not find method include() for arguments [:project2] on root project 'project1' of type org.gradle.api.Project. 错误:(21,0)在类型为org.gradle.api.Project的根项目'project1'上找不到参数[:project2]的方法include()。

I'm using Gradle 3.5 and I'm getting this error both on the command line ( gradle build ) and in IntelliJ. 我正在使用Gradle 3.5,并且在命令行( gradle build )和IntelliJ中都收到此错误。 I found a few StackOverflow threads about the same issue ( this and this ), but they were not helpful. 我发现了一些关于同一问题( thisthis )的StackOverflow线程,但是它们没有帮助。

The Gradle multi-project documentation also doesn't mention any specific requirements which I may be missing that can cause the error. Gradle多项目文档也没有提及我可能会遗漏的可能导致错误的任何特定要求。

When I leave the include call out, I get the message that the project path could not be found in the root project. 当我退出include调用时,我收到一条消息,指出在根项目中找不到项目路径。

I also tried moving the dependency to a subdirectory of project1 , but without success. 我还尝试将依赖项移至project1的子目录,但未成功。

I wonder what I'm doing wrong and why apparently not many other people are having the same problem. 我想知道我在做什么错,为什么其他人显然没有遇到相同的问题。 I'd be grateful for hints. 我会很感激的提示。

Note: this is not an Android project. 注意:这不是一个Android项目。

As pointed out in the first comment, include actually needs to go into settings.gradle and not into build.gradle . 如第一条评论中所述, include实际上需要进入settings.gradle而不是build.gradle The same applies to changing the projectDir property. 更改projectDir属性也是projectDir

Comment 3 gave also me another idea. 评论3也给了我另一个想法。 The project can be included in settings.gradle as follows: 该项目可以包含在settings.gradle ,如下所示:

includeBuild '../project2'

and in project1/build.gradle I specify the dependency as project1/build.gradle我将依赖项指定为

dependencies {
    compile 'group:project2:version'
}

I generally like this better, since it's less code and looks cleaner. 我通常会更好,因为它更少的代码并且看起来更干净。 The downside, however, is that recursive composite builds aren't possible. 但是不利的是,递归复合构建是不可能的。 So if project2 itself is also a composite build, this won't work. 因此,如果project2本身也是复合构建,则将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM