3

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

and in the sub-project I have:

tasks.build.dependsOn install sourceSets { main { resources { srcDirs = [ "src/main/resources", "build/poms" ] } } } 

This will generate ./build/poms/pom-default.xml, but it will not add it to the JAR.

Creating a dependency on an earlier phase than build creates circular dependencies (and I don't know whether this is the problem anyway).

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?

1
  • This seems over-complicated. It depends on what you are trying to do of course, but you can have module-B import module-A by adding: dependencies { compile project(':moduleA') } to the build.gradle file for module-B Commented Nov 2, 2019 at 19:19

1 Answer 1

3

You should be able to include the POM in your JAR by adding the following to your subprojects closure:

jar { into("META-INF/maven/${project.group}/${project.name}") { from generatePomFileForMavenPublication rename { it.replace('pom-default.xml', 'pom.xml') } } } 

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.

(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.)

Sign up to request clarification or add additional context in comments.

5 Comments

This is where this whole gradle thing is incredibly baffling to me. Can I ask some follow-up questions? 1) Where in the docs would I start reading to find the breadcrumbs of info to eventually get to this answer myself? 2) pom-default.xml is in a very different directory: how does it find that? 3) if I am looking for the docs for each one of the nouns in your answer, how do I go about that? E.g. where does "rename" and "it" and "into" come from?
I'll try. The JAR is created by the Jar task, which has an into() method because it's also a CopySpec. rename is also a method of the Jar task. it is Groovy syntax and refers to the current object of an iterator. Taking files from a task refers to the other task's declared outputs.
@JohannesErnst What I found helpful when diving into Gradle, is to setup a modern IDE (i.e. IntelliJ) which allows me to click into all these strange methods (into, rename, it) and get the developers documentation in the (open) source code.
Note that to be able to 'mvn install-file' the jar, I had to place the pom.xml in a META-INF subfolder like "META-INF/maven/${project.group}/${project.name}".
Good point, this makes the example even better for practical use. @Alex

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.