Compile using Gradle

This PR is based on #553 with a few minor improvements and squashing the commits.

It's meant to allow working with Gradle and Maven at the same time. If maven support is dropped we can do more improvements to the Gradle scripts.