How to publish Java Artifact using Build tool Gradle To Repository (Nexus)

ยท

2 min read

How to publish Java Artifact using Build tool Gradle To Repository (Nexus)

How to publish Using Gradle build tool

  • You need to configure each build tool with Nexus credentials and address
  • Add the following code snippet in the build.gradle file in your project.
  • This file is also called the Gradle build script. The build configuration, tasks, and plugins are described in this file.
  • Now we have to give user the Gradle or Maven credential to connnect with Nexus
  • Add a plugin in the build.gradle and it will allow gradle to connect with nexus and push in repository
    apply plugin : 'maven-publish'
    
  • Now , add a publishing block

    publishing {
    publication { 
    
        }
    repositories {
    
        }
    }
    
  • Publication block gives the info about jar file configuration
  • Repositories block gives the info about the nexus repositories that we are going to upload our jar file
apply plugin: 'maven-publish'

publishing{
    publications{
        maven(MavenPublication){
            artifact("build/libs/my-app-$version"+".jar"){
                extension 'jar'
            }
        }
    }
    repositories {
        maven {
            name 'nexus'
            url "http://[your nexus ip]:[your nexus port]/repository/maven-snapshorts/"
            credentials {
                username xxx
                password xxx
            }
        }
    }
}
  • Add credentials in gradle.properties file
    repoUser = pankaj
    repoPassword = passwd
    
  • Now, reference those credential in the build.gradle file
apply plugin: 'maven-publish'

publishing{
    publications{
        maven(MavenPublication){
            artifact("build/libs/my-app-$version"+".jar"){
                extension 'jar'
            }
        }
    }
    repositories {
        maven {
            name 'nexus'
            url "http://[your nexus ip]:[your nexus port]/repository/maven-snapshorts/"
            credentials {
                username project.repoUser
                password project.repoPasswd
            }
        }
    }
}

Final step

Build the jar file

  • Open your project folder in the terminal and build the jar file
    ./gradlew build
    
  • You will get output like this :

build.png

  • You can check in your /build/libs/ directory . There will be a .jar file

Upload the jar file to the repository

./gradlew publish

NOTE : the publish command was not present in Gradle . That's why we apply the apply plugin : 'maven-publish' in the first place so, Gradle will know what to publish and where to publish

  • At final check Your Repository in Nexus

publish.png

ย