How to use Maven-Publish Plugin with the Jenkins Artifactory Plugin

Summary

How to use Maven-Publish with the Jenkins Artifactory Plugin 

Description

According to Gradle, it is recommended that the maven-publish and ivy-publish plugins be used in lieu of the maven plugin since it will eventually be deprecated.  As such, if the maven-publish plugin is used to generate pom files, it is important that the the plugin is configured correctly in the build.gradle of the project— otherwise, the the pom files could be generated incorrectly.

Resolution

Below is an example of build.gradle of how the maven-publish plugin needs to be configured in order to properly generate pom files.
Please note the following:

  • The ‘java-library’ plugin needs to be applied along with the ‘maven-publish plugin.
  • The publishing closure needs to reference the MavenPublication interface via the mavenJava publication.
  • The artifactory closure for the Jenkins Artifactory plugin needs to reference the mavenJava publication in the defaults closure.
buildscript {  repositories {    jcenter()  }  dependencies {    classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.+')  }}allprojects {    apply plugin: 'java-library'    apply plugin: 'maven-publish'    apply plugin: 'com.jfrog.artifactory'        group = 'org.jfrog.example.gradle'    version = '1.0'    publishing {        publications {            mavenJava(MavenPublication) {                from components.java            }        }    }}artifactory {    contextUrl = "http://localhost:8081/artifactory"   //The base Artifactory URL if not overridden by the publisher/resolver    publish {            repository {                repoKey = 'lib-release-local'                username = "${artifactory_user}"                password = "${artifactory_password}"            }               defaults {                publications('mavenJava')                publishArtifacts = true                publishBuildInfo = true                publishPom = true                }   }}