How to avoid duplication of children repositories in a parent project

Viewed 1250

I have a multi-project build with the following structure:

Root project 'just-another-root-project'
+--- Project ':producer'
\--- Project ':consumer'

The root settings.gradle file:

rootProject.name = 'just-another-root-project'
include 'consumer', 'producer'

...connects created modules.


The producer.gradle file:

plugins {
    id 'java-library'
}

group 'com.github.yarbshk.jarp'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven {
        url 'http://maven.nuiton.org/release/'
    }
}

dependencies {
    implementation 'com.sun:tools:1.7.0.13'
}

...has an external dependency (com.sun.tools) that is not published in Maven Central therefore I've added a link to the Nuiton repository.


The consumer.gradle file:

plugins {
    id 'java'
}

group 'com.github.yarbshk.jarp'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor project(':producer')
}

The build described above is not working! To make it so I was enforced to duplicate all repositories from producer.gradle into consumer.gradle. So the question is how to build the root project without the excessive dependency duplication? How to do it in the right way? Thanks for any answer or hint :)


UPDATE 1:

I get the following error when try to build the project with files shown above:

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all files for configuration ':consumer:compile'.
> Could not find com.sun:tools:1.7.0.13.
  Searched in the following locations:
      https://repo.maven.apache.org/maven2/com/sun/tools/1.7.0.13/tools-1.7.0.13.pom
      https://repo.maven.apache.org/maven2/com/sun/tools/1.7.0.13/tools-1.7.0.13.jar
  Required by:
      project :consumer > project :producer
2 Answers

You can configure repositories directly in the root project like that:

root project build.gradle:

// configure repositories for all projects
allprojects {
    repositories {
        mavenCentral()
        maven {
            url 'http://maven.nuiton.org/release/'
        }
    }
}

EDIT (from you comment on other response)

You can also define only mavenCentral() repository on root project level (it will be added to repositories for all projects) and configure http://maven.nuiton.org/release repository only for producer subproject :

root project

repositories {
    // will apply to all project
    mavenCentral()
}

producer project

 repositories {
    maven {
        url 'http://maven.nuiton.org/release/'
    }
    // mavenCentral inherited from root project
}

consumer project

// no need to re-define repositories here.
Related