Ask Your Question
4

How to automatically version dependencies while using maven-publish with platform dependencies in gradle?

asked 2022-07-27 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-01-13 19:00:00 +0000

david gravatar image

To automatically version dependencies while using maven-publish with platform dependencies in Gradle, you can use the GenerateModuleMetadata task. Here are the steps:

  1. Make sure you have the maven-publish plugin added to your Gradle project:

    plugins {
       id 'maven-publish'
    }
    
  2. Define your platform dependencies in the dependencies block:

    dependencies {
       implementation platform('org.example:platform:1.0')
       implementation 'org.example:library'
    }
    
  3. Configure the publishing block to use the maven-publish plugin:

    publishing {
       publications {
           mavenJava(MavenPublication) {
               from components.java
           }
       }
       repositories {
           maven {
               url 'http://your-maven-repo'
           }
       }
    }
    
  4. 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-27 11:00:00 +0000

Seen: 8 times

Last updated: Jan 13 '22