Maven publishing a subset of classes

Keywords: service code, publish script, client code, simple setup, repo, transitive, generate, pom, upload, package, couple, project. Powered by TextRank.

I have been trying to get a client for a http rest service that I'm running published to a Maven repo. The client code exists within the same project as the service code. The gradle publish script should include only a couple of classes, package them and upload them to a repo. This simple setup is easy to accomplish until you realize that the transitive dependencies are missing.

Setting it up needs this code

// package the classes for the client
task clientJar(type: Jar, dependsOn: classes) {
	includeEmptyDirs = false
	from sourceSets.main.output
	include ("**\\client\\*")
}
publishing {
	publications {
		mavenJar(MavenPublication) {
			artifact clientJar
		}
	}
	repositories {
		maven {
            // repo details
		}
	}
}

To generate the correct POM that includes the transitive dependencies, you need another approach. Basically use the POM that from components.java would generate but publish the artifact from the clientJar task.

...
	publications {
		internal(MavenPublication) {
			from components.java
			pom.withXml {
				pomString = asString().toString()
			}
		}
		mavenJar(MavenPublication) {
			artifact clientJar
			pom.withXml {
				def builder = asString()
				builder.delete(0, builder.length())
				builder.append(pomString)
			}
		}
	}


Metadata

First published on 2022-04-29

Generated on May 29, 2024, 10:00 PM

Index

Mobile optimized version. Desktop version.