Is it possible to add a custom type of dependency?


(Matt Khan) #1

I need to expand my build to encompass some jruby code which relies on some gems. As far as I know, there is no existing wrapper that transposes a gem into a maven style dependency (though jruby can consume maven dependencies). It would be nice if I could do something like

dependencies {
    compile gem('json')
}

the intention being that behind the scenes I’d do something like the “gem in a jar” approach, i.e. install the gem to a specific location and then jar that location up (if I wanted to repackage it into maven) or point a file dependency (if I didn’t)

Is there any way for me to implement this sort of custom extension to a core class at the moment?

Thanks Matt


(Peter Niederwieser) #2

You can add a ‘gem’ method to the ‘dependencies’ block, if that’s what you are asking for. The method should return a (lazy) ‘FileCollection’. See the “Writing Custom plugins” chapter in the Gradle user guide for how to add new methods to Gradle’s object model. If this is just for your build(s), it might be good enough to declare a top-level ‘gem’ method, for example in the root project or (as a closure) in a script plugin. In either case, the main work will be implementing that method.


(Matt Khan) #3

Can you be a bit more specific about where in that doc it talks about adding methods to the object model? I can see the project.container() part but not adding a method.

My Q really had 2 sides;

  1. is it possible to add in a new implementation of Dependency? i.e. extend the core structure/behaviour of gradle 2) how can I declare a dependency that, when it is resolved (or when it is declared if need be) does some custom logic (in my case, transforming a gem into a jar)?

Cheers Matt


(Peter Niederwieser) #4

See “51.3. Getting input from the build”. It’s not the only way to extend the object model, but the preferred one. Using the extension mechanism, you can’t add a method directly to the dependencies block, but you can add a JavaBean that contains a method.

ad 1. I don’t think so, but this may not be needed in your case.

ad 2. As I said, your method has to return a ‘FileCollection’. This object should only resolve when its files are requested. A simple way to get a FileCollection is to implement a lazy java.lang.Iterable and convert it to a FileCollection with ‘project.files()’.