简体   繁体   中英

gradle circular dependency with external project

I have a fairly simple setup, that does not work and I cannot work out why:

the folder structure is as follows:

/dependency
    /build.gradle
    /settings.gradle
    /src/main/...

/Mainproject
    /build.gradle
    /settings.gradle

    /Subproject_1
        /build.gradle
        /src/main...

     /Subproject_2
        /build.gradle
        /src/main...

I want the Subproject to depend on "dependency" locally for quick testing. so I stuck to the manual and did:

/Mainproject/settings.gradle:

include "Subproject_1", "Subproject_2", "dependency"
project(":dependency").projectDir = file('../dependency')

/Mainproject/build.gradle:

allprojects {
    apply plugin: 'java'
    dependencies {
        compile project(path: ':dependency')
    }
}

dependencies {
    compile project(':Subproject_1')
    compile project(':Subproject_2')
}

/dependency/build.gradle:

version '1.0'
apply plugin: 'java'

repositories {
    maven {
        url "http://...."
    }
}

dependencies {
    compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
    compile group: 'javax', name: 'javaee-api', version: '7.0'
}

jar {
    manifest {
        attributes 'Implementation-Title': 'Archive delegation dispatcher classes',
                'Implementation-Version': project.version
    }
}

The build.gradle files of Subproject_1 and _2 are empty. The settings.gradle file of dependency is empty.

When i gradle build MainProject i get:

Circular dependency between the following tasks:
:dependency:classes
\--- :dependency:compileJava
     \--- :dependency:jar
          \--- :dependency:classes (*)

(*) - details omitted (listed previously)

And I cannot get my head around why that would be.

Any hints?

When you include "dependency" in your Mainproject's settings.gradle file, you are making the "dependency" project a subproject of "Mainproject".

Then this block in your Mainproject's build.gradle file defines "dependency" as a compile dependency of all subprojects.

allProjects {
    dependencies {
        compile project(path: ':dependency')
    }
}

Since the "dependency" project is also a subproject, you have a circular dependency defined where the "dependency" project depends on itself.

Instead, try creating a settings.gradle file for each of the subprojects with the following:

include "dependency"
project(":dependency").projectDir = file('../dependency')

Then modify your settings.gradle file for the Mainproject to look like this:

include "Subproject_1", "Subproject_2"

You've stated that allProjects have a dependency on the project dependency as seen here:

allprojects {
    apply plugin: 'java'
    dependencies {
        compile project(path: ':dependency')
    }
}

You need this to only apply to your projects that aren't dependency . You can do that by excluding it when applying dependencies, like this

subprojects { project ->
    if (project.name != "dependency") {
        apply plugin: 'java'
        dependencies {
            compile project(path: ':dependency')
        }
    }
}

Because dependencies itself. Move it must be OK.

Turning off Instant run worked for me, could work for someone else too. I had some changes related to gradle and my application appeared not to work after it.

build.gradle (:shared)

This is because you are trying to include a module inside of yourself.

From:

dependencies {
    implementation project(':shared')
    ...
}

To:

dependencies {
    ...
}

GL

Source

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