Defining repositories

We must configure an Ivy repository to publish our configured publication. We can choose a local directory or a repository manager, such as Artifactory or Nexus.

Publishing to a local directory

If we have a directory where we want to publish our publications, we must add it to the publishing configuration block. Inside the block, we add a 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.

We define a simple directory repository in the next example build file with the name localRepo:

apply plugin: 'ivy-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(IvyPublication) {
      module = 'sample'

      from components.java
    }
  }

  // Add a local director as repository
  // for the publications.
  repositories {
    ivy {
      name = 'localRepo'
      url = "$buildDir/localRepo"
    }
  }
}

First, we run the tasks task to see which task is added to the Publishing tasks group:

$ gradle tasks
...
Publishing tasks
----------------
generateDescriptorFileForPublishJarPublication - Generates the Ivy Module Descriptor XML file for publication 'publishJar'.
publish - Publishes all publications produced by this project.
publishPublishJarPublicationToLocalRepoRepository - Publishes Ivy publication 'publishJar' to Ivy repository 'localRepo'.
...
BUILD SUCCESSFUL

Total time: 11.514 secs

To publish our project's artifact, we can execute the publishPublishJarPublicationToLocalRepoRepository or publish tasks. The following output shows the tasks that are executed:

$ gradle publish
:generateDescriptorFileForPublishJarPublication
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:publishPublishJarPublicationToLocalRepoRepository
:publish

BUILD SUCCESSFUL

Total time: 6.383 secs

Once the task has been run, we get the following files in the build/localRepo directory:

build/localRepo/
└── book.gradle
    └── sample
        └── 2.1.DEVELOPMENT
            ├── ivy-2.1.DEVELOPMENT.xml
            ├── ivy-2.1.DEVELOPMENT.xml.sha1
            ├── sample-2.1.DEVELOPMENT.jar
            └── sample-2.1.DEVELOPMENT.jar.sha1

Publishing to Artifactory

To publish our publications to an Artifactory repository, 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 will use an Artifactory repository to publish the publication to:

apply plugin: 'ivy-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(IvyPublication) {
      module = 'sample'

      from components.java
    }
  }

  // Add a Artifactory repository for
  // the publications with Maven layout.
  repositories {
    ivy {
      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.
      credentials {
        username = 'user'
        password = 'passw0rd'
      }
    }
  }
}

Gradle creates a new task, publishPublishJarPublicationToArtifactoryRepository, 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, as shown in the following code:

$ gradle publishPublishJarPublicationToArtifactoryRepository
:generateDescriptorFileForPublishJarPublication
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:publishPublishJarPublicationToArtifactoryRepository
Upload http://localhost:8081/artifactory/libs-release-local/book.gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar
Upload http://localhost:8081/artifactory/libs-release-local/book.gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar.sha1
Upload http://localhost:8081/artifactory/libs-release-local/book.gradle/sample/2.1.DEVELOPMENT/ivy-2.1.DEVELOPMENT.xml
Upload http://localhost:8081/artifactory/libs-release-local/book.gradle/sample/2.1.DEVELOPMENT/ivy-2.1.DEVELOPMENT.xml.sha1

BUILD SUCCESSFUL

Total time: 12.214 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:

Publishing to Artifactory

Publishing to Nexus

Another repository manager is Nexus. To publish to a Nexus repository manager is not much different from publishing to Artifactory or the local directory. We only have to change the url property to reference the repository and set the correct optional security credentials.

In the following example build file, we use a Nexus repository manager:

apply plugin: 'ivy-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(IvyPublication) {
      module = 'sample'

      from components.java
    }
  }

  // Add a Nexus repository for
  // the publications.
  repositories {
    ivy {
      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. The following code shows this:

$ gradle publishPublishJarPublicationToNexusRepository
:generateDescriptorFileForPublishJarPublication
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:publishPublishJarPublicationToNexusRepository
Upload http://localhost:8081/nexus/content/repositories/releases/book.gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar
Upload http://localhost:8081/nexus/content/repositories/releases/book.gradle/sample/2.1.DEVELOPMENT/sample-2.1.DEVELOPMENT.jar.sha1
Upload http://localhost:8081/nexus/content/repositories/releases/book.gradle/sample/2.1.DEVELOPMENT/ivy-2.1.DEVELOPMENT.xml
Upload http://localhost:8081/nexus/content/repositories/releases/book.gradle/sample/2.1.DEVELOPMENT/ivy-2.1.DEVELOPMENT.xml.sha1

BUILD SUCCESSFUL

Total time: 5.746 secs

When we take a look at the Nexus web application inside the repository, we can see that our project is added to the repository, as shown in the following screenshot:

Publishing to Nexus
..................Content has been hidden....................

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