gradlemaven-publish

How to generate pom file for a gradle project with all dependencies?


I have a Gradle project which stores dependencies in toml file. I want to generate pom file with dependencies. This is my build.gradle file:

plugins {
    id 'maven-publish'
}

dependencies {
    api ( libs.bundles.api) {
        exclude(group: 'org.apache.hadoop', module: 'zookeeper')
        exclude(group: 'org.apache.hadoop', module: 'slf4j-log4j12')
        exclude(group: 'org.apache.hadoop', module: 'log4j')
        exclude(group: 'org.apache.hadoop', module: 'jsp-api')
        exclude(group: 'org.apache.hadoop', module: 'servlet-api')
    }
    compileOnly(libs.slf4j.api)
    testImplementation(libs.bundles.test.implementation)
    annotationProcessor(libs.lombok)
    testAnnotationProcessor(libs.lombok)
}

publishing {
    publications {
        customPublication(MavenPublication) {
            from components.java
        }
    }
}

When I run

gradle publishToMavenLocal

I get pom file with dependencies only from libs.bundles.api. There aren't dependencies for scope compileOnly, testImplementation, annotationProcessor, testAnnotationProcessor. Please help to figure out this problem.


Solution

  • A published POM file will only contain the runtime dependencies. The dependencies needed to consume and run the project. And that is correct.

    compileOnly

    These are only needed to compile your project, and not needed to run your project.

    testImplementation

    These are only needed to run the tests of your project and not needed to run it as a dependency.

    annotationProcessor

    These are only used during the build of your project and are not needed to run it.

    testAnnotationProcessor

    These are only needed during the build of your tests of your project and not needed for a consumer to use your project.

    In order to do what you want, you need to customize the POM in publishing:

    build.gradle.kts

    This is in Kotlin DSL, but can easily be converted to Groovy DSL.

    publishing {
        publications {
            create<MavenPublication>("fakePom") {
                this.pom.withXml {
                    val allDeps = project.configurations.runtimeClasspath.get().resolvedConfiguration.firstLevelModuleDependencies +
                        project.configurations.compileClasspath.get().resolvedConfiguration.firstLevelModuleDependencies +
                        project.configurations.testRuntimeClasspath.get().resolvedConfiguration.firstLevelModuleDependencies +
                        project.configurations.testCompileClasspath.get().resolvedConfiguration.firstLevelModuleDependencies
    
                    val root = asNode()
    
                    root.children()
                        .filterIsInstance<Node>()
                        .map { it as Node }
                        .filter { "dependencies" == it.name() || "dependencyManagement" == it.name() }
                        .forEach {
                            root.remove(it)
                        }
    
                    val ds = root.appendNode("dependencies")
    
                    allDeps.forEach { d ->
                        val dn = ds.appendNode("dependency")
                        dn.appendNode("groupId", d.moduleGroup)
                        dn.appendNode("artifactId", d.name)
                        dn.appendNode("version", d.moduleVersion)
                    }
                }
            }
        }
    }
    
    ./gradlew generatePomFileForFakePomPublication