Download non jar dependency in gradle

We are using gradle as our build tool. Recently, we had to download a third party war which is a maven artifact and bundle it into an RPM which was our distributable. So in some sense the war was a dependency to our project.

As an initial solution, we download the war manually from the maven repository and checked it into our source. The gradle script when building the rpm would then take the war from the source tree and package it along with the RPM. Obviously, we faced troubles when we had to increment the version of the war. We did not want to manually download the war again and replace the war in the source tree.

As a second increment, we downloaded the war using wget from gradle during build time and then packaged it into the RPM. This was a better solution than first one because we did not have to manually download the war every time we had to update the version. But this solution suffered from two problems. One, it expects wget to be installed in your system and two, we loose out on the dependency handling capabilities of gradle. For example, you could specify a + in the version and gradle would automatically fetch the latest version of the war and you don’t even have to manually change the version in the build.gradle file.

So we came up with the third approach with a bit of help. The solution is to add the war as a dependency under a custom configuration and then use the resolve method to download the war. 

configurations {
mrsWar
}

dependencies {
mrsWar "org.openmrs.web:openmrs-webapp:${openmrsVersion}@war"
}

task downloadMRSWar << {
new File("${buildDir}/resources/main").mkdirs();
configurations.mrsWar.resolve().each { file ->
//Copy the file to the desired location
}
}
}

This goes to prove the point that custom dependency configurations is one of the primary reasons why you should switch to gradle if you are using maven.