Need the gradle script to publish a jar in my project to a nexus repository


(lttle Lingzi) #1

In my Gradle Java project I have a binary .jar file I would like to publish it to the nexus repository during the build process. Let’s say my project structure is this:

c:\MyProject\ClassesToBePublishedJar.jar
c:\MyProject\build.gradle
c:\MyProject\OtherProjectFolders

I think I will do this:

  1. modify the build.gradle file to
    A) define the jar to be published in this case it is c:\MyProject\ClassesToBePublishedJar.jar,
    B) define the URL for the nexus repository location
    C) define the name and password to access this nexus repository
  2. rerun gradle clean build, during this process the jar I wanted to publish should be placed in the nexus.

Please let me know if this process is correct and if yes, can you give me the script. If not, can you point me out the correct path to achieve my goal.


(Chris Doré) #2

This should be relatively easy to do using either the maven-publish or ivy-publish plugins. What repository format are you publishing to?

In addition to the examples provided in the documentation, if the jar you want to publish is generated external to and before Gradle’s execution, then you can publish the file directly instead of publishing the output of a task or the java component:

apply plugin: 'maven-publish'
ext {
    jarToPublish = file( 'ClassesToBePublishedJar.jar' )
}
publishing {
    publications {
        yourPublicationNameHere( MavenPublication ) {
            artifact jarToPublish
            /* can specify a classifier if not encoded in the filename
            artifact jarToPublish {
                classifier 'theClassifier'
            }
            */
        }
    }
    repositories {
        maven {
            url 'http://you.nexus.server/repo'
            credentials {
                username 'user'
                password 'password'
            }
        }
    }
}

If the jar is generated by a task in the Gradle build, then it is best to use the task as the source of the artifact (as shown in the documentation). If the task is not a subclass of AbstractArchiveTask, then something like this will help you:

artifact( tasks.theTask.outputs.files.singleFile ) {
    builtBy tasks.theTask // ensures task dependencies are created
}