Gradle: Adding version to filename for ivy resolved dependencies - gradle

I'm using a local ivy repository as following:
repositories {
ivy {
url = "${project.rootDir.absolutePath}/../ivy/ivy-repositories/shared/"
layout "pattern", {
artifact "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]"
ivy "[organisation]/[module]/[revision]/ivy.xml"
}
resolve.dynamicMode = true
}
}
I'm using the distribution plugin and set content with:
distributions {
main {
contents {
into ('libs')
{ from {
project(':x-impl').configurations.runtime
} }
}
}
My problem is that the filename of the resolved artifacts (jars) lacks the version. Ie 'commons-codec.jar' instead of 'commons-codec-1.6.jar'. Both on my Eclipse classpath and in the distribution.
Is there any way i can let the version be part of the filename? I can not (easily) change the layout/pattern of the local ivy repository.
Thins question was originally asked at the gradle community but received no answers:
http://gsfn.us/t/4iiml
Thanks,
Andreas

Related

Is it possible to disable publish sha1 and ivy artifacts when use gradle ivy-publish plugin?

I try to publish a rpm artifact to Nexus repository with gradle plugin ivy-publish (script code written below). Is it possible to disable publishing some additional artifacts (sha1, ivy files), automatically generated by plugin ivy-publish ?
I've already disabled publishing sha256 and sha512 artifacts with systemProp.org.gradle.internal.publish.checksums.insecure
Gradle version is 6.3.
publishing {
publications {
artifactDistribution(IvyPublication) {
module = project.name
revision = project.version
def publishingArtifactories = [
[filename: "build/distributions/${project.name}-${project.version}-${project.release}.i386.rpm", extension: "rpm"],
]
publishingArtifactories.each {
artifact (it.filename) {
if (it.extension) {
extension it.extension
}
}
}
}
}
repositories {
ivy {
credentials {
username repoUser
password repoPassword
}
url project.publishUrl
layout 'pattern', {
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
ivy '[organisation]/[module]/[revision]/[module]-[revision].ivy'
m2compatible = true
}
}
}
}

Gradle how to publish a gradle-plugin to maven central

Hi I have a java project, which contains a submodule which is a gradle plugin. I want to publish this module to maven central.
I used this two plugins in my build.gradle:
plugins {
id 'java-gradle-plugin'
id 'maven-publish'
}
and my publishing block looks something like:
publishing {
publications {
javaLibrary(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
artifactId = project.archivesBaseName
pom {
name = artifactId
description = "..."
url =
licenses {
license {
}
}
developers {
...
}
scm {
...
}
issueManagement {
...
}
ciManagement {
...
}
}
}
}
repositories { maven { url = "some local repo" } }
}
I noticed that when I build this module, the generated pom-default.xml is what I expected, but when I run gradle publishToMavenLocal and manually checked the pom.xml file in the .m2 folder, all the metadata like name description licenses are gone!
I also noticed in the .m2 folder there are 2 artifacts that are related to this single plugin, I think it's somewhat related with https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_markers but I don't fully understand the meaning. Both these 2 artifacts' pom are missing the pom metadata as I described above.
Could some gradle expert help me here: how to keep the metadata in the published pom?
You should not need to manually define a MavenPublication for your plugin submodule. The java-gradle-plugin reacts to the application of the maven-publish plugin and automatically configures/creates publications for the plugin artifacts. See this line.
You are correct for the (2) artifacts produced. One is the plugin marker (single pom.xml) and the other is the actual plugin JAR artifact.
As for POM customization, Gradle seemingly provides its own the POM irrespective of any POM customization(s) you have defined: https://github.com/gradle/gradle/issues/17022
Sorry for late answer, but you can do something like this:
afterEvaluate {
tasks.withType(GenerateMavenPom) { task ->
doFirst {
// Update POM here
def pom = task.pom
pom.name = ...
pom.url = ...
pom.description = ...
pom.scm {
...
}
}
}
}
This will catch the pom of the plugin marker artifact as well.

How to add a maven repository by url using kotlinscript DSL (build.gradle.kts)

Whats the equivalent of the following code snippet from a build.gradle in a build.gradle.kts version?
repositories {
mavenCentral()
maven {
url '<MAVEN REPO URL>'
}
}
As an addition to the other answers, in #kotlin-dsl/256 shortcut methods were added to the various repository methods to do something like the following:
repositories {
mavenCentral()
maven(url = "<MAVEN REPO URL>")
}
According to the issue, this was added in the Kotlin DSL version 0.11.1. The 0.11.x versions were included in the Gradle 4.2 release.
To see the Gradle version you are running with your build when using the Gradle wrapper run ./gradlew --version.
The official doco allows you to switch the examples between the Groovy and Kotlin DSLs. Currently the answer listed there to your question is:
repositories {
mavenCentral()
maven {
url = uri("<MAVEN REPO URL>")
}
}
I needed to add Gitlab with authentication, which has a more complicated syntax. For others that stumble upon this, here is the official Gitlab example translated to the kts/Kotlin syntax.
val gitLabPrivateToken: String by project
maven {
url = uri("https://<gitlab-url>/api/v4/groups/<group>/-/packages/maven")
name = "GitLab"
credentials(HttpHeaderCredentials::class) {
name = "Private-Token"
value = gitLabPrivateToken
}
authentication {
create<HttpHeaderAuthentication>("header")
}
}
The example URL here is true to Gitlab doco. But for me, it only worked with a URL like this:
https://gitlab.com/api/v4/projects/12345/packages/maven
At 2018-01-13 the correct syntax is the following (instead of url, the function setUrl):
repositories {
mavenCentral()
maven {
setUrl("<MAVEN REPO URL>")
}
}
You can add a custom Maven URL in the following way as per official docs:
repositories {
maven {
url = uri("<your-custom-url>")
}
}

Publish wrapped OSGi bundles to artifactory with Gradle

As not all jars are automatically usable OSGi bundles I use wrapping to generate them. After having being wrapped I'd like to publish them to my Artifactory repository. However, my lack of understanding of Gradle inhibits success, and after reading several suggested Stackoverflow answers I am still stuck.
This is my build.gradle file:
buildscript {
repositories {
maven { url 'http://localhost:8081/artifactory/gradle-dev' }
}
dependencies {
classpath 'org.standardout:bnd-platform:1.2.0'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
}
}
apply plugin: 'org.standardout.bnd-platform'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'
group = 'com.google.code.gson'
version = '2.8.0'
publishing {
publications {
osgiBundles(MavenPublication) {
artifacts {
files("build/plugins")
}
}
}
}
artifactory {
contextUrl = "${artifactory_contextUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = 'gradle-dev-local'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
defaults {
publications ('osgiBundles')
}
}
resolve {
repository {
repoKey = 'gradle-dev'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
}
}
platform {
useBndHashQualifiers = false
defaultQualifier = ''
bundle(group: 'com.google.code.gson', name:'gson', version:'2.8.0') {
bnd {
instruction 'Export-Package', 'com.google.gson,com.google.gson.stream,com.google.gson.annotations,com.google.gson.reflect'
}
}
}
The output of the script is as follows:
gradle artifactoryPublish
:generatePomFileForOsgiBundlesPublication
:artifactoryPublish
Deploying artifact: http://localhost:8081/artifactory/gradle-dev-local/com/google/code/gson/bundle-jars/2.8.0/bundle-jars-2.8.0.pom
Deploying build descriptor to: http://localhost:8081/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://localhost:8081/artifactory/webapp/builds/bundle-jars/1489323863518
When I look in the artifactory repository the structure is not what I expected:
+- com
+--- google/code/gson/bundle-jars
|+-- 2.8.0
| +- bundle-jars.pom
+--- maven-metadata.xml
The wrong directory structure (google/code/gson/bundle-jars), where I expected several sub directorties (google, code, gson) with a 2.8.0 and a jar file.
I think I have to change the publications block, but I don't know what it should be.
I use unpuzzle (or rather this fork) to create Maven artifacts from OSGi bundles (and publish them to Artifactory).
This is probably not the most efficient solution for your use case, but at least something that works and I can come up with fast.
Here is an example of where I use unpuzzle for this purpose. Maybe that can serve as a starting point (together with the unpuzzle docs). There is a lot of bloat in my example because there I try to actually determine the original Maven artifacts for OSGi bundles created from them - as you always want the OSGi bundle, that's probably not relevant for you.
Note that by default the published artifacts will have different names (based on the bundle symbolic name) and a different group (which is configurable). But I think that is to be preferred over having the original group and name, otherwise it may get confused w/ the original. Adapting the group and name individually is possible as you can see in the example.
The publication should be as follows:
publishing {
publications {
osgiBundles(MavenPublication) {
groupId 'my.group'
artifactId 'com.google.code.gson'
version '2.8.0'
artifact file("build/plugins/com.google.code.gson_2.8.0.jar")
}
}
}

How can get both rev and revConstraint for dynamic dependency in published ivy.xml of Gradle build

I have a question about Ivy publishing in Gradle.
From Ivy, I expect that if I publish an artifact with for example this dependency:
<dependency org="org.apache.ant" name="ant" rev="1+"/>
My published ivy.xml gets both the fixed and the dynamic version:
<dependency org="org.apache.ant" name="ant" rev="1.9.6" revConstraint="1+"/>
I want that also in Gradle. I have Gradle 2.10.
Here is my Gradle project:
apply plugin: "java"
group = 'org.wibble'
version = "1.2.3"
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.ant', name: 'ant', version: '1+' // resolves to version 1.9.6 at the time of writing
}
uploadArchives.repositories {
ivy { name "testrepo"; url "$buildDir/testrepo" }
}
If I run gradle uploadArchives the resulting ivy.xml just has this:
<dependency org="org.apache.ant" name="ant" rev="1+" conf="compile->default"/>
In the source code of Gradle I do see that there is a facility for writing both rev and revConstraint:
if (!dep.getDynamicConstraintDependencyRevisionId().equals(dependencyRevisionId)) {
<...>
writer.attribute("revConstraint", dep.getDynamicConstraintDependencyRevisionId().getRevision());
}
With debugging I also see that this code is hit, but, in my case both getDynamicConstraintDependencyRevisionId and dependencyRevisionId give '1+' at this point, and the 1.9.6 version is forgotten at this point.
What can I do to get the dependency version recorded in the published ivy.xml, just like in Ivy?
Not sure if this is of any help anymore. But I had to address a similar issue recently, and came up with the following approach. Hope it can help some one else looking for a solution. I derived to this answer based on clues from RaGe's Pom solution and gradle forum solution.
The reason I had to implement this was, the ivy being published by gradle had rev, but not revConstraint attribute. But rev always said "latest.release" which is what is used for our internal libraries. So I had to manually put the rev to actual revision and the revConstraint to latest.release.
publishing {
publications {
ivyJava(IvyPublication) {
from components.java
configurations.create('sources')
artifact(sourceJar) {
type "source"
conf "sources"
classifier "sources"
}
configurations.create('javadoc')
artifact(javadocJar) {
type "javadoc"
conf "javadoc"
classifier "javadoc"
}
descriptor {
withXml {
if (project.configurations.findByName("runtime") != null) {
Map resolvedVersionMap = [:]
Configuration runtimeConfiguration = project.configurations.getByName('runtime')
ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
resolution.getAllComponents().each { ResolvedComponentResult versionResult ->
resolvedVersionMap.put("${versionResult.moduleVersion.group}:" +
":${versionResult.moduleVersion.name}", versionResult.moduleVersion.version)
}
asNode().dependencies.dependency.each { dep ->
if ("latest.release".equalsIgnoreCase(dep.#rev) || "latest.integration".equalsIgnoreCase(dep.#rev)
|| "latest.snapshot".equalsIgnoreCase(dep.#rev)) {
dep.#revConstraint = dep.#rev
dep.#rev = resolvedVersionMap.get("${dep.#org}:" +
":${dep.#name}")
dep.#changing = "true"
}
}
}
}
}
}
}
}

Resources