3

I am trying to get the Gradle Maven Publish Plugin to publish a snapshot version of my Java library to my local Maven repo such that:

  1. The version of the jar is 1.0.0.SNAPSHOT-<timestamp>, where <timestamp> is the current system time in millis (similar to something like System.currentTimeInMillis()); and
  2. I log to STDOUT/console the full name of the jar being published, including the version above; and
  3. A properly-formatted pom.xml is published to Maven local alongside the jar, so that any other Gradle/Maven projects can "pull it down" locally and fetch its transitive dependencies properly

My best attempt so far:

plugins {
    id 'java-library'
    id 'maven-publish'
}

dependencies {
    compile(
        'org.hibernate:hibernate-core:5.0.12.Final'
        ,'com.fasterxml.jackson.core:jackson-core:2.8.10'
        ,'com.fasterxml.jackson.core:jackson-databind:2.8.10'
        ,'com.fasterxml.jackson.core:jackson-annotations:2.8.0'
    )

    testCompile(
        'junit:junit:4.12'
    )
}

repositories {
    jcenter()
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
group 'com.me'

jar {
    baseName = 'my-lib'
    version = '1.0.0-SNAPSHOT'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

However, with this setup, when I run ./gradlew publishToMavenLocal:

  • I do see the jar being deployed to ~/.m2/repository/com/me/my-lib/ but without a pom.xml and no 1.0.0.SNAPSHOT version appended to it
  • I don't even know how/where I would append the timestamp onto the version
  • I don't even know how/where I would do a println(...) to report the full name of the jar being published

Any ideas?

hotmeatballsoup
  • 385
  • 6
  • 58
  • 136

1 Answers1

8

Regarding #3, To install your artifact to a local repository you do not need the maven-publish plugin, rather the maven plugin

See The Maven plugin documentation, specifically the Tasks section and the Installing to the local repository section with it, you can run gradle clean build install

It works for me with a build.gradle file as simple as this

version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'maven'

Note, if you need to publish something other then the default generated jar then you need to change the archives configuration


Regarding #1 appending the timestamp, move the version line outside the jar clause and change it from

version = '1.0.0-SNAPSHOT'

to

version = "1.0-SNAPSHOT-${System.currentTimeMillis()}"

This is using Groovy GString (AKA string interpolation - note the change from single quotes to double quotes) to append the current time in millis to the version


Last but not least, regarding #2 printing the jar full name append the following to the build.gradle file

install.doLast {
        println jar.archiveName
}

Essentially we're appending to the install task (the one executed in the top of my answer) a println of the jar configuration's archiveName (see here if you want something else)


So all in all my build.gradle file looks like this:

group 'com.boazj'
version "1.0-SNAPSHOT-${System.currentTimeMillis()}"

apply plugin: 'java'
apply plugin: 'maven'

install.doLast {
        println jar.archiveName
}
Boaz
  • 4,549
  • 2
  • 27
  • 40