问题描述
我试图在使用gradle创建的jar
中包含生成的pom.xml
.
I'm attempting to include a generated pom.xml
in the jar
that I'm creating with gradle.
到目前为止,在我的父项目中,我有
So far, in my parent project, I have
subprojects {
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
}
在子项目中,我有:
tasks.build.dependsOn install
sourceSets {
main {
resources {
srcDirs = [ "src/main/resources", "build/poms" ]
}
}
}
这将生成./build/poms/pom-default.xml
,但不会将其添加到JAR中.
This will generate ./build/poms/pom-default.xml
, but it will not add it to the JAR.
在比build
更早的阶段创建依赖关系会创建循环依赖关系(而且我也不知道这是否是问题所在).
Creating a dependency on an earlier phase than build
creates circular dependencies (and I don't know whether this is the problem anyway).
此外,我希望pom.xml
出现在名称为pom.xml
(而不是pom-default.xml
)的META-INF
内部,因此无论如何这都不是正确的方法.
Also, I'd like the pom.xml
to show up inside META-INF
with name pom.xml
(not pom-default.xml
), so this may not be the right approach anyway.
以某种方式,我认为它不可能像看起来那样复杂?
Somehow I'm thinking it can't be as complicated as this looks?
推荐答案
通过将以下内容添加到您的subprojects
闭包中,您应该能够在您的JAR中包含POM:
You should be able to include the POM in your JAR by adding the following to your subprojects
closure:
jar {
into('META-INF') {
from generatePomFileForMavenPublication
rename { it.replace('pom-default.xml', 'pom.xml') }
}
}
如果您已经有jar
闭包,则可以在其中添加它.这会自动在 generatePomFileForMavenPublication 任务上创建任务依赖项,以便在创建JAR时存在POM文件.
不需要您问题中的sourceSets
部分.
If you already have a jar
closure, you can add it there. This automatically creates a task dependency on the generatePomFileForMavenPublication task, so that the POM file is there when the JAR is created.
The sourceSets
part from your question would not be required for this.
(附带说明:完全没有必要这样做,因为Maven发布过程会将POM作为单独的工件发布.)
(Side note: It would not be strictly necessary to do this at all, because the Maven publish process will publish the POM as an individual artifact anyway.)
这篇关于在gradle中将pom.xml包含在Jar中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!