生成一个带有所有依赖项的 Gradle 项目的 pom 文件应该是这样的:

huangapple go评论58阅读模式
英文:

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

问题

我有一个Gradle项目,它将依赖项存储在toml文件中。我想生成带有依赖项的pom文件。这是我的build.gradle文件:

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
        }
    }
}

当我运行以下命令时:

> gradle publishToMavenLocal

我只能从libs.bundles.api获得带有依赖项的pom文件。对于compileOnly、testImplementation、annotationProcessor和testAnnotationProcessor作用域,没有依赖项。请帮助解决这个问题。

英文:

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.

答案1

得分: 1

发布的POM文件只包含运行时依赖项,这些依赖项是用于使用和运行项目的。

compileOnly

这些只需要用于编译您的项目,而不需要用于运行项目。

testImplementation

这些只需要用于运行项目的测试,而不需要用于运行它作为依赖项。

annotationProcessor

这些仅在构建项目期间使用,不需要在运行时使用。

testAnnotationProcessor

这些仅在构建项目测试时需要,不需要供其他人使用您的项目时。

要执行您想要的操作,您需要自定义发布的POM文件:

build.gradle.kts

这是使用Kotlin DSL编写的,但可以轻松转换为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
英文:

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&lt;MavenPublication&gt;(&quot;fakePom&quot;) {
            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&lt;Node&gt;()
                    .map { it as Node }
                    .filter { &quot;dependencies&quot; == it.name() || &quot;dependencyManagement&quot; == it.name() }
                    .forEach {
                        root.remove(it)
                    }

                val ds = root.appendNode(&quot;dependencies&quot;)

                allDeps.forEach { d -&gt;
                    val dn = ds.appendNode(&quot;dependency&quot;)
                    dn.appendNode(&quot;groupId&quot;, d.moduleGroup)
                    dn.appendNode(&quot;artifactId&quot;, d.name)
                    dn.appendNode(&quot;version&quot;, d.moduleVersion)
                }
            }
        }
    }
}
./gradlew generatePomFileForFakePomPublication

huangapple
  • 本文由 发表于 2023年7月6日 20:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定