Publish a zip file to Nexus (Maven) with Gradle - maven

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.

Related

How to publish an obfuscated jar file and pom to Nexus repository with gradle

I need to publish a couple of obfuscated jar files with their POM (before obfuscation) to a Nexus repository with Gradle.
The problem is if I select components.java then the non-obfuscated jar file is deployed.
I am using yGuard for obfuscation, and now in my build file I have created pom file, clean jar file and obfuscated jar file, but cannot find a way to publish the pom and obfuscated jar file together to our Nexus repository.
By the way the project is a multi module java-library project.
In my main build.gradle file I have something like
configure(subprojects) {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
jar {
archiveFileName = "${project.name}-${project.version}-clean.jar"
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForCleanPublication }
rename '.*', 'pom.xml'
}
}
publishing {
publications {
clean(MavenPublication) {
from components.java
withBuildIdentifier()
}
}
repositories {
maven {
name 'nexus'
url 'SOME URL'
allowInsecureProtocol = true
credentials {
username 'someUserName'
password 'somePassword'
}
}
}
}
}
so the pom is created according to module dependencies, but now I cannot publish the obfuscated jar file with same POM file.
Notes: The obfuscated jar files are generated in same folder as 'build\lib' without '-clean.jar` suffix.
Notes: when I remove from components.java and add my own artifact (obfuscated file) the generated pom won't have dependencies, and I couldn't find a way to override the jar file added to publication.

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.

Unable to publish artifacts to Nexus maven repository using Gradle

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.

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