Upload jar file to Artifactory or Nexus repository using Gradle and Maven publish

Sometime we end up with a situation when we have a jar file as dependency of our project and that jar file is not in our artifact repository. This makes difficulties to include this file into our gradle or maven project. Thanks to maven-publish plugin which help us to easily deploy the jar to our private Artifactory or Nexus repository.

Here is a sample gradle file to demonstrate that.

Consider we have our private Artifactory at http://artifactory.firstfewlines.com:8081/artifactory with admin/admin123 as credential. Consider sample.jar is our jar file that we want to be available as groupId: ‘com.firstfewlines.sample’, artifactId ‘sample’ and version ‘2.1.4’. Also say we know it’s dependencies to other open source projects like Apache httpclient and httpcore, which we can include under pom.withXml { … }.


apply plugin: 'java'
apply plugin: 'maven-publish'

def repoUrl = "http://artifactory.firstfewlines.com:8081/artifactory"

version = '1.0.0'
group = 'com.firstfewlines.sample'

publishing {
    repositories {
        maven {
            credentials {
                username 'admin'
                password 'admin123'
            url "${repoUrl}/release"

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.firstfewlines.sample'
            artifactId 'sample'
            version '2.1.4'

            artifact 'sample.jar'
            pom.withXml {
                def dependencies = asNode().appendNode('dependencies')
                def d = dependencies.appendNode('dependency')
                d.appendNode('groupId', 'org.apache.httpcomponents')
                d.appendNode('artifactId', 'httpclient')
                d.appendNode('version', '4.3.6')
                d = dependencies.appendNode('dependency')
                d.appendNode('groupId', 'org.apache.httpcomponents')
                d.appendNode('artifactId', 'httpcore')
                d.appendNode('version', '4.3.3')

Before you upload your jar make sure you change all the parameters to match your situation. You have to keep the jar file into same directory as the build.gradle

Now upload

Once everything is set, just run gradle publish and your are done.

Hansaraj avatar
About Hansaraj
Hansaraj is a Software Engineer experienced in Java, Groovy, JavaScript, SQL, C#, C++
comments powered by Disqus