How to specify module name when publishing to Artifactory from Jenkins - maven

I'm using the gradle artifactory publish plugin documented here: http://www.jfrog.com/confluence/display/RTF/Gradle+1.6+Publishing+Artifactory+Plugin
I'm using the standard config exactly as laid out in the documentation.
When I run the gradle publishArtifactory task from the command line I get output like this. I.e. It deploys to my correct module name.
Deploying artifact: http://<my server>/artifactory/libs-snapshot-local/<my actual module name>/web/0.1-SNAPSHOT/web-0.1-SNAPSHOT.war
When I configure Jenkins to run the gradle publishArtifactory task using the same gradle build file I get output like this. I.e. It uses the Jenkins build for the module name.
Deploying artifact: http://artifactory01.bcinfra.net:8081/artifactory/libs-snapshot-local/<the name of the jenkins build>/web/0.1-SNAPSHOT/web-0.1-SNAPSHOT.war
Any ideas on how to prevent the artifactory plugin from using the Jenkins build name for the module name?

The module name used for uploading is derived from the gradle project name. The default value for a gradle project name is taken from the project folder name. I suspect that on your jenkins job you check out your code into a folder named like your build job. That's why per default this folder name is used as project name.
The cleanest solution is to explicitly set your project name in gradle.
Therefore you need a settings.gradle file in your project root folder that contains the project name:
rootProject.name = "web"

You can also let Gradle single-handedly do the publishing to Artifactory, without the need for the Artifactory plugin in Jenkins.
This way, you can set the names of the artifacts using artifactId "your artifact name" without changing the project's name as suggested by Rene Groeschke.
Here's my publish.gradle that demonstrates this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
// Because this is a helper script that's sourced in from a build.gradle, we can't use the ID of external plugins
// We either use the full class name of the plugin without quotes or an init script: http://www.gradle.org/docs/current/userguide/init_scripts.html
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
// Pack the sources into a jar
task sourceJar(type: Jar) {
from sourceSets.main.allSource; classifier = "sources"
}
// Pack the Javadoc into a jar
task javadocJar(type: Jar) {
from javadoc.outputs.files; classifier = "javadoc"
}
apply plugin: "maven-publish"
publishing {
publications {
mavenJava(MavenPublication){
from components.java
// Set the base name of the artifacts
artifactId "your artifact name"
artifact jar
artifact sourceJar
artifact javadocJar
}
}
}
artifactory {
contextUrl = "http://localhost:8081/artifactory"
publish {
// Publish these artifacts
defaults{ publications ("mavenJava") }
repository {
repoKey = "libs-release-local"
// Provide credentials like this:
//-Partifactory.publish.password=yourPassword
//-Partifactory.publish.username=yourUsername
}
}
resolve {
repository {
repoKey = "libs-release"
}
}
}
You can use this script in your build.gradle via apply from: "path/to/publish.gradle" and call it like this:
./gradlew artifactoryPublish -Partifactory.publish.username="yourUsername" -Partifactory.publish.password="yourPassword"

Related

Get full artifact name generated by gradle

I'm publishing an artifact using maven-publish.
When I'm pushing a release artifact, the resulting artifact version is the same as the project version itself. For example if gradle.properties has version=1.2.3, the artifact would be something like foo-1.2.3.zip.
When I run a SNAPSHOT publish, the resulting artifact will include additional information in the version. For example version=1.2.4-SNAPSHOT gives foo-1.2.4-20220427.094127-1.zip. The additional information would appear to be the time and date to avoid clashes, I assume.
Is there anyway I can access this full artifact name in my gradle scripts?
If you don't specify the artifact names explicitly like this for example
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'library'
version = '1.1'
from components.java
}
}
}
Then gradle will use the defaults that are taken from your build.gradle, gradle.properties and the module directory names.
I'm not sure if there's a way to configure the mavenPublish plugin to print out the maven artifact upon publishing, but you can look in your ~/.m2 directory to see what exactly has been published to your local if you run ./gradlew publishToMavenLocal
I'm also thinking you could create a custom task to print out the group, artifact and version, something like this in the build.gradle of your project you are publishing:
project.task('getMavenCoordinates') {
doLast {
println "Maven Artifact: ${project.group}:${project.name}:${project.version}"
}
}
project.task("publishToMavenLocal").dependsOn("getMavenCoordinates")
If you wanted to configure the task to run after a different maven-pubish task, just change it accordingly! But something like that could help achieve what you want.

Setting up Artifactory in gradle global

currently I have a project which is deploying artifacts in our artifactory. For that Project everything is setted up perfect and it works.
I just wonderd if there is a possibility to set up the whole artifactory configuration global in gradle, so that I don't have to write the artifactory {...} stuff for each project.
You can maintain a file lets say build_dependency.gradle and define the task for all project
allprojects
{
//task common for all the project
}
subprojects
{
//task for subprojects
}
or specify the type of project e.g ext.warProject = 1 in dependency file and refer it in build_dependency.gradle as
if(project.hasProperty('warProject '))
{
//task here
}
and use this file in build.gradle like apply from: "$rootDir/path_to_file/build_dependency.gradle"
"$rootDir/path_to_dependenccy_file"`
You could simply write your own Gradle plugin that would be responsible for:
applying the artifactory plugin and other related plugin(s) like maven-publish
provide default values for the artifactory extension properties , like contextUrl, repoKey, credentials, etc...
Then your different projects will just have to apply your custom plugin, and provide only the project-specific configuration (configuration of the artefact to be published, for example, in publishing extension)
EDIT there are other ways to implement that, but it depends on what you mean by "global in gradle":
global to your own computer? then you could create a User InitScript that would contain the artifactory plugin configuration part
global to your team/company ? then you could need to implement a custom plugin, and maybe include this plugin into a custom gradle wrapper distribution (see example here
EDIT2 If you just want to set the artifactory plugin configuration of different sub-project of a same multi-project build, then the simpliest solution would be to define this configuration in the subprojects block of the root project build script:
subprojects {
apply plugin: "com.jfrog.artifactory"
artifactory {
publish {
contextUrl = '<repo url>'
repository {
repoKey = "<repo name>"
username = "user"
password = "pass"
}
}
}
}

Publish a zip file to Nexus (Maven) with Gradle

Say you have a PL file you want to upload to Nexus with Gradle. How would such a script look like.
group 'be.mips'
version = '1.4.0-SNAPSHOT'
In settings.gradle --> rootProject.name = 'stomp'
And let's say that the pl file is in a subdirectory dist (./dist/stomp.pl).
Now I want to publish this stomp.pl file to a nexus snapshot repository.
As long as you go with Java, then Gradle (just like Maven) works like a charm. But there's little documentation found what to do if you have a DLL, or a ZIP, or a PL (Progress Library).
I publish such artifacts for a long time. For example, ZIP archives with SQL files. Let me give you an example from real project:
build.gradle:
apply plugin: "base"
apply plugin: "maven"
apply plugin: "maven-publish"
repositories {
maven { url defaultRepository }
}
task assembleArtifact(type: Zip, group: 'DB') {
archiveName 'db.zip'
destinationDir file("$buildDir/libs/")
from "src/main/sql"
description "Assemble archive $archiveName into ${relativePath(destinationDir)}"
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact source: assembleArtifact, extension: 'zip'
}
}
repositories {
maven {
credentials {
username nexusUsername
password nexusPassword
}
url nexusRepo
}
}
}
assemble.dependsOn assembleArtifact
build.dependsOn assemble
publish.dependsOn build
gradle.properties:
# Maven repository for publishing artifacts
nexusRepo=http://privatenexus/content/repositories/releases
nexusUsername=admin_user
nexusPassword=admin_password
# Maven repository for resolving artifacts
defaultRepository=http://privatenexus/content/groups/public
# Maven coordinates
group=demo.group.db
version=SNAPSHOT
If you're using Kotlin DSL, declare the artifact as show below:
artifact(tasks.distZip.get())
where distZip is the task that produces the zip file, which in the above example, is from the application plugin.

Publishing artifact from gradle project to bintray (maven repository)

I have configured Gradle to publish project artifact using new Maven Publisher Plugin, unfortunately this plugin has problem with dependency in generated pom.xml - dependencies has scope runtime instead of compile.
My configuration is like this:
apply plugin: 'maven-publish'
publishing {
publications {
mavenCustom(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "https://api.bintray.com/maven/codearte/public/fairyland"
credentials {
username = bintrayUser
password = bintrayKey
}
}
}
}
Publishing was simple with one command:
gradle publish
How to achieve this in old (working) way? Is possible to automate project taging when project is released?
Ok, I figured it out:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
name = 'Codearte Public Repository'
repository(id: 'codearte-repository', url: 'https://api.bintray.com/maven/codearte/public/fairyland'){
authentication(userName: bintrayUser, password: bintrayKey)
}
}
}
Uploading with command:
gradle uploadArchives
The fact that all POM dependencies have runtime scope is a known limitation of the new, incubating maven-publish plugin. Until this gets fixed, you can either fix it up yourself by using the publication.pom.withXml hook, or fall back to the maven plugin. Both plugins are documented in the Gradle User Guide.
Tagging is an entirely different question. You can either use one of the third-party Gradle SCM plugins or call out to a command line tool (e.g. with an Exec task).

Gradle script to copy maven artifact from local folder to another folder

In my project i want to copy certain artifact (war file) from maven repo to local folder in order to deploy. I tried using configurations object but i couldn't give specific groupid, artifact id, and version in that way
repositories {
mavenCentral() // or some other repo
}
configurations {
deploy
}
dependencies {
deploy "someGroup:someArtifact:someVersion"
}
task copyDeploy(type: Copy) {
from configurations.deploy
into "deploy"
}
You can find all of this and more in the Gradle User Guide (e.g. under "Working with dependencies") and the many samples in the full Gradle distribution.

Resources