Repository Management with Nexus


4.8. Gradle

Gradle has a built in dependency management component that supports the Maven repository format. In order to configure a Gradle project to resolve dependencies declared in build.gradle file, a maven repository as shown in Minimal Gradle Setup has to be declared

Minimal Gradle Setup. 

repositories {
    maven {
        url "http://localhost:8081/nexus/content/groups/public"

These minimal settings allow Gradle to download the declared dependencies.

The above setup is specific to each project. Alternatively an init.gradle file placed e.g., in ~/.gradle can establish Nexus the source for dependencies in all projects. A simple implementation could look like

allprojects {
  ext.RepoConfigurator = {
    maven {
      url = uri('http://localhost:8081/nexus/content/groups/public') }
  buildscript.repositories RepoConfigurator
  repositories RepoConfigurator

Other setup could be an expansion of the following example allowing file system based repostories:

 * init.gradle file for development using Nexus as proxy repository
 * @author Manfred Moser <

apply plugin:NexusRepositoryPlugin

class NexusRepositoryPlugin implements Plugin<Gradle> {

  final static String LOG_PREFIX = "init.gradle/NexusRepositoryPlugin:"

  final Closure NexusConfig = {
    maven {
      name = 'standard-nexus'
      url = 'http://localhost:8081/nexus/content/groups/public'
    // if required you can add further repositories or groups here
    // and they will be left intact if the name starts with standard-
    // although it is better to just add those repositories in Nexus
    // and expose them via the public group

  final Closure RepoHandler = {
    all { ArtifactRepository repo ->
      if ("standard-") ) {
         println "$LOG_PREFIX $ at $repo.url activated as repository."
      } else {
        if (repo instanceof MavenArtifactRepository) {
          remove repo
          println "$LOG_PREFIX $ at $repo.url removed."
        } else {
          println "$LOG_PREFIX $ kept (not a Maven repository)."

  void apply(Gradle gradle) {
    // Override all project specified Maven repos with standard
    // defined in here
    gradle.allprojects{ project ->
      println "$LOG_PREFIX  Reconfiguring repositories."
      project.repositories RepoHandler
      project.buildscript.repositories RepoHandler

      project.repositories NexusConfig
      project.buildscript.repositories NexusConfig

Gradle init scripts can be much more powerful and customized and are explained with more examples in the official Gradle documentation.

To deploy build outputs to a Nexus repository with the uploadArchives task, user authentication can be declared in e.g.,


and then used in the uploadArchives task with a mavenDeployer configuration from the Maven plugin:

uploadArchives {
  repositories {
    mavenDeployer {
        url: "${nexusUrl}/content/repositories/releases") {
          authentication(userName: nexusUsername, password: nexusPassword)
        url: "${nexusUrl}/content/repositories/snapshots") {
          authentication(userName: nexusUsername, password: nexusPassword)

Full example projects can be found in the gradle folder of the Nexus book examples project. A full build of the simple-project, including downloading the declared dependencies and uploading the build output to Nexus can be invoked with

cd gradle/simple-project
gradle upload

Further details about using these example projects can be found in Chapter 25, Evaluating Nexus Step by Step.