3

I want to publish a maven artifact from a project which includes all the classes from the submodules (like a fatJar) instead of having them as dependencies in the pom file in the published artifact.

Assuming I have a gradle project setup like

root
- moduleToPublish
- dependendencyOne
- dependendencyTwo

and I want to publish an artifact for moduleToPublish, I setup its buid.gradle like below (note the direct dependencies on the other submodules)

dependencies {
compile project(':dependencyOne')
compile project(':dependencyTwo')

compile other3PMavenDependencies...
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

When I run ./gradlew :moduleToPublish:publish, only classes from moduleToPublish are included in the artifact whereas the submodule dependencies land in the pom file along with the 3p dependencies.

In my case though, I would like to have the artifact include classes from those submodules as well and I am not sure how to get there. This is because I want all my consumers to be able to consume a single artifact that includes all dependencies from my project instead of me publishing the individual dependencies and then have the consumers use them separately. I have these modules split in gradle though because internal modules use parts of them at different times - so I do not want to merge them as a single gradle module.

I tried artifact ':dependencyOne' assuming it would use the MavenArtifact plugin but that only complained about more than one artifact being published. Any help here would be great.

noobNeverything
  • 212
  • 4
  • 14
  • you could customize the `jar` task from project `moduleToPublish` to include classes generated from the other two modules. something like : `jar { from (project(':dependendencyOne').sourceSets.main.output) }` – M.Ricciuti Nov 17 '18 at 17:22
  • Thanks - that actually worked. I was looking to tweak the `maven-publish` plugin instead of fixing it in the generated JAR. The only problem I have left is to remove these internal dependencies from the POM file being generated as part of `maven-publish` because without that, the consumer is still complaining about the missing dependency (even if its part of the generated JAR). The only solution I found and am playing around with is to modify the `pom` configuration in `maven-publish` to explicitly skip these internal dependencies. Is there a better approach to solve this? – noobNeverything Nov 17 '18 at 21:13
  • To be precise, I ended up using ``` pom.withXml { asNode().dependencies.dependency.each { dep -> if(dep.groupId.last().value().last().contains("")) { assert dep.parent().remove(dep) } } }``` – noobNeverything Nov 17 '18 at 21:54

0 Answers0