How to upload custom metadata with artifact to nexus repository using gradle

(shreedhar) #1

We need to store some extra information with artifacts(like git branch name, sha-1 code etc) in nexus repository.

We are using gradle as build tool and wanted to know how can we upload metadata file with artifact into nexus repository using Gradle.

(Chris Doré) #2

If you’re using the maven-publish plugin, here’s an example of publishing an additional artifact:

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

group = ''
version = '5.2'

task createMeta {
    File outFile = new File( buildDir, "${name}/meta.file" )

    outputs.upToDateWhen { false }  // best to replace this with proper inputs if they exist
    outputs.file outFile

    doLast {
        outFile.withWriter( 'UTF-8' ) { writer ->
            writer.println 'some meta data'

publishing {
    publications {
        main( MavenPublication ) {
            artifact( tasks.createMeta.outputs.files.singleFile ) {
                // configures a
                builtBy tasks.createMeta
                classifier 'metadata'

(shreedhar) #3

Thank you Chris ! :slight_smile:

Now, I am just curious that how can we attach a metadata file which is already there or created by other source.

(Chris Doré) #4

If you need to move/rename a file already on the Nexus server to be an artifact of a publication (is that what you mean by “attach”?) then I would take a look at what APIs Nexus provides and then write a task to use those APIs.

(shreedhar) #5

No. I want to bundle a metadata file with jar file(project’s outcome) and upload to Nexus repository.

Also I tried your solution above but I see publish task is getting skipped.

Ouput of gradle publish

Task :publish
Skipping task ':publish' as it has no actions.
publish (Thread[Daemon worker Thread 11,5,main]) completed. Took 0.002 secs.
Stopped 0 worker daemon(s).

Ouput of: gradle createMeta

> Task :createMeta
Putting task artifact state for task ':createMeta' into context took 0.0 secs.
Executing task ':createMeta' (up-to-date check took 0.0 secs) due to:
  Task.upToDateWhen is false.

:createMeta (Thread[Daemon worker Thread 12,5,main]) completed. Took 0.011 secs

1 actionable task: 1 executed
Stopped 0 worker daemon(s).

If above task is executed successfully, how can I check the metadata.
P.S. I am new to gradle :slight_smile:

(Chris Doré) #6

My example task should have created <projectdir>/build/createMeta/meta.file.

(Chris Doré) #7

Sorry, my example did not include any repositories, did you add them?

publishing {
    publications {...}
    repositories {
        maven {
            url '' // I'm not sure what the Nexus repo URL format is

The publishToMavenLocal task should work without any repositories specified. Here’s what I get running my example (I had to add a group and version to my example and fix the mkDirs typo, I updated my original post):

$ ./gradlew pTML
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE


$ cat build/createMeta/meta.file
some meta data

$ ls -l ~/.m2/repository/gradle/forum/test/gradle-forum-test/5.2/
total 12
-rw-rw-r-- 1 cdore cdore 863 Jun 16 12:43 gradle-forum-test-5.2.jar
-rw-rw-r-- 1 cdore cdore  15 Jun 16 12:44 gradle-forum-test-5.2-metadata.file
-rw-rw-r-- 1 cdore cdore 405 Jun 16 12:44 gradle-forum-test-5.2.pom