Gradle

Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. Powered by a Groovy DSL and packed with innovation, Gradle provides a declarative way to describe all kinds of builds through sensible defaults. Gradle is quickly becoming the build system of choice for many open source projects, leading edge enterprises and legacy automation challenges.

— from Gradle package description

Gradle is quickly gaining popularity as flexible Java build system, which can be used in situations requiring writing custom build logic. Flexibility means that Gradle build scripts can differ significantly across different projects and there is no universal way of packaging Gradle projects. However, in most common situations the following steps can are necessary:

  1. Add build-dependency on gradle-local

  2. In the %build section of the spec use %gradle_build macro to invoke Gradle in local, offline mode to build the project

  3. In the %install section, use %mvn_install macro to install Maven artifacts produced during build

  4. Use generated file .mfiles lists to populate %files section with -f switch

Common spec file sections
BuildRequires:  gradle-local
...

%build
%gradle_build

%install
%mvn_install -J ...

%files -f .mfiles

%files javadoc -f .mfiles-javadoc

%gradle_build macro is very similar to %mvn_build and it takes the same arguments. It is a wrapper for gradle command which enables so called "local mode" of artifact resolution - dependency artifacts are resolved from local system before trying other repositories (system Maven repositories are used through XMvn). By default %gradle_build invokes Gradle with build goal, but when tests are skipped (option -f) then assemble goal is invoked instead.

In most simple cases calling %gradle_build should be enough to resolve all dependencies, but sometimes additional patching of build script may be necessary. The following example patch adds XMvn resolver to make Gradle correctly resolve dependencies of build script itself.

Patch enabling local mode
diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle
index a3cb553..50dd2a4 100644
--- a/buildSrc/build.gradle
+++ b/buildSrc/build.gradle
@@ -21,6 +21,7 @@ apply plugin: 'idea'
 apply plugin: 'eclipse'

 repositories {
+    xmvn()
     maven { url 'https://repo.gradle.org/gradle/libs' }
     mavenCentral()
 }

Once Gradle build completes, all Maven artifacts produced by the build will be marked for installation. %mvn_install macro, which should be called from the %install section of spec file, automatically handles installation of all marked artifacts and generates .mfiles file lists, which are used to define package contents through %file sections. If you are building Javadocs (recommended) then you should call %mvn_install with -J argument pointing to directory containing generated Javadoc documentation.

Most of Maven macros starting with %mvn_ can also be used when building packages with Gradle. This includes macros that control which artifacts should be installed where and how (%mvn_package, %mvn_alias, %mvn_file, %mvn_compat_version), %mvn_artifact that is used to mark additional artifacts as installable and %mvn_config which can be used to add extra config for XMvn.