Where in the build script do you attach a custom artifact, and what's the minimal correct snippet to add a fat JAR to a Maven publication?
answer
- publishing { publications { ... } }
- on the publication, call artifact(...)
- from(components.java) = main jar
- pass tasks.named("fatJar")
- add classifier to avoid clash
basics
~10 sInside publishing { publications { ... } }, on the publication, call artifact(tasks.named("fatJar")). That single line adds the fat JAR as an extra file on that publication's coordinates.
solid answer
~30 sYou attach it inside the `maven-publish` plugin's DSL: `publishing { publications { create<MavenPublication>("maven") { from(components["java"]); artifact(tasks.named("fatJar")) } } }`. The `from(components.java)` adds the standard jar and dependency metadata; the `artifact(...)` call adds your extra file. You typically give it a classifier so it doesn't collide with the main jar. Make sure the `maven-publish` (and usually `java`) plugins are applied, and that the `fatJar` task is a `Jar`/`Zip` so Gradle can read its archive output and wire the build dependency. That's all that's required for `./gradlew publish` to upload both files.
code
kotlin · 15 linesplugins {
`java-library`
`maven-publish`
}
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
artifact(tasks.named("fatJar")) {
classifier = "all"
}
}
}
}go deeper
Know the exact DSL location and the one-line artifact(tasks.named(...)) call with a classifier.
Explain the interplay with from(components.java) and why a classifier is needed.
Note plugin prerequisites, that the task form auto-wires the build, and collision avoidance.
Standardize publication setup (conventions plugin) so all modules attach extra artifacts consistently.
## Plugins and the DSL location Custom artifacts are added through the **`maven-publish`** plugin (or `ivy-publish`). Apply it, then configure the `publishing` extension: ```kotlin plugins { `java-library` `maven-publish` } publishing { publications { create<MavenPublication>("maven") { from(components["java"]) // main jar + dependencies artifact(tasks.named("fatJar")) { // extra file classifier = "all" } } // repositories { ... } where to publish } } ``` Key points for a junior to get right: - The `artifact(...)` call lives **on the publication**, inside `publications { yourPub { ... } }` — not at the top level, not inside `dependencies`. - `from(components["java"])` provides the *main* artifact and POM dependencies; `artifact(...)` is purely additive. - Pass the **task** (`tasks.named("fatJar")`), not a path, so the file is found and the build is ordered correctly. - Add a `classifier` if the extra file would otherwise clash with the main jar (same empty classifier + `jar` extension). ## What gets produced After `./gradlew publish` you'll see, in the repo, both `mylib-1.0.jar` and `mylib-1.0-all.jar` (plus the POM/metadata). The fat jar shares the coordinates; the `all` classifier distinguishes it. ## Common first mistakes - Putting `artifact(...)` outside the publication block (compile/DSL error). - Omitting the classifier and colliding with the main jar. - Forgetting to apply `maven-publish` so there's no `publishing` extension at all.
- Which plugin must be applied for the publishing DSL to exist?`maven-publish` (or `ivy-publish` for Ivy). Without it there is no `publishing` extension and `publications`/`artifact(...)` are unavailable.
- What does `from(components.java)` add versus `artifact(...)`?`from(components.java)` adds the main jar plus dependency metadata to the publication; `artifact(...)` only attaches one extra file. They're complementary.
saying these in an interview costs you the question
- Putting `artifact(...)` inside `dependencies { }` instead of on the publication.
- Forgetting to apply `maven-publish`.
- Omitting a classifier so the fat jar collides with the main jar.