Unable to publish artifacts to Nexus maven repository using Gradle - maven

I am new to Gradle, nexus and Maven and trying to use Gradle 'publish' task to publish maven artifacts from Jenkins to a new nexus repository. The Jenkins job is failing with below error while publishing. I have provided the user name and password for Nexus in the job.
:common-test:publishMavenJavaPublicationToMavenRepositoryCould not find metadata
com.xx.audit:common-test:1.0.3-SNAPSHOT/maven-metadata.xml in
remote (https://nexus.xx.com:8443/content/repositories/snapshots)
Upload https://nexus.xx.com:8443/content/repositories/snapshots/yy/audit/common-test/1.0.3-SNAPSHOT/common-test-1.0.3-20151102.120123-1.jar
Could not transfer artifact com.xx.audit:common-test:jar:1.0.3-20151102.120123-1
from/to remote (https://nexus.xx.com:8443/content/repositories/snapshots):
Could not write to resource 'yy/audit/common-test/1.0.3-SNAPSHOT/common-test-1.0.3-20151102.120123-1.jar'
Do we need to create folder structure in nexus maven repository before publishing first time? And add the maven-metadata.xml? How are the *.pom.sha, *.pom.md5 are generated.
Please help me on this.
build.gradle configuration:
apply plugin: "maven-publish"
//* New Gradle task to jar source
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
//* New Gradle task to jar javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar {
classifier "sources"
}
}
}
}
publishing {
repositories {
maven {
credentials {
username nexusUsername
password nexusPassword
}
if (project.version.endsWith('-SNAPSHOT')) {
url nexusSnapshotRepoURL
} else {
url nexusReleaseRepoURL
}
}
}
}

I do not know the maven-publish Gradle plugin. Typically the uploadArchives task is used. A full example for its usage is available in the Nexus book examples project. Use that as a reference to test your credentials and setup.

Related

Gradle Not resolving SNAPSHOT as timestamp

I am currently having a problem when publishing a Gradle build to a snapshot Artifactory repository where 'SNAPSHOT' is not being resolved to a timestamp. The jars can be found on the repo but are in the format of '1.0.1-SNAPSHOT.jar' instead of e.g. '1.0.1-20180420.112216-1.jar'. This is causing problems when other builds depend on the project in question. We currently have Maven builds which are pushing to the same repo without any problems.
I am using the maven-publish and com.jfrog.artifactory plugins. It is worth mentioning that I don't have a lot of Gradle experience.
Relevant pieces from build.gradle:
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
artifactory {
contextUrl = ${rep.url}
publish {
ext.systemProperties = System.getenv()
println "Publishing using this user: ${systemProperties.artifactory_user}"
println "Publishing to this repo: ${systemProperties.artifactory_repo}"
repository {
repoKey = "${systemProperties.artifactory_repo}"
username = "${systemProperties.artifactory_user}"
password = "${systemProperties.artifactory_password}"
maven = true
}
defaults {
publications('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
relevant from gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.2-bin.zip
Environment variables come from Jenkins and are the same used in our maven builds.
I don't see where you actually set your jar name in the code at all. The Gradle doc for the Maven Publishing plugin tells you how you can customize the publication identity of your artifact:
publishing {
publications {
mavenJava(MavenPublication) {
groupId 'org.gradle.sample' // Modify the artifact group
artifactId 'project1-sample'// Modify the artifact name
version '1.1' // Modify the artifact version
from components.java
}
}
}
If you are not overriding these values in the publishing closure as shown above, the default values of the project are being used for naming the artifact which is defined by the group and version properties of your project. The default version value is version '1.0-SNAPSHOT' if you start a Gradle project from scratch in IntelliJ for example.
The following code should help you to resolve your issue:
publishing {
publications {
mavenJava(MavenPublication) {
version 'your_time_stamp'
from components.java
}
}
}

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.

gradle archiva integration copy jar from build to archiva repository

I am integrating gradle to archiva.I was able to build the jar of the project successfully , but craeting inside build/libs.
I want to add this jar to my archiva repository internal after build. please guide me
my archiva repo
http://localhost:8080/repository/internal/
See Gradle docs:
Chapter 34. Maven Publishing - describes how to use 'maven-publish' gradle plugin to define publications and repositories.
34.4. Performing a publish - contains full example of additions to build.gradle:
apply plugin: 'java'
apply plugin: 'maven-publish'
group = 'org.gradle.sample'
version = '1.0'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
publishing {
repositories {
maven {
url "http://localhost:8080/repository/internal/"
credentials {
username repoUser
password repoPassword
}
}
}
}
Add credentials {} section if you need to supply username/password for repository. repoUser and repoPassword are variables defined somewhere else. For example, apply gradle-properties-plugin and define these variables in gradle-local.properties:
repoUser=jsmith
repoPassword=secret
Make sure not to commit this file into source code repository.

How to specify module name when publishing to Artifactory from Jenkins

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"

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).

Resources