Wednesday 27 August 2014

Faking it with Gradle Repositories

There are days with Gradle when you need to pull files from a location that is not a Maven or Ivy repository. Gradle does provide a flatDir repository, but that is restricted in that files have to be local and they can only be referenced as :artifactId:version. As a regular Gradle user you probably want to use the group:artifactId:version or group:artifactId:version:classifier@ext format.

What if you want to pull from say Sourceforge, Google Projects or a legacy build artifact server ? There used to be a repositories plugin for Gradle, but it no longer seems to work with later Gradle versions. The good news is that it is possible in many cases to achieve this with Ivy + pattern layout.

Ivy usually requires an XML file (Ivy file) to describe its transitive dependencies, but in Gradle if the Ivy file does not exist, then it is simply assumed that there are no transitive dependencies. This is good for working with arbitrary artifact stores as they will not have such files. Managing transitive dependencoes are now up to you, the build script author, but you probably know this anyway as you are already doing something that is not of the ordinary.

In concept.gradle the artifact pattern string contains a number of fields. Those in square brackets are field names, those in parenthesis are optional sections.

To get a better understanding of what these fields mean, I suggest reading the Ivy terminology page. For now let's just see how the standard Mavane items you are familiar with in Gradle will map to these fields.

  • group - This maps to organisation.
  • artifactId - Effectively maps to both module and artifact. Strictly speaking not the same, but in practice this is usuaaly how it works out.
  • version - Maps to revision
  • classifier - This seems to be seldom used, is usually optional, but maps to classifier.
  • ext - Refers to the extension of the artifact. Is optional and maps to ext.
How would you go about mapping this in practice? Let's assume you have a project that is dependent on libusb on SourceForge. The latter has a flexible folder structure, so once the Files section has been inspected one can write an appropriate pattern as is shown in sourceforge.gradle

Notice that the ext field is not in parenthesis, meaning that in this case I expect the extension to be passed.

Finally to prove that it work we can create a little build script tryIt.gradle and running it with gradle --info -b tryIt.gradle tryIt should provide output similar to output.txt.

No comments:

Post a Comment