Repositories in subprojects


(Émile Fugulin) #1

Hi,

I fairly new to gradle and I got this question that I wasn’t able to find an answer.
My main build.gradle contains a subprojects block with general repositories block.
Something like:

subprojects {
    repositories {
        mavenCentral()
    }
}

One of my subproject requires another repository for one of its dependencies.
I tried adding a repositories block in its build.gradle like:

repositories {
    maven {
        url 'https://somemavenrepo.com'
    }
}

dependencies {
    //... dependency that requires the additional repository 
}

But the build fail and the logs tell me that gradle seems to ignore the url since it doesn’t try it…
Is it expected? Is there a way to make this work?
For information, I use gradle 4.

Thanks!


(Pierre) #2

Hi,

I think you did some mistake somewhere, because it seems to work on my side. I setup a maven repo with docker:

version: "3"

volumes:
    nexus: {}

services:
    nexus:
        image: sonatype/nexus3:3.5.0
        container_name: nexus
        ports:
            - "8081:8081"
        volumes:
            - nexus:/nexus-data

Start it with docker-compose up -d
Upload a project named foo with:

apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'foo'
version = '1'

publishing {
    publications {
        fooLib(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url "http://localhost:8081/repository/maven-${version.endsWith('-SNAPSHOT') ? 'snapshots' : 'releases'}"
            credentials {
                username = 'admin'
                password = 'admin123'
            }
        }
    }
}

Create a multi-project with 2 subprojects: priv and pub.

// rootProject build.gradle
subprojects {
    apply plugin: 'java'
    repositories {
        mavenCentral()
    }
    test {
        testLogging {
            events 'passed', 'skipped', 'failed'
        }
    }
}

In pub, create a unit test asserting that true is true.

// pub build.gradle
dependencies {
    testCompile 'junit:junit:4.12'
}

In priv, test a class packaged in foo:

// priv build.gradle
repositories {
    maven {
        url 'http://localhost:8081/repository/maven-public/'
    }
}
dependencies {
    compile 'foo:foo:1'
    testCompile 'junit:junit:4.12'
}

Build the project:

me@here:/tmp/foo/consume$ gradle test
Download http://localhost:8081/repository/maven-public/foo/foo/1/foo-1.pom
Download http://localhost:8081/repository/maven-public/foo/foo/1/foo-1.jar

> Task :priv:test

FooTest > should_access_foo PASSED

> Task :pub:test

TrueTest > should_be_true PASSED


BUILD SUCCESSFUL in 3s

You probably made some typo somewhere else in your project configuration.


(Stefan Oehme) #3

Keep in mind that repositories are only used in the project they are declared in. My best guess is that you are transitively leaking a dependency from that repo into another subproject which doesn’t have that repository. Your build then fails for that other project, because it will (by design) only look in its own repos.


(Émile Fugulin) #4

Thanks for the replies!

@st_oehme that would make sense since other projects depend on this one.
We used to use maven so that dependency might have been leaked transitively yes.
I will double check that and get back to you.