Gradle: Push to Maven Repository

Spread the love

If you are a developer sharing your artefacts is a common task, that needs to be in place from the start.

In most teams and companies a Maven repository is already set up, this repository would be used mostly through CI/CD tasks enabling developers to distribute the generated artefacts.

In order to make the example possible we shall spin up a Nexus repository using Docker Compose.

First, let’s create a default password and the directory containing the plugins Nexus will download. As of now, the password is clear text, this will serve us for doing our first action. By resetting the admin password on nexus the file shall be removed, thus there won’t be a file with a clear-text password.

mkdir nexus-data
cat a-test-password > admin.password

Then onwards to the Compose file:

    image: sonatype/nexus3
      - 8081:8081
      INSTALL4J_ADD_VM_PARAMS: "-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m"
      - nexus-data:/nexus-data
      - ./admin.password:/nexus-data/admin.password

Let’s examine the file.
By using INSTALL4J_ADD_VM_PARAMS we override the Java command that will run the Nexus server, this way we can instruct to use more memory.
Because nexus takes too long on initialization we shall create a Docker volume. Every time we run the compose file, the initialization will not happen from start, instead will use the results of the previous initialization. By mounting the admin password created previously we have a predefined password for the service.

By issuing the following command, the server will be up and running:

After some time nexus will have been initialized and running, therefore we shall proceed to our Gradle configuration.

We will keep track of the version on the file

The build.gradle file:

plugins {
    id 'java'
    id 'maven-publish'
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
test {
publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId 'org.example'
            artifactId 'gradle-push'
            version version
            versionMapping {
                usage('java-api') {
                usage('java-runtime') {
            pom {
                description = 'Gradle Push to Nexus'
                licenses {
                    license {
                        name="The Apache License, Version 2.0"
                developers {
                    developer {
                        id = 'John'
                        name="John Doe"
                scm {
                    connection = 'scm:git:'
                    developerConnection = 'scm:git:'
    repositories {
        maven {
            def releasesRepoUrl = "http://localhost:8081/repository/maven-releases/"
            def snapshotsRepoUrl = "http://localhost:8081/repository/maven-snapshots/"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            credentials {
                username "admin"
                password "a-test-password"

The plugin to be used is the maven-publish plugin. If we examine the file, we identify that the plugin generates a pom maven file which shall be used in order to execute the deploy command to Nexus. The repositories are configured in the repositories section including the nexus users we defined previously. Whether the version is a SNAPSHOT or a release the corresponding repository endpoint will be picked. As we see clear text password is used. In the next blog, we will examine how we can avoid that.

Now it is sufficient to run

This will deploy the binary to the snapshot repository.

snapshot repository

You can find the source code on GitHub.

Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button