How do you apply the ivy-publish plugin and declare a basic Ivy publication in a Gradle build?
answer
- plugins { `ivy-publish` }
- publishing { publications { } }
- create<IvyPublication>
- from(components["java"])
- generates ivy.xml descriptor
basics
~10 sApply the ivy-publish plugin, then inside publishing { publications { } } create a publication of type IvyPublication and add a component such as from components.java.
solid answer
~30 sFirst apply the plugin in the `plugins {}` block with `id("ivy-publish")`. That plugin contributes the `publishing` extension. Inside it you register a named publication of type `IvyPublication` via the `create<IvyPublication>("name")` (Kotlin) DSL. The publication needs content — typically `from(components["java"])` to publish the main JAR and its dependencies. The plugin then auto-generates tasks like `generateDescriptorFileForIvyPublication` and `publishIvyPublicationToMavenLocal`/`...ToRepository`. You separately declare Ivy `repositories {}` (under `publishing`) for the actual destinations. Unlike `maven-publish`, an Ivy publication produces an `ivy.xml` descriptor and uses organisation/module/revision coordinates rather than groupId/artifactId/version.
code
kotlin · 17 linesplugins {
`java-library`
`ivy-publish`
}
publishing {
publications {
create<IvyPublication>("ivyJava") {
from(components["java"])
}
}
repositories {
ivy {
url = uri(layout.buildDirectory.dir("ivy-repo"))
}
}
}go deeper
Recall the two steps: apply ivy-publish, then create<IvyPublication> and add from(components.java).
Explain the publishing extension's two containers and that the plugin auto-generates descriptor + publish tasks.
Contrast ivy.xml vs pom.xml, organisation/module/revision vs GAV, and when an org would still pick Ivy.
Discuss governing publication format choice across an org and migration paths between Ivy and Maven repositories.
## What the ivy-publish plugin is `ivy-publish` is the modern Gradle plugin for publishing artifacts and metadata in the **Apache Ivy** format. It is the Ivy counterpart of `maven-publish`. Applying it adds a `publishing` extension (a `PublishingExtension`) to the project, which exposes two containers: `publications {}` and `repositories {}`. ## Applying the plugin You apply it declaratively in the `plugins {}` block — there is no version because it is a core Gradle plugin: ```kotlin plugins { `java-library` `ivy-publish` } ``` ## Declaring a publication A publication is a named, typed entry in the `publications {}` container. For Ivy you declare an `IvyPublication`. In the Kotlin DSL the idiom is `create<IvyPublication>("name") { ... }`; in Groovy it is `myPub(IvyPublication) { ... }`. A publication on its own is empty — you give it content with: - `from(components["java"])` — publishes a software component (main JAR + dependency metadata), or - `artifact(someTaskOrFile)` — attaches an individual artifact. ## Generated tasks From each publication + repository pair, the plugin lazily registers tasks: - `generateDescriptorFileFor<PubName>Publication` — writes the `ivy.xml`. - `publish<PubName>PublicationTo<RepoName>Repository` — uploads to a named repository. - An aggregate `publish` task depends on them all. ## Coordinates and descriptor Ivy identifies modules by **organisation / module / revision** (analogous to Maven's group / artifact / version) and emits an `ivy.xml` descriptor rather than a `pom.xml`. You can override these and customize descriptor metadata (author, description, license) inside the publication block. ```kotlin publishing { publications { create<IvyPublication>("ivyJava") { from(components["java"]) organisation = "com.example" module = "my-lib" revision = "1.0.0" } } repositories { ivy { url = uri(layout.buildDirectory.dir("repo")) } } } ```
- What file does an Ivy publication generate that a Maven publication does not?An `ivy.xml` descriptor (the Ivy module descriptor) instead of a `pom.xml`. Gradle may also publish a `module` Gradle Metadata file alongside it.
- Where do you declare the destination repository for an Ivy publication?In the `repositories {}` block inside the `publishing` extension, using an `ivy { url = ... }` entry — distinct from the dependency-resolution `repositories {}` at the top level.
saying these in an interview costs you the question
- Confusing the top-level dependency `repositories {}` with the `publishing { repositories {} }` block.
- Saying ivy-publish produces a pom.xml — it produces ivy.xml.
- Forgetting to give the publication content with from(...) or artifact(...).