gradle publish custom pom - maven

I have written a gradle task which writes a custom pom to "build/libs/pom.xml"
I want to publish above custom pom.xml , so I defined :
def pomXml = artifacts.add('archives', file("$buildDir/libs/pom.xml")) {
builtBy('writePoms') // writePoms is my custom task to create custom pom.xml
}
publishing {
publications {
maven(MavenPublication) {
artifact pomXml
artifactId "myartifact"
groupId 'com.xyz'
version project.version
}
}
repositories {
// Task for manually publishing the maven image. When CI works, setup CI_TOKEN auth.
maven {
url <url>
name "GitLab"
credentials(HttpHeaderCredentials) {
name = System.getenv("CI_JOB_TOKEN") ? "Job-Token" : "Private-Token"
value = System.getenv("CI_JOB_TOKEN") ? System.getenv("CI_JOB_TOKEN") : gitLabPrivateToken
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
When I run ./gradlew publish it publishes pom from "build/publications/maven/pom-default.xml" instead of "build/libs/pom.xml"
Can someone help how can I achieve it?

I had a similar issue (aggregate build of multiple external repositories with their own build systems and then publishing the results of those builds to a local artifactory).
I was able to effectively replace the generated POM for the "artifacts" publication with the content of an external POM via withXml:
pluginManager.withPlugin("maven-publish") {
extensions.configure(PublishingExtension::class.java) {
publications.register<MavenPublication>("artifacts") {
groupId = "com.example"
artifactId = "external"
version = "1.0.0"
artifact("external.jar")
pom.withXml {
asNode().setValue(XmlParser().parse("some/external/pom.xml").value())
}
}
}
}
This leaves the <xml ...> and <project ...> nodes intact (acceptable for my case).

Related

How do I publish file created by zip task in gradle

I'd like to publish the output of my custom zip task to a maven repo. My problem is that when I set the artifact of the publication as the zip task, the file that gets zipped is not what gets published. Instead what gets published is the value given to the "from" argument of my custom zip task. How would I make it so that "jar.zip" is the file that is published?
tasks.register('zipJars', Zip) {
archiveFileName = "jar.zip"
destinationDirectory = layout.buildDirectory.dir("${projectDir.parentFile}/DesktopAndroid/jars/zipped")
from fatJarDev
}
publishing {
publications {
apkBuilding(MavenPublication){
artifact zipJars
}
}
repositories {
maven {
name = 'Local'
url = "file://${rootDir}/Repository"
}
}
}```
Ah - are you referring to the name of the file in the maven repo ? you will need to customise it in publication ;
see https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html
publishing {
publications {
apkBuilding(MavenPublication){
artifact zipJars
artifactId 'zipjars'
}
}
repositories {
maven {
name = 'Local'
url = "file://${rootDir}/Repository"
}
}
}

Configure description for package published to the GitHub Packages Gradle registry

When publishing a package to the GitHub Packages Gradle registry, the resulting package's description is empty.
Is it possible to set the package description via config?
I have the following (abridged) configuration in build.gradle.kts, I was assuming it would use pom.description or the repo's README.md as the description but that doesn't seem to be the case.
plugins {
`maven-publish`
}
publishing {
publications {
create<MavenPublication>("mavenJava") {
groupId = project.group.toString()
artifactId = rootProject.name
version = project.version.toString()
from(components["java"])
pom {
name.set("steam-webapi-kt")
description.set("Steam WebAPI wrapper in Kotlin and Ktor")
url.set("https://github.com/j4ckofalltrades/steam-webapi-kt")
}
}
}
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/j4ckofalltrades/steam-webapi-kt")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
Sample package: https://github.com/j4ckofalltrades/steam-webapi-kt/packages/899640

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.

Build and publish test classes with gradle.kts

We have a multiproject gradle repository with several subprojects. One of the subprojects is supposed to generate separate testJar and publish it to the local maven repository:
// build a repositories-testJar.jar with test classes for services tests
tasks.register<Jar>("testJar") {
archiveClassifier.set("testJar")
from(sourceSets["test"].output)
}
// the testJar must be built within the build phase
tasks.build {
dependsOn(tasks.withType<Jar>())
}
// needed for publishing plugin to be aware of the testJar
configurations.create("testJar") {
extendsFrom(configurations["testCompile"])
}
artifacts {
add("testJar", tasks.named<Jar>("testJar"))
}
This works and I can see -testJar.jar is generated within the /build/libs.
Then, within the rootProject gradle.kts we set up a publishing extension:
allprojects {
configure<PublishingExtension> {
publications {
create<MavenPublication>(project.name) {
groupId = "foo.bar"
artifactId = project.name.toLowerCase().replace(":", "-")
version = "ci"
from(components["kotlin"])
}
}
repositories {
maven {
url = uri("file://${rootProject.rootDir}/../m2")
}
}
}
}
This also normally works and I can see modules being published in the m2 directory. However the testJar is not published. I couldn't find a way how to attach the testJar artifact to the publication defined in root project.
Solved with addition of:
publishing {
publications {
create<MavenPublication>(project.name + "-" + testArtifact.name) {
groupId = "my.group"
artifactId = project.name.toLowerCase().replace(":", "-")
version = "version"
artifact(testArtifact)
}
}
}

I want to publish multiple artifacts with different names with gradle and maven-publish

I have a library containing usual classes and those especially for unit tests. So I want to publish two separate artifacts from the same project. My working solution with the maven plugin looks like this:
task jarTest (type: Jar, dependsOn: testClasses) {
from sourceSets.test.output
archiveBaseName.set('foo-test')
description = 'test utilities'
}
artifacts {
archives jarTest
}
uploadArchives {
repositories {
mavenDeployer {
//...
addFilter('foo') {artifact, file ->
artifact.name == 'foo'
}
addFilter('foo-test') {artifact, file ->
artifact.name == 'foo-test'
}
}
}
}
Unfortunately the maven plugin is deprecated and will be removed in Gradle 7. maven-publish is the suggested replacement and I'm looking for a replacement solution.
My current attempt looks like
publishing {
publications {
mavenJava(MavenPublication) {
artifact jar
artifact jarTest
}
}
}
There is the obvious problem that there are two artifacts with the same name.
Setting the name like this does not work:
artifactId = jar.archiveBaseName
this neither:
afterEvaluate {
artifactId = jar.archiveBaseName
}
It's possible to configure the artifact like this
artifact(jar) {
classifier "src"
extension "zip"
}
But there is no property for the name regarding the documentation (https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenArtifact.html).
So I'm looking for sth. like the addFilter from the maven plugin.

Resources