When and how would you declare more than one MavenPublication in a single project, and what governs the choice of software component for each?
answer
- publications is a NamedDomainObjectContainer
- each publication: unique name + a component
- library from components["java"]
- BOM from components["javaPlatform"]
- avoid two publications with same GAV
basics
~10 sAdd several entries to publications {}, each create<MavenPublication>(name) { from(components[...]) } with a distinct name and (usually) a different component — for example a java library plus a platform/BOM component, or differently-coordinated variants.
solid answer
~40 sThe `publications` container holds many publications, so you can register multiple `MavenPublication`s in one project. Each needs a unique name (which drives its generated task names) and a source of artifacts — normally `from(components[...])`. You'd do this when one project must publish more than one logical module: e.g. the main library `from(components["java"])` and a BOM/platform `from(components["javaPlatform"])` (from the `java-platform` plugin), or a publication that combines the library plus extra artifacts under alternate coordinates. The component you pick determines the artifacts and dependency metadata of that publication; two publications pointing at the *same* coordinates would collide, so multiple publications typically differ in coordinates (group/artifact) or in the component they expose. Keep them minimal — most libraries publish exactly one.
code
kotlin · 10 linesplugins { `java-platform`; `maven-publish` }
publishing {
publications {
create<MavenPublication>("platform") {
from(components["javaPlatform"])
artifactId = "my-bom"
}
}
}go deeper
Know that one project usually has a single publication.
Recognize you can add multiple entries to the publications container, each from a component.
Explain valid multi-publication scenarios (library + platform/BOM) and the coordinate-collision pitfall.
Design publishing topology across modules (BOM module, aggregation, adhoc components) and governance of coordinates.
## The publications container is a collection `publishing { publications { } }` is a `NamedDomainObjectContainer<Publication>`. Nothing stops you from registering several `MavenPublication`s in the same project; each is independent. ## Legitimate reasons for more than one - **Library + platform/BOM**: apply `java-platform` to publish a BOM from `components["javaPlatform"]`, while a sibling sub-project (or, less commonly, a wrapper) publishes the actual library from `components["java"]`. - **Multiple components in one project**: a project that defines a custom `AdhocComponentWithVariants` (via `softwareComponentFactory`) might expose more than one consumable component. - **Alternate coordinates**: re-publishing the same outputs under a second artifactId (e.g. a relocation/alias) — each publication carries its own coordinates. ## What governs the component choice The component dictates the publication's artifacts *and* dependency metadata. So the decision is: "what consumable thing am I describing?" — a normal jar library (`java`), a dependency-constraints-only platform (`javaPlatform`), or a hand-built adhoc component. ## Collisions to avoid Two publications resolving to the *same* GAV will clash on upload. When you have multiple publications, give each distinct coordinates or a distinct component; otherwise the repository sees duplicate modules. ```kotlin publishing { publications { create<MavenPublication>("library") { from(components["java"]) } create<MavenPublication>("platform") { // requires the java-platform plugin from(components["javaPlatform"]) artifactId = "my-bom" } } } ``` ## Default behavior reminder Most projects need exactly one publication. Reach for multiples only when you genuinely publish more than one logical module from a single build.
- What component do you publish from for a BOM/platform?`components["javaPlatform"]`, contributed by the `java-platform` plugin; it publishes dependency constraints/managed versions rather than a jar.
- What goes wrong if two MavenPublications resolve to the same GAV?They collide at upload time — the repository receives duplicate coordinates. Each publication should have distinct coordinates or a distinct component.
saying these in an interview costs you the question
- Adding multiple publications for the same artifact without changing coordinates — they collide.
- Assuming every project needs more than one publication; the common case is exactly one.