How to exclude a child 'project dependency' from parent when we add parent into another project?

Example:
This is a multi project build
==================================================================
Test
settings.gradle:
include “:Test-service”, “:Test-persistence”,":Test-security"
==================================================================
Test-service
build.gradle:
dependencies {
compile project (’:Test-persistence’)
compile project (’:Test-security’)
other dependencies
}
==================================================================
Test-security
build.gradle:
dependencies {
compile project (’:Test-persistence’)
other dependencies
==================================================================
Test-persistence
build.gradle:
dependencies {
other dependencies
}
==================================================================

How to exclude "Test-persistence" from "Test-security" when I add it in "Test-service" ?

I’m not sure I understand. Do you want to exclude the transitive dependency of ‘Test-security’? If so, how would that be any different since you are explicitly adding a dependency to ‘Test-persistence’?

Hi Mark,

Thanks for your reply. There was a mistake in the example. I edited that. For compilation, Test-security and Test-service both use Test-persistence. Also, Test-service uses Test-security. When I am trying to use Test-security in Test-service, I want to exclude Test-persistence which is present in Test-security.

What are you trying to accomplish? Why do you want to exclude that particular project dependency? Excluding ‘Test-persistence’ from ‘Test-security’ will have the same final result since you are explicitly depending on it.

Hi Mark,

Sorry for the delayed response. I want to exclude Test-persistence from Test-security because when Test-service is getting loaded, the Application is trying to register entity manager for Hibernate twice as it is present in both Test-security and Test-service. Also, I wanted to learn how to exclude a project dependency.

That shouldn’t be the case. The JAR should only be on the classpath once. In any case, you can exclude a project dependency like any other.

    compile(project (':Test-security')) {
        exclude module: 'Test-persistence'
    }

Hi Mark,

I have tried the above approach, but the issue is still exists. Can you please help me to resolve this?