We must configure a Maven repository to publish our configured publication. We can choose a local directory or a repository manager, such as Artifactory or Nexus. Gradle also adds support installing the publication to our local Maven repository.
Gradle already adds our local Maven repository as a destination for our publications. For each named publication, there is a publish<publicationName>ToMavenLocal
task. Gradle also creates the publishToMavenLocal
task, which will publish all publications to the local Maven repository.
We have the following example build file:
apply plugin: 'maven-publish' apply plugin: 'java' version = '2.1.DEVELOPMENT' group = 'book.gradle' repositories { jcenter() } dependencies { compile 'org.springframework:spring-context:4.1.4.RELEASE' } publishing { publications { publishJar(MavenPublication) { artifactId = 'sample' from components.java } } }
From the command line, we will run the publishToMavenLocal
task and see which tasks are executed:
$ gradle publishToMavenLocal :generatePomFileForPublishJarPublication :compileJava :processResources UP-TO-DATE :classes :jar :publishPublishJarPublicationToMavenLocal :publishToMavenLocal BUILD SUCCESSFUL Total time: 5.135 secs
You may have noticed that first the publication artifact is created with the jar
task and its task dependencies. Also, the POM file is generated, and our publication is copied to the local Maven repository via the publishPublishJarPublicationToMavenLocal
task, which is a task dependency for publishToMavenLocal
.
When we look at the local Maven repository directory, we see that our project artifact is published:
/Users/mrhaki/.m2/repository/ book └── gradle └── sample ├── 2.1.RELEASE │ ├── sample-2.1.RELEASE.jar │ └── sample-2.1.RELEASE.pom └── maven-metadata-local.xml
If we have our own company's Maven repository or a directory where we want to publish our publications, then we must add it to the publishing
configuration block. Inside the block, we can add the repositories
configuration block containing one or more named repositories. For the combination of each publication and repository, Gradle creates a task with the publish<publicationName>To<repositoryName>Repository
name pattern.
Next, we will define a simple directory repository in the next example build file with the name localRepo
:
apply plugin: 'maven-publish' apply plugin: 'java' version = '2.1.DEVELOPMENT' group = 'book.gradle' repositories { jcenter() } dependencies { compile 'org.springframework:spring-context:4.1.4.RELEASE' } publishing { publications { publishJar(MavenPublication) { artifactId = 'sample' from components.java } } // Add a Maven repository for // the publications. repositories { maven { name = 'localRepo' url = "$buildDir/localRepo" } } }
First, we will run the
tasks
task to see which task is added to the Publishing tasks
group:
$ gradle tasks ... Publishing tasks ---------------- generatePomFileForPublishJarPublication - Generates the Maven POM file for publication 'publishJar'. publish - Publishes all publications produced by this project. publishPublishJarPublicationToLocalRepoRepository - Publishes Maven publication 'publishJar' to Maven repository 'localRepo'. publishPublishJarPublicationToMavenLocal - Publishes Maven publication 'publishJar' to the local Maven repository. publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache. ... BUILD SUCCESSFUL Total time: 4.514 secs
To publish our project's artifact, we can execute the publishPublishJarPublicationToLocalRepoRepository
or publish
task. The following output shows the tasks that are executed:
$ gradle publish :generatePomFileForPublishJarPublication :compileJava :processResources UP-TO-DATE :classes :jar :publishPublishJarPublicationToLocalRepoRepository Uploading: book/gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar to repository remote at file:/Users/mrhaki/Projects/book/sample/build/localRepo/ Transferring 2K from remote Uploaded 2K :publish BUILD SUCCESSFUL Total time: 5.012 secs
Once the task is performed, we get the following files in the
build/localRepo
directory:
build/localRepo/ └── book └── gradle └── sample ├── 2.1.DEVELOPMENT │ ├── sample-2.1.DEVELOPMENT.jar │ ├── sample-2.1.DEVELOPMENT.jar.md5 │ ├── sample-2.1.DEVELOPMENT.jar.sha1 │ ├── sample-2.1.DEVELOPMENT.pom │ ├── sample-2.1.DEVELOPMENT.pom.md5 │ └── sample-2.1.DEVELOPMENT.pom.sha1 ├── maven-metadata.xml ├── maven-metadata.xml.md5 └── maven-metadata.xml.sha1
To publish our publications to an Artifactory repository with a Maven layout, we only have to configure the repository in the publications.repositories
configuration block. We can set the url
property, a name
, and optional security credentials.
In the next example build file, we use an Artifactory repository to which we publish the publication:
apply plugin: 'maven-publish' apply plugin: 'java' version = '2.1.DEVELOPMENT' group = 'book.gradle' repositories { jcenter() } dependencies { compile 'org.springframework:spring-context:4.1.4.RELEASE' } publishing { publications { publishJar(MavenPublication) { artifactId = 'sample' from components.java } } // Add a Artifactory repository for // the publications with Maven layout. repositories { maven { name = 'artifactory' url = "http://localhost:8081/artifactory/libs-release-local" // Username and password should be // saved outside build file in // real life, eg. in gradle.properties // or passed via command line as // project properties. credentials { username = 'user' password = 'passw0rd' } } } }
Gradle creates a new publishPublishJarPublicationToArtifactoryRepository
task based on the publication name and the repository name. When we invoke the task, we can see that the publication is deployed to the Artifactory repository. The following code shows this:
$ gradle publishPublishJarPublicationToArtifactoryRepository :generatePomFileForPublishJarPublication :compileJava :processResources UP-TO-DATE :classes :jar :publishPublishJarPublicationToArtifactoryRepository Uploading: book/gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar to repository remote at http://localhost:8081/artifactory/libs-release-local Transferring 2K from remote Uploaded 2K BUILD SUCCESSFUL Total time: 5.012 secs
When we open the Artifactory web application in a web browser, we can see that our project is now part of the repository, as shown in the following screenshot:
Another repository manager is Nexus. Publishing to a Nexus repository manager is not much different than publishing to Artifactory or a local directory. We only have to change the url
property to reference the repository and set the optional security credentials.
In the following example build file, we use a Nexus repository manager:
apply plugin: 'maven-publish' apply plugin: 'java' version = '2.1.DEVELOPMENT' group = 'book.gradle' repositories { jcenter() } dependencies { compile 'org.springframework:spring-context:4.1.4.RELEASE' } publishing { publications { publishJar(MavenPublication) { artifactId = 'sample' from components.java } } // Add a Maven repository for // the publications. repositories { maven { name = 'nexus' url = "http://localhost:8081/nexus/content/repositories/releases" credentials { username = 'admin' password = 'admin123' } } } }
This time, the
publishPublishJarPublicationToNexusRepository
task is created. The task is also added as a task dependency to the publish
task. To accomplish this, use the following code:
$ gradle publishPublishJarPublicationToNexusRepository :generatePomFileForPublishJarPublication :compileJava :processResources UP-TO-DATE :classes :jar :publishPublishJarPublicationToNexusRepository Uploading: book/gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar to repository remote at http://localhost:8081/nexus/content/repositories/releases Transferring 2K from remote Uploaded 2K BUILD SUCCESSFUL Total time: 5.012 secs
When we take a look with the Nexus web application inside the repository, we can see that our project is added to the repository, as shown in the following screenshot:
18.219.236.70