Wednesday, May 8, 2024
HomeJavaGradle: Push to Maven Repository - Java Code Geeks

Gradle: Push to Maven Repository – Java Code Geeks


If you’re a developer sharing your artifacts is a standard process, that must be in place from the beginning.

In most groups and corporations a Maven repository is already setup, this repository can be used largely by way of CI/CD duties enabling builders to distribute the generated artifacts.

To be able to make the instance attainable we will spin up a Nexus repository utilizing Docker Compose.

First let’s create a default password and the listing containing the plugins Nexus will obtain. As of now the password is evident textual content, this can serve us for doing our first motion. By resetting the admin password on nexus the file shall be eliminated, thus there gained’t be a file with a transparent textual content password.

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

Then onwards to the Compose file:

providers:
  nexus:
    picture: sonatype/nexus3
    ports:
      - 8081:8081
    setting:
      INSTALL4J_ADD_VM_PARAMS: "-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m"
    volumes:
      - nexus-data;/nexus-data
      - ./admin.password:/nexus-data/admin.password
volumes:
  nexus-data;

Let’s study the file.
By utilizing INSTALL4J_ADD_VM_PARAMS we override the Java command that can run the Nexus server, this manner we are able to instruct to make use of extra reminiscence.
As a result of nexus takes too lengthy on initialization we will create a Docker quantity. Everytime we run the compose file, the initialization is not going to occur from begin, as a substitute will use the outcomes of the earlier initialization. By mounting the admin password created beforehand we have now a predefined password for the service.

By issuing the next command, the server shall be up and operating:

Yow will discover extra on Compose on the Builders Important Information to Docker Compose.

After a while nexus can have been initialized and operating, due to this fact we will proceed to our Gradle configuration.

We are going to hold observe of the model on the gradle.properties file

The construct.gradle file:

plugins {
    id 'java'
    id 'maven-publish'
}

group 'org.instance'
model '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

check {
    useJUnitPlatform()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId 'org.instance'
            artifactId 'gradle-push'
            model model
            from elements.java
            versionMapping {
                utilization('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                utilization('java-runtime') {
                    fromResolutionResult()
                }
            }
            pom {
                title="gradle-push"
                description = 'Gradle Push to Nexus'
                url="https://github.com/gkatzioura/egkatzioura.wordpress.com.git"
                licenses {
                    license {
                        title="The Apache License, Model 2.0"
                        url="http://www.apache.org/licenses/LICENSE-2.0.txt"
                    }
                }
                builders {
                    developer {
                        id = 'John'
                        title="John Doe"
                        e mail="an-email@gmail.com"
                    }
                }
                scm {
                    connection = 'scm:git:https://github.com/gkatzioura/egkatzioura.wordpress.com.git'
                    developerConnection = 'scm:git:https://github.com/gkatzioura/egkatzioura.wordpress.com.git'
                    url="https://github.com/gkatzioura/egkatzioura.wordpress.com.git"
                }
            }
        }
    }

    repositories {
        maven {
            def releasesRepoUrl = "http://localhost:8081/repository/maven-releases/"
            def snapshotsRepoUrl = "http://localhost:8081/repository/maven-snapshots/"
            url = model.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            credentials {
                username "admin"
                password "a-test-password"
            }
        }
    }
}

The plugin for use is the maven-publish plugin. If we study the file, we establish that the plugin generates a pom maven file which shall be used with the intention to execute the deploy command to Nexus. The repositories are configured on the repositories part together with the nexus customers we outlined beforehand. Whether or not the model is a SNAPSHOT or a launch the corresponding repository endpoint shall be picked. As we see clear textual content password is used. On the subsequent weblog we’ll study how we are able to keep away from that.

Now it’s ample to run

It will deploy the binary to the snapshot repository.

Yow will discover the supply code on GitHub.

Revealed on Java Code Geeks with permission by Emmanouil Gkatziouras, associate at our JCG program. See the unique article right here: Gradle: Push to Maven Repository

Opinions expressed by Java Code Geeks contributors are their very own.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments