Use centralised repository configuration in sub projects

Hi,

Following the recommendations here
https://docs.gradle.org/current/userguide/platforms.html#sec:sharing-catalogs
and
https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration

I’ve put the maven repositories configuration in /settings.gradle

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        mavenLocal()
        maven {
            name "my repo"
            url "https://...."
           }
        mavenCentral()
    }

    versionCatalogs {
        libs {
            library('my-lib', '...')
            library('my-lib2', '...')
        }
    }
}

This work well but for publishing.
Now in sub-projects I would like to benefit from this centralised configuration.
Unfortunately unless I (re)define the repos in the subproject build file in (publishing task). the publish
task do nothing as no repository is defined (I guess)

publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'foo'
            artifactId = 'myartifact'
            version = 1.0
            from components.java
            pom {
                name = '..'
                description = '...'
                url = '....'
            }
        }
    }
    // <--- unless. I add again a repositories {} close here, nothing happen  
}

I would greatly appreciate some guidance .

regards

This centralized declaration - as the name of the enclosing block suggests - is just for the repositories used to resolve dependencies, not for publishing.

Maybe you want to write some convention plugin that defines your repository for publishing that you can then use in the projects that should be published.

Btw. you should strongly consider not to use mavenLocal, especially not as first repository and without any content filter. Like this you make you build much slower than necessary and additionally you make it potentially flaky and unpredictable, as mavenLocal as invented by Maven is heavily broken by design.

Hi,
Thank for the reply.
Indeed as I do not want to duplicate information I would move the repository
declaration in my convention plugin.

The two things I am still struggling with (sorry to be slow , but I am learning at the same time)
#1 - how do I reference the repository with the publishing block? I failed trying this.
something like

publishing {
    publications {
        maven(MavenPublication) {
            ...
            pom {
               ...
            }
        }
    }
   repositories.add(<find the right one by name>)
}

#2 - how do I deal with credentials ? they are passed by properties at ‘root level’
Shall consumed them in the convention plugin or with the publish task ?

thank again.

PS ;I’ll keep in mind your comment on mavenLocal . thx!

how do I reference the repository with the publishing block? I failed trying this.

Show what you tried and you might get help. :slight_smile:
But generally said, you don’t.
Your convention plugin sets it, either after applying the publishing plugin, or in reaction to it being applied using pluginManager.withPlugin.
And your project then applies the convention plugin and lets it do its work.

how do I deal with credentials ? they are passed by properties at ‘root level’

https://docs.gradle.org/current/userguide/declaring_repositories.html#sec:handling_credentials

Hi,

in my convention plugin I have

repositories {
    mavenLocal()
    maven {
        name "my-great-repo"
        url "..."
    }
    mavenCentral()
}

in my sub project foo I have in …/foo/build.gradle

ublishing {
    publications {
        maven(MavenPublication) {
            groupId = 'xxx'
            artifactId = 'xxxx'
            version = version
            from components.java
            pom {
                name = '....'
                description = '...'
                url = '...'
            }
        }
    }
}

like this , executing “gradle publish” nothing happen . i.e nothing get published
If I duplicate the repository it works

ublishing {
    publications {
        maven(MavenPublication) {
            groupId = 'xxx'
            artifactId = 'xxxx'
            version = version
            from components.java
            pom {
                name = '....'
                description = '...'
                url = '...'
            }
        }
    }
    repositories {
       maven {
        name "my-great-repo"
        url "..."
    }
   }
}

doing this I can publish my artifacts.
By the way I see new tasks being defined foo:publishMavenPublicationTomy-great-repoRepository

I want to avoid duplicating the maven repository information .
thx in advance

Your convention plugin does the same, the settings script was doing. You just define the repositories for resolving dependencies. You need to configure the publishing repositories in your conventions plugin as I suggested earlier already.

Hi,

Thank you
I got it now. I was looking at things the other way around

thank again
regards