I’ve been using Gradle on my current project. So far I really like it. It follows the ‘build by convention’ idea that Maven uses and is even easier to extend and add to than Ant. One of the main reasons is the build file is actually just a Groovy file. So adding logic is simple.
One of the problems with any new tool, though, is wide spread support. Usually the IDE department is the biggest issue here. Gradle uses Ivy for dependency management, but it does it under the covers. You specify your dependencies in the build.gradle file using the Groovy syntax. It’s actually nicer than XML. The issue is getting your IDE to recognize the dependencies declared in your build file. There aren’t any plug-ins for this yet. This is no good as far as I’m concerned, because it makes adding jars to your project way too hard.
Today I decided I needed to add Mockito to my project, and ran into this issue of trying to figure out how to have it in my Gradle build file and on my Eclipse classpath. It annoyed me enough that I considered just dropping Gradle and going with Maven. Then I realized that since the build file is just Groovy, it should be pretty simple to have an ivy.xml file and just read it in the build file and insert the dependencies programmatically. It turns out this was pretty simple. Here’s how it worked.
In Gradle you specify your dependencies by declaring a dependency section in the build file like this:
You can also represent a dependency as a list of strings like this:
It’s important to note that all that is happening here is you are passing a list of strings to a method called ‘compile’ and ‘testCompile’. So, I realized all I have to do is write a function to build these strings off of the ivy.xml file.
Using the XML processing power of Groovy this is actually really easy. I changed the dependency section to look like this;
This code just parses the ivy.xml file and looks for dependencies that match the conf you specified (or have no conf at all) and adds them to the list of dependencies. It ignores some of the advanced features of Ivy, but it works for what I needed it to. Now I can just use IvyDE in Eclipse and have the same dependencies in my IDE as Gradle uses when it builds.
What a neat solution!
ReplyDeleteNice solution!
ReplyDeleteHave you also tried https://github.com/unsaved/gradle-ivyxml-plugin ?
No, I haven't tried that. Since Eclipse has a nice Gradle plugin now, I don't really use an ivy.xml file any more.
ReplyDeleteFacing issues converting dependencies from ivy.xml to gradle . Not finding a way to run the task. Could you please help me in this ?
ReplyDelete