Gradle publish / deploy to Nexus or Artifactory using maven-publish plugin

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 publish the jar to our private Nexus or Artifactory repository.

Here is a sample gradle file which will publish any given jar file to Nexus or Artifactory repository.

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 { ... }.

build.gradle

 1apply plugin: 'java'
 2apply plugin: 'maven-publish'
 3
 4def repoUrl = "http://artifactory.firstfewlines.com:8081/artifactory"
 5
 6version = '1.0.0'
 7group = 'com.firstfewlines.sample'
 8
 9publishing {
10    repositories {
11        maven {
12            credentials {
13                username 'admin'
14                password 'admin123'
15            }
16            url "${repoUrl}/release"
17        }
18    }
19}
20
21publishing {
22    publications {
23        maven(MavenPublication) {
24            groupId 'com.firstfewlines.sample'
25            artifactId 'sample'
26            version '2.1.4'
27
28            artifact 'sample.jar'
29            pom.withXml {
30                def dependencies = asNode().appendNode('dependencies')
31                
32                def d = dependencies.appendNode('dependency')
33                d.appendNode('groupId', 'org.apache.httpcomponents')
34                d.appendNode('artifactId', 'httpclient')
35                d.appendNode('version', '4.3.6')
36                                
37                d = dependencies.appendNode('dependency')
38                d.appendNode('groupId', 'org.apache.httpcomponents')
39                d.appendNode('artifactId', 'httpcore')
40                d.appendNode('version', '4.3.3')
41            }
42        }
43    }
44}

Before you upload your jar, make sure you change all the parameters to match your environment. And don't forget to keep the jar file into same directory where the build.gradle is kept.

Now upload

Once you are done with the gradle file as above, just run gradle publish from command line.


comments powered by Disqus