简体   繁体   中英

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

I have a Gradle project which depends on another Gradle project. My project structure is like this:

  • project1
    • build.gradle
    • settings.gradle
    • src/
  • project2
    • build.gradle
    • settings.gradle
    • src/

in project1/build.gradle I want to add project2 as a dependency:

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.

I'm using Gradle 3.5 and I'm getting this error both on the command line ( gradle build ) and in IntelliJ. I found a few StackOverflow threads about the same issue ( this and this ), but they were not helpful.

The Gradle multi-project documentation also doesn't mention any specific requirements which I may be missing that can cause the error.

When I leave the include call out, I get the message that the project path could not be found in the root project.

I also tried moving the dependency to a subdirectory of project1 , but without success.

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.

As pointed out in the first comment, include actually needs to go into settings.gradle and not into build.gradle . The same applies to changing the projectDir property.

Comment 3 gave also me another idea. The project can be included in settings.gradle as follows:

includeBuild '../project2'

and in project1/build.gradle I specify the dependency as

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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