Can I use the Java plugin to create META-INF/maven and contents in my JAR?


(Thad Humphries) #1

I’m very new to Gradle. As my first task, I’m creating build.gradle scripts for some Maven libraries I use.

I’ve tried both the Java plugin’s uploadArchive and the maven-publish plugin to put the *.jar and *.pom files into my local repository (I prefer uploadArchive). The files are usable by other Gradle scripts and Maven pom.xml files. The latter is important as these libraries will later be used by licensees outside my company.

When I create my JAR files with Maven, the org.apache.maven.plugins.maven-jar-plugin adds to a JAR’s META-INF a directory ‘META-INF/maven/$project.group/$project.name’ that contains my pom.xml and generated pom.properties file. Gradle’s Java jar does not do this.

Question: How can I create this directory tree in my Gradle-produced JAR, generate the pom.properties file and add it, and add the Gradle-generated pom? This isn’t strictly necessary, I know, but doing so would simplify my work when in comes to distributing my JAR files.


(Peter Niederwieser) #2

Adding contents to a Jar is easy (see docs). Getting access to the generated POM may be easier with the new ‘maven-publish’ plugin (vs. the ‘maven’ plugin), which provides a separate task for generating the POM. However, the new plugin still has some limitations, and you may have to post-process the POM (using a ‘taskName.pom.withXml’ hook) to fix dependency scopes and potentially other information.

There is likely also a way to get access to the generated POM with the old ‘maven’ plugin, but I can’t tell offhand how it works. Perhaps the Gradle User Guide has more on this.