Using Gradle to publish jars to MavenLocal - gradle

I'm using Gradle to manage the build of a product using multiple eclipse projects.
Several of those projects use jar files that are not available from a central repository (like MavenCentral).
I'd like to have an eclipse project that I just place those jars into, and a build.gradle that will place those jar files into mavenLocal using the groupId, artifactId and version that I specify.
This way I can just add mavenLocal() to other projects and specify the dependency.
I have the following build.gradle:
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'myGroupId'
artifactId 'myArtifact'
version '1.2.3'
from <how to reference jar>
}
}
}
I've tried file('myJar.jar'), but that isn't accepted. It appears that Gradle wants an artifact for the from value, but I can't seem to find how to specify a prebuilt jar file as that artifact.
Seems like it ought to be pretty simple, but I'm not finding it.

Currently, 'from' can only accept components.java and components.web. To include a jar, use 'artifact', for example:
publishing {
publications {
maven(MavenPublication) {
groupId 'myGroupId'
artifactId 'myArtifact'
version '1.2.3'
artifact <path to file>
}
}
}
'artifact' can also accept output of archive tasks:
publishing {
publications {
maven(MavenPublication) {
groupId 'myGroupId'
artifactId 'myArtifact'
version '1.2.3'
artifact getlibrary
}
}
}
task getlibrary(type:Jar){
from <directory>
classifier='lib'
}

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 apply plugin vs plugins

I have a simple plugin with greet task doing some 'Hello World' print.
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
}
group = 'standalone.plugin2.greeting'
version = '1.0'
gradlePlugin {
plugins {
greeting {
id = 'standalone.plugin2.greeting'
implementationClass = 'standalone.plugin2.StandalonePlugin2Plugin'
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'standalone.plugin2.greeting'
version = '1.0'
from components.java
}
}
}
Now, I have runner application to just run the greet task
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath 'standalone.plugin2.greeting:standalone-plugin2:1.0'
}
}
apply plugin: 'standalone.plugin2.greeting'
With apply plugin natation it works OK, but when I use plugins notation instead like this:
plugins {
id 'standalone.plugin2.greeting' version '1.0'
}
it doesn't work.
The error message is:
* What went wrong:
Plugin [id: 'standalone.plugin2.greeting', version: '1.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'standalone.plugin2.greeting:standalone.plugin2.greeting.gradle.plugin:1.0')
Searched in the following repositories:
Gradle Central Plugin Repository
What is the difference? According to documentation apply plugin is old and should not be used.
Before the introduction of the plugins block, plugin dependencies had to be resolved in the same way as regular project dependencies using a combination of repositories and dependencies. Since they need to be resolved before running the actual build script, they need to be defined in the special buildscript block:
buildscript {
repositories {
// define repositories for build script dependencies
}
dependencies {
// define build script dependencies (a.k.a. plugins)
}
}
repositories {
// define repositories for regular project dependencies
}
dependencies {
// define regular project dependencies
}
Once the dependencies were resolved, they could be applied using apply plugin:.
By default, the new plugins block just resolves the plugins from the Gradle Plugin Repository. This is a regular Maven repository, so it can be used using the old way, too:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
In your case, the plugin only exists in mavenLocal, so the plugins block cannot resolve it, as it only looks into the Gradle Central Plugin Repository. You may use the pluginManagement block to resolve plugins from custom repositories.
As described in the article linked above, it is necessary to create a link between the plugin identifier (used inside the plugins block) and the Maven coordinates that provide the respective plugin. To create this link a marker artifact following a specific convention must be published. The Gradle Plugin Development Plugin automatically publishes this marker artifact if used in combination with the Maven Publish Plugin.

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.

Resources