I’m having a gradle project that create artifacts that should be consumed by a maven project. Some of the dependencies that I would like to describe in the pom.xml created from the maven-publish plugin is not jars, and needs to have the correct so the maven project can download the correct artifact.
How is the “gradle way” to do this. One solution I came up with was to modify the pom in the pom.withXml block, but I’m hoping there is a better way.
/Hazze
An example project that shows the issue en alternative solution:
settings.gradle:
rootProject.name = 'testproject'
include 'p1'
include 'p2'
build.gradle:
allprojects {
group = 'testproject'
version = '1.0'
repositories {
jcenter()
}
}
p1/build.gradle:
plugins {
id 'base'
id 'maven-publish'
}
def rpmFile = file("$buildDir/rpms/my-package.rpm")
def rpmArtifact = artifacts.add('archives', rpmFile) {
type 'rpm'
builtBy 'rpm'
}
task rpm() {
outputs.file rpmFile
doLast {
rpmFile << 'file contents'
}
}
publishing {
publications {
maven(MavenPublication) {
artifact rpmArtifact
}
}
}
p2/build.gradle:
plugins {
id 'java-library'
id 'maven-publish'
}
dependencies {
implementation project(path:':p1', configuration:'archives')
//A maven dependency works and generates the correct <type> element in the produced pom.
implementation 'org.sonarsource.sonarqube:sonar-application:7.9.1@zip'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
pom.packaging 'pom'
//Ugly workaround to set the <type> element.
pom.withXml {
asNode().dependencies.'*'.each() {
if( it.groupId.text() == group && it.artifactId.text() == 'p1' ) {
it.appendNode('type','rpm')
}
}
}
}
}
}