Artifactory, Atlassian, Gradle

Publishing with Gradle into Artifactory

In order to publish your project artifact into Artifactory, you need to define publication details in your build.gradle file.

buildscript {
    // repository to resolve dependencies during the build
	repositories {
		maven {
			url "${artifactory2_contextUrl}/libs-release"
			credentials {
				username = "${artifactory_user}"
				password = "${artifactory_password}"
			}
		}
	}
	dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.3"
    }
}
plugins {
    id 'java-library'
}
group = 'com.my.company'
version = '1.0.0-SNAPSHOT'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
repositories {
	maven {
		url "${artifactory2_contextUrl}/libs-release"
		credentials {
			username = "${artifactory_user}"
			password = "${artifactory_password}"
		}
	}
}
sourceCompatibility = 1.11
targetCompatibility = 1.11
dependencies {
	compileOnly 'org.projectlombok:lombok:1.18.6'
	annotationProcessor 'org.projectlombok:lombok:1.18.6'
    compile 'org.slf4j:slf4j-api:1.7.26'
    compile 'org.apache.commons:commons-lang3:3.8.1'
    compile 'org.apache.commons:commons-collections4:4.3'
    compile 'org.springframework.boot:spring-boot-starter-aop:2.1.3.RELEASE'
    testImplementation 'junit:junit:4.12'
}
// task for generation sources.jar
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}
publishing {
    publications {
        mavenJava(MavenPublication) {
        	// publish generated jar file
            from components.java
            // publish source code as well
            artifact (sourcesJar) {
                classifier = 'sources'
            }
        }
    }
}
//repository to publish into
artifactory {
    // the base Artifactory URL if not overridden by the publisher/resolver
	contextUrl = "${artifactory2_contextUrl}"
	publish {
		repository {
            // define if snapshot or release repository should be used for the deploy
			def repositoryKey = version.endsWith("SNAPSHOT") ? "libs-snapshot-local" : "libs-release-local"
			println "Publishing ${project.name} version ${version} into ${repositoryKey}"

            repoKey = repositoryKey
        	username = "${artifactory_user}"
			password = "${artifactory_password}"
			maven = true
		}
		defaults { publications ('mavenJava') }
	}
}

First you need to define the Artifactory server and credentials for deploying your artifact. In the “artifactory” you can define the target repository even dynamically. In my example the repository URL depends on the type of the artifact. It uses different repository for snapshot and final releases.

It is recommended to use placeholders, in the file, and get the sensitive info from gradle.properties.

artifactory2_contextUrl=https://artifactory.three.com
# configure user and password in your gradle.properties file in the user's ${gradleUserHomeDir}/.gradle directory
artifactory_user=user
artifactory_password=secret

You need to define the “org.jfrog.buildinfo:build-info-extractor-gradle” dependency, which is the “JFrog Gradle plugin for Build Info extraction and Artifactory publishing”. Also it is the tool, that does the deployment work for you.

In order to actually include publishing into your build process, you need to apply following plugins: 

apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'

In case of you want to publish the source code into Artifactory as well, you need to define the “sourcesJar” task, and include it in “publications”.

Normally you want to publish your generated jar file, so you include the “from components.java” into “publications”.

After you have configured your Gradle build, you can start deploying with
gradlew artifactoryPublish

 

Advertisement