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 4. Publishing Artifacts

In the previous chapters, we learned how to define and use dependencies in our projects. However, the code we write in our projects can also be a dependency for another project. In order for another project to use our code as a dependency, we should publish our code as a dependency artifact so that it can be used by other projects.

In this chapter, you will learn how you can define artifacts in your project. These artifacts need to be published for others to use them. We first publish them using a filesystem, so the artifacts can be used from the same computer or even if we use a network share on an intranet. In later chapters, we will see how to publish our artifacts to a Maven repository, an Ivy repository, and Bintray.

Defining artifact configurations


A Gradle project can contain artifacts we want to publish. An artifact can be a ZIP or JAR archive file or any other file. We can define one or more artifacts in one project. Thus, we don't have to create two different projects if we want to have two different artifacts from the same source tree.

In Gradle, we group artifacts using configurations. We used configurations to define dependencies for our project, but now we will use the configurations to group our artifacts that can be dependencies for others. So a configuration can contain both dependencies and artifacts. If we apply the Java plugin to our project, we get a configuration named archives, that contains the default JAR artifact for the project.

In the following example Gradle build file, we use the Java plugin. We add a task to display the filename of the artifact that belongs to the archives configuration. The following code shows this:

apply plugin: 'java'

// Set the archivesBaseName property,...

Defining artifacts


In the previous section, you learned that the Java plugin adds an archives configuration that is used to group artifacts from the project. Just as we created configurations for dependencies in our project, we can also create our own configurations for its artifacts. To assign an archive or file to this configuration, we must use the artifacts configuration block in our build script. Inside the configuration closure, we use the name of the configuration followed by the artifact. We can also further customize the artifact definition inside the artifacts block.

We can define artifacts with the following three types:

Creating artifacts


We saw how to define artifacts, but we also need to create artifacts in our build files. We can either use an archive task to create the artifact or a file can be an artifact. Most of the time, when we use Gradle in a Java project, we build an archive with compiled classes and resources. Actually, the Java plugin adds a jar task to our project that will just do that. The JAR file created is then added to the archives configuration.

In the next example build file, we will use the Java plugin and simply rely on the default artifact configuration and tasks. The following code shows this:

apply plugin: 'java'

// Define project properties.
group = 'com.mrhaki.sample'
version = '2.1'
archivesBaseName = 'sample'

// Extra task to check the artifacts.
task artifactsInfo << {
  configurations
    .findByName('archives')
    .allArtifacts
    .each { artifact ->
      println artifact.file.name
    }
}

We can now run the buildArchives task and check the artifacts with the...

Publishing artifacts to the local directory


We now know how to create one or more artifacts and how to use artifact configurations to group them. In this section, we will see how we can copy our artifacts to a local directory or network share. Remember that, for each artifact's configuration, Gradle adds a build<ConfigurationName> task and an upload<ConfigurationName> task. Now it is time to learn more about the upload<ConfigurationName> task so that we can copy our artifacts. In the following chapters we will also learn how to deploy to a Maven repository, an Ivy repository, and to Bintray.

For each upload<ConfigurationName> task, we must configure a repository definition. The repository definition is basically the destination of our artifacts when we upload or publish them. In this section, we use a local directory, so we define a repository using the flatDir method. We specify a name and the directory so that Gradle knows where the output of the upload<ConfigurationName...

Signing artifacts


We can digitally sign artifacts in Gradle with the signing plugin. The plugin supports generating Pretty Good Privacy (PGP) signatures. This signature format is also required for publication to Maven Central Repository. To create a PGP signature, we must install a few PGP tools on our computer. Installation of the tools is different for each operating system. On Unix-like systems, the software is probably available via a package manager. With the PGP software, we need to create a key pair that we can use to sign artifacts.

To sign artifacts, we must apply the signing plugin to our project. Then we must configure the plugin using a signing configuration block. We need to at least add information about our PGP key pair. We need the hexadecimal representation of the public key, the path to the secret key ring file with our private key, and the passphrase used to protect the private key. We assign this information to the keyId, secretKeyRingFile, and password properties of the...

Summary


In the previous chapters, you learned how to use external dependencies. In this chapter, you learned how you can define artifact configurations to assign your own artifacts. These artifacts can be dependencies for other developers on other projects and applications.

You also learned how to create a default artifact when you use the Java plugin. Next, we saw how to create more than one artifact from the same project.

You then learned how to configure an Upload task, so you can upload your artifacts to a local directory. This directory could also be a network share accessible to other development teams.

Finally, you learned how you can sign your artifacts using the signing plugin. This could be useful when you want to provide some extra confidence to people using the artifacts.

In the next chapters, you will see how you can upload your artifacts to a Maven repository, an Ivy repository, and Bintray.

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

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.artifact s package.

File

The information for the artifact is extracted from the filename. The artifact is an instance of ConfigurablePublishArtifact that extends PublishArtifact.

...