Reader small image

You're reading from  Gradle Dependency Management

Product typeBook
Published inJun 2015
Reading LevelIntermediate
Publisher
ISBN-139781784392789
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Hubert Klein Ikkink
Hubert Klein Ikkink
author image
Hubert Klein Ikkink

Hubert Klein Ikkink was born in 1973 and lives in Tilburg, the Netherlands, with his beautiful wife and gorgeous children. He is also known as mrhaki, which is simply the initials of his name prepended by mr. He studied Information Systems and Management at the Tilburg University. After finishing his studies he started to work at a company which specialized in knowledge-based software. There he started writing his first Java software (yes, an applet!) in 1996. Over the years his focus switched from applets, to servlets, to Java Enterprise Edition applications, to Spring-based software. In 2008 he wanted to have fun again when writing software. The larger projects he was working on were more about writing configuration XML files, tuning performance and less about real development in his eyes. So he started to look around and noticed Groovy as a good language to learn about. He could still use existing Java code, libraries, and his Groovy classes in Java. The learning curve isn’t steep and to support his learning phase he wrote down interesting Groovy facts in his blog with the title Groovy Goodness. He posts small articles with a lot of code samples to understand how to use Groovy. Since November 2011 he is also a DZone Most Valuable Blogger (MVB); DZone also posts his blog items on their site. In 2010, 2011, and 2012 Hubert was invited to speak at Gr8Conf in Copenhagen, Denmark. This is a very good conference with all the project leaders of Groovy and Groovy-related projects. In November 2010 he presented a Gradle talk at the J-Fall conference of the Dutch Java User Group. In November 2011 he presented about the new features in Groovy 1.8 at the same conference. The conference is visited by 1000 Java developers and he got the chance to educate some of them about the greatness of Gradle and Groovy. Hubert works for a company called JDriven in the Netherlands. JDriven focuses on technologies that simplify and improve development of enterprise applications. Employees of JDriven have years of experience with Java and related technologies and are all eager to learn about new technologies. Hubert works on projects using Grails and Java combined with Groovy and Gradle.
Read more about Hubert Klein Ikkink

Right arrow

Chapter 7. Publishing to an Ivy Repository

You learned in a previous chapter how we can publish our project artifacts to a Maven repository. In this chapter, we are going to use an Ivy repository to which we can publish.

Just as with publishing to a Maven repository, we are going to use the new and incubating publishing feature in Gradle to publish to an Ivy repository.

Defining publications


We must add the ivy-publish plugin to our project to be able to publish our artifacts to an Ivy repository. The plugin allows us to use the Ivy format to describe our artifacts that need to be published.

The ivy-publish plugin is based on the 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 a general life cycle publish task to the project. Other tasks can be added as task dependencies to this task, so with a single publish task, all the project's publications can be published.

The ivy-publish plugins also adds some extra task rules to the project. There is a task to generate an Ivy descriptor file to each publication in the project. The plugins also add...

Defining publication artifacts


A publication we define must have a unique name in our project. We can add multiple publications with their own name inside a publications configuration block. To add an artifact, we can use the artifact method in the publication definition. We can also use the artifacts property to directly set all artifacts.

We can define the artifact with the artifact method in the ways described in the following table:

Type

Description

AbstractArchiveTask

The information for the artifact is extracted from the archive task. The artifact is an instance of PublishArtifact in the org.gradle.api.artifacts package.

File

The information for the artifact is extracted from the filename.

Map

This is another way to define artifacts. The map must contain a source key referencing a file or archive task and other properties we can use, such as classifier and extension, to further configure the artifact.

Using archive task artifacts


In the following example build file, we define a new publication with the publishJar name and we define the output of the jar archive task as an artifact:

apply plugin: 'ivy-publish'
apply plugin: 'java'

// Configuration block for publishing
// artifacts from the project.
publishing {

  // Define publications with what
  // needs to be published.
  publications {

    // Name of this publication
    // is publishJar.
    publishJar(IvyPublication) {

      // Use output of jar task
      // as the artifact for
      // the publication.
      artifact jar

      // Alternatively we can use
      // a Map notation:
      // artifact source: jar
    }

  }
}

Next, we run the tasks task, and in the output, we see new generated tasks to publish this publication:

$ gradle tasks
...
Publishing tasks
----------------
generateDescriptorFileForPublishJarPublication - Generates the Ivy Module Descriptor XML file for publication 'publishJar'.
publish - Publishes all publications...

Generating Ivy descriptor files


An important part of an Ivy publication is the descriptor file. We already saw that Gradle added a generateDescriptorFile<publicationName> task to our project. Furthermore, we can define some properties of the descriptor file inside a publication configuration. Gradle also offers a hook to customize the generated descriptor file even further.

Gradle uses the project's version, group, name, and status properties for the info element in the Ivy descriptor file generated. We will create a new example build file where we define the project properties, so they will be included in the file:

apply plugin: 'ivy-publish'
apply plugin: 'java'

// Defined project properties, that are
// used in the generated descriptor file.
// The name of the project is by default
// the directory name, but we can
// change it via a settings.gradle file
// and the rootProject.name property.
version = '2.1.RELEASE'
group = 'book.gradle'

repositories {
  jcenter()
}

dependencies...

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'

     ...

Summary


In this chapter, you learned how to use the new and incubating ivy-publish plugin. You saw how we can declare our publications with the publications configuration block. Gradle will automatically create new tasks based on what we have declared as publications.

You also learned how to customize the Ivy descriptor file that is generated by Gradle publishing tasks.

Finally, you saw how we can configure repositories to deploy our publications to. We used a local directory using the file protocol, and we used the Artifactory and Nexus repository managers.

In this book, we saw how we can define the dependencies we need in our project. You learned how to customize the dependency resolution and how to define the repositories that store the dependencies.

Then, you learned how we can deploy our project as dependencies for others. We saw how we can publish to a Maven repository, including Bintray, and an Ivy repository. You now have the knowledge to manage dependencies in your Java projects with...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Gradle Dependency Management
Published in: Jun 2015Publisher: ISBN-13: 9781784392789
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Hubert Klein Ikkink

Hubert Klein Ikkink was born in 1973 and lives in Tilburg, the Netherlands, with his beautiful wife and gorgeous children. He is also known as mrhaki, which is simply the initials of his name prepended by mr. He studied Information Systems and Management at the Tilburg University. After finishing his studies he started to work at a company which specialized in knowledge-based software. There he started writing his first Java software (yes, an applet!) in 1996. Over the years his focus switched from applets, to servlets, to Java Enterprise Edition applications, to Spring-based software. In 2008 he wanted to have fun again when writing software. The larger projects he was working on were more about writing configuration XML files, tuning performance and less about real development in his eyes. So he started to look around and noticed Groovy as a good language to learn about. He could still use existing Java code, libraries, and his Groovy classes in Java. The learning curve isn’t steep and to support his learning phase he wrote down interesting Groovy facts in his blog with the title Groovy Goodness. He posts small articles with a lot of code samples to understand how to use Groovy. Since November 2011 he is also a DZone Most Valuable Blogger (MVB); DZone also posts his blog items on their site. In 2010, 2011, and 2012 Hubert was invited to speak at Gr8Conf in Copenhagen, Denmark. This is a very good conference with all the project leaders of Groovy and Groovy-related projects. In November 2010 he presented a Gradle talk at the J-Fall conference of the Dutch Java User Group. In November 2011 he presented about the new features in Groovy 1.8 at the same conference. The conference is visited by 1000 Java developers and he got the chance to educate some of them about the greatness of Gradle and Groovy. Hubert works for a company called JDriven in the Netherlands. JDriven focuses on technologies that simplify and improve development of enterprise applications. Employees of JDriven have years of experience with Java and related technologies and are all eager to learn about new technologies. Hubert works on projects using Grails and Java combined with Groovy and Gradle.
Read more about Hubert Klein Ikkink