In build.gradle how to extract and reuse Maven repository configuration?
Asked Answered
C

2

6

I've got this build.gradle file:

repositories {
    maven {
        credentials {
            username "$artifactory_user"
            password "$artifactory_password"
        }
        url 'http://some.domain/artifactory/repo'
    }
}

publishing {
    repositories {
        publications {
            maven(MavenPublication) {
                from components.java
            }
        }
        maven {
            credentials {
                username "$artifactory_user"
                password "$artifactory_password"
            }
            url 'http://some.domain/artifactory/repo'
        }
    }
}

I want to extract and reuse the Maven repository definition.

This is not working:

repositories {
    mavenRepository()
}

publishing {
    repositories {
        publications {
            maven(MavenPublication) {
                from components.java
            }
        }
        mavenRepository()
    }
}

private void mavenRepository() {
    maven {
        credentials {
            username "$artifactory_user"
            password "$artifactory_password"
        }
        url 'http://some.domain/artifactory/repo'
    }
}

It results in

Could not find method mavenRepository() for arguments [gradle-dev] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.

How can I achieve this goal?

Capacitor answered 23/1, 2019 at 15:32 Comment(2)
see similar question with some solutions : https://mcmap.net/q/1915004/-refactor-maven-block-in-gradleAddieaddiego
Possible duplicate of Refactor maven block in gradleAddieaddiego
P
3

lets say you have a separate repositories.gradle file and name your maven repo..

maven {
    name 'myrepo'
    url 'https://xxxx'
    credentials {
        username = System.getenv("xxx") ?: "${xxx}"
        password = System.getenv("xxx") ?: "${xxx}"
    }
}
maven { name 'anotherrepo' }

then at root project level you can run:

apply from: "$rootDir/gradle/repositories.gradle"

This will populate repositories with all repos. Then to use populate a specific one somehwere else, you can use:

repositories.add(rootProject.repositories.getByName('myrepo'))

I found the answer on this blog page: https://themightyprogrammer.dev/snippet/reusing-gradle-repo thanks to mightprogrammer!

Pennate answered 14/1, 2021 at 12:34 Comment(0)
A
0

Using Gradle 8, if you have a root build.gradle.kts and a subproject build.gradle.kts, in the root you can define the repo like:

subprojects {
   publishing {
      repositories {
         maven {
            name = "MyRepo"
            ... 
         }
      }
   }
}

and the subproject's build.gradle.kts

publishing {
   publications {
      create<MavenPublication>("maven") {
         groupId = ...
         ...
      }
   }
}

Your gradle <submodule>:tasks should now include a publishMavenPublicationsToMyRepoRepository task

Ameline answered 9/4, 2024 at 14:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.