1 | initial version |
To automatically version dependencies while using maven-publish with platform dependencies in Gradle, you can use the GenerateModuleMetadata
task. Here are the steps:
Make sure you have the maven-publish
plugin added to your Gradle project:
plugins {
id 'maven-publish'
}
Define your platform dependencies in the dependencies
block:
dependencies {
implementation platform('org.example:platform:1.0')
implementation 'org.example:library'
}
Configure the publishing
block to use the maven-publish
plugin:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url 'http://your-maven-repo'
}
}
}
Add the GenerateModuleMetadata
task to automatically version the platform dependency:
tasks.named("generateMetadataFileForMavenJavaPublication") {
enabled = true
outputs.upToDateWhen { false }
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
// Automatically version the platform dependency
pom {
withXml {
def dependenciesNode = asNode().appendNode("dependencies")
configurations.compileClasspath.allDependencies.each { dependency ->
if (dependency instanceof PlatformDependency) {
def depNode = dependenciesNode.appendNode("dependency")
depNode.appendNode("groupId", dependency.group)
depNode.appendNode("artifactId", dependency.name)
depNode.appendNode("version", dependency.version)
depNode.appendNode("scope", "import")
}
}
}
}
}
}
}
With this setup, the GenerateModuleMetadata
task will automatically version the platform dependency in the generated POM file. When you run ./gradlew publish
, the POM file with the updated dependency will be published to your Maven repository.