Chapter 5. Publishing to a Maven Repository

In the previous chapter, you learned how to use the Upload task to publish your project artifacts. In this chapter, you will learn more about the new and still-developing feature of publishing your artifacts to a Maven repository.

You will learn about the new publishing mechanism in Gradle. This feature is currently still under development, and that means the implementation might change in the future. But for now, this way of publishing artifacts will be the default.

Defining publication

We must add the maven-publish plugin to our project to add the new publication feature of Gradle. The plugin allows us to define and deploy our project artifacts in the Maven format. This means our deployed project can be used by other developers and projects that support the Maven format. For example, other projects could use Gradle or Maven and define a dependency to our published artifacts.

The maven-publish plugin is based on a general publishing plugin. The publishing plugin adds a new publishing extension to our project. We can use a publications configuration block in our build script to configure the artifacts we want to publish and the repositories we want to deploy to. The publications extension has the PublishingExtension type in the org.gradle.api.publish package. The plugin also adds the general life cycle publish task to the project. Other tasks can be added as task dependencies to this task; thus, with a single publish task, all the publications in the project can be published.

The maven-publish plugins also add extra task rules to the project. There is a task to generate a Maven POM file for each publication in the project. The plugins also add a new task rule to publish each publication to the local Maven repository. Finally, a task rule is added based on a combination of the publication and the repository, to publish a publication to the specified repository.

Let's create an example build file and apply the maven-publish plugin to see the new task:

apply plugin: 'maven-publish'
apply plugin: 'java'

Now, we will invoke the tasks task from the command line:

$ gradle tasks
...
Publishing tasks
----------------
publish - Publishes all publications produced by this project.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
...
BUILD SUCCESSFUL

Total time: 4.647 secs

We can see the publish and publishToMavenLocal tasks in the output. The dynamic task rules for publishing single publications to repositories are not shown.

To configure our publications, we must first add a publishing configuration block. Inside the block, we define the publications configuration block. In this block, we define a publication. A publication defines what needs to be published. The maven-publish plugin expects a publication to have the MavenPublication type found in the org.gradle.api.publish.maven package. Besides the artifacts that need to be published, we can also define details for the generated POM file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.174