Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining my own custom maven repository in Android gradle build script

I know Maven repositories can be added normally using something similar to the following snippet.

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
    }
}

However, what I would like to achieve is actually creating my own Maven repository entry. So adding to the example above, this might look something like the following.

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        myNewRepo()
    }
}

Note that "myNewRepo" is the new repository and was added to the bottom. If I simply add the string as show above, I get the following error from gradle.

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

My reason for doing this is that I have a list of several of my own Maven repositories and it gets burdensome to have to each of these entries for all of my apps. For example, my current implementation looks like this.

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            url: "some url"
            name: "some name"
        }
        maven {
            url: "some url"
            name: "some name"
        }
        maven {
            url: "some url"
            name: "some name"
        }
        maven {
            url: "some url"
            name: "some name"
        }
        maven {
            url: "some url"
            name: "some name"
        }
    }
}

I feel like creating my own repository entry can make this a lot cleaner.

My other motivation for doing so is that I would like to be able to add some of these maven repositories conditionally based on some input property provided to gradle e.g.

gradle -Puse_repo=true <task>.

I thought if I was able to create my own entry, that I can add the check inside that entry to whether include the repository or not.

Thanks!

like image 294
Jon Avatar asked Dec 17 '25 19:12

Jon


2 Answers

I don’t think you can really add a new entry like jcenter() as those entries are essentially just methods on the built-in interface RepositoryHandler.

However, if you’re just trying to move your own repository configurations into a separate method/closure, then you can go with one of the following options:

def myNewRepos = {
    maven {
        url = "some url"
        name = "some name"
    }
    if (project.hasProperty('use_repo')
          && project.property('use_repo') == 'true') {
        maven {
            url = "some other url"
            name = "some other name"
        }
    }
    // ... any others ...
}

/* OPTION 1: */
repositories {
    mavenLocal()
    // ... any others ...
}
repositories myNewRepos

/* OPTION 2: */
repositories {
    mavenLocal()
    // ... any others ...

    myNewRepos.delegate = owner
    myNewRepos()
}
like image 54
Chriki Avatar answered Dec 19 '25 20:12

Chriki


Below is how I configured my company Nexus Maven repo:

allprojects {
repositories {
    mavenLocal()
    google()
    jcenter()
    maven {
        url "https://nexus.my-company.com/repository/maven-repo/"
        credentials {
            username "user"
            password "pass"
        }
    }

}

like image 41
Sagar Mody Avatar answered Dec 19 '25 20:12

Sagar Mody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!