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.

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

nexusUrl=http://localhost:8081/nexus
nexusUsername=admin
nexusPassword=admin123

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

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "${nexusUrl}/content/repositories/releases") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            snapshotRepository(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 23, Evaluating Nexus Step by Step.