I'm looking at porting a maven build to gradle. One feature of maven is pom inheritance whereby I can declare a variety of common behaviour in a pom, publish that to a repository and then use that via the <parent>
element in a concrete project.
My Q is simply whether there is an equivalent behaviour in gradle?
I've previously done this in ant+ivy by importing a common build.xml which relied on either having already checked out the location of the common build.xml from source control or using something like svn:externals. I can repeat this approach without any real difficulty but this seems to be one thing maven does quite nicely so it would be nice to see something similar in gradle.
My current solution is option 3; package the common scripts into a jar as resources and then unjar during the buildscript section like so
This seems to do what I want.
There are two possibilities:
Publish a build script to a web server, and include it with
apply from: "http://path/to/script.gradle"
Write a Gradle plugin, publish it as a Jar to a Maven or Ivy repository, and include it with:
The second option is more complicated, but also somewhat more powerful. For example, plugin Jars will be cached, whereas remote build scripts currently won't. In general, I recommend to start with 1., and move to 2. if and once it becomes necessary. In the future, Gradle will likely offer a mechanism which combines the ease of use of 1. with the advantages of 2.
Here's an improvement to the accepted solution, for when you have more than one dependency in your buildscript:
Worked like a charm for me.
Buildings on Matt’s solution, I find the following to be a bit cleaner:
Just my two cents. :-)
I have an answer and another question:
First, to access a shared file from a repository (i.e. Nexus) you can build a URL that includes a query:
I did this for our project and it works great. I can manage the 'build-common.gradle' file in a separate SVN project and upload it to Nexus as a SNAPSHOT. The above URL (with appropriate values inserted for 'server-url', 'repository-name', and 'group-name') finds the latest SNAPSHOT of my .gradle script I uploaded. No need to package it in a jar.
My version: