Gradle Primer

Gradle is one of the most versatile build tools, based on Groovy. It is extremely complex, and most of the programmers only require very little for their day to day activity. This article tries to provide a boost for whoever is currently using Ant or Maven and want or has to switch to Gradle.

For a regular project - a war, maybe an ear application with some dependencies - I would not say Gradle is better than Ant or Maven, however for more complex things, probably it is more efficient.

Besides, it is being promoted by Google. Their new Android Studio automatically creates Gradle build scripts for any new project.



  • Uses Maven style repositories
  • Java projects have group, artifact and version just like Maven
  • For each project there are two files, build.gradle and settings.gradle
  • settings.gradle merely gets executed before build.gradle
  • For jar, war and ear projects the folder structure is similar with Maven. Just plug your build.gradle and settings.gralde in the top folder and proceed
  • There are a lot of predefined tasks for java projects, however one can easily define a new task or inherit from an existent one
  • It is easy to define a dependency to another project by providing something similar with a relative file path


Here is a sample, ready to donwload, it contains the following:

  • One jar project with a number of external dependencies
  • One jar project that depends on the previously mentioned jar project
  • One war project that depends on the two above mentioned projects
  • One top level project which builds all the above

Please download the sample here: gradle_primer.

Please note that it does not work outside the box, as follows: either uncomment the mavenCentral() construct in the repositories in all build.gradle files or specify the right url for the alternate Maven repository you use.

Top level build script

The top level build script only loads and builds the underlying projects. There are as usual two files, build.gradle and settings.gradle.

The build.gradle is empty! The settings.gradle only imports the underlying projects:

  2. include "jar1", "jar2", "war1"

Note the project name: "jar1" means there is a folder called jar1 right underneath, where the jar1 project resides, pretty much like at Maven.

Now, if you want to point to a project that is in a folder on the same level with the folder that contains the current build script, you use this: ":jar1". That colon means first you go up one level and then down on the jar1 folder.

Jar file sample

We present the settings and build files for the jar2 project, becuase it shows external dependencies together with dependency to the jar1 project.

Here is the settings.gradle file:

  2. = 'jar2'
  3. is pretty much the only setting needed for a regular java project. This name is the artifactId. The other names - group and version - are usually in the build.gradle. This can be confusing so please pay attention.

Here is the build file:

  2. group 'com.test.alfa'
  3. version '1.0-SNAPSHOT'
  5. apply plugin: 'java'
  7. version = '1.0'
  8. sourceCompatibility = 1.8
  9. targetCompatibility = 1.8
  11. //create a single Jar with all dependencies
  12. jar {
  13. doLast {
  14. println "after building the jar file"
  15. }
  16. }
  18. //Get dependencies from Maven central repository
  19. repositories {
  20. mavenCentral()
  21. }
  23. //Project dependencies
  24. dependencies {
  25. compile "org.springframework:spring-webmvc:4.0.9.RELEASE"
  26. compile "javax.servlet:javax.servlet-api:3.1.0"
  27. compile "org.eclipse.persistence:org.eclipse.persistence.jpa:2.5.2"
  28. compile project(":jar1")
  29. }

Note the following:

  • See how the dependency to the jar1 project is defined - the colon meaning the jar1 project is on the same level with this jar2 project
  • See how the dependency to other projects in the maven repository is defined
  • Apply plugin java is required to show this is a java project
  • The plugin java defines the jar task who builds a jar. Simple.

The War Project

And now the war project. Most of the things you already saw in the jar2 project. settings.gradle as expected only defines the which is the artifact id. Will not list it anymore.

And here is the build.gradle file

  2. group 'com.test.alfa'
  3. version '1.0-SNAPSHOT'
  5. apply plugin: 'java'
  6. apply plugin: 'war'
  8. version = '1.0'
  9. sourceCompatibility = 1.8
  10. targetCompatibility = 1.8
  12. //create a single Jar with all dependencies
  13. war {
  14. archiveName "war1.war"
  15. doLast {
  16. println "after building the war file"
  17. }
  18. }
  20. //Get dependencies from Maven central repository
  21. repositories {
  22. // mavenCentral()
  24. maven {
  25. url "http://put the url here"
  26. }
  27. }
  29. //Project dependencies
  30. dependencies {
  31. compile "org.springframework:spring-webmvc:4.0.9.RELEASE"
  32. providedCompile "javax.servlet:javax.servlet-api:3.1.0"
  33. compile "org.eclipse.persistence:org.eclipse.persistence.jpa:2.5.2"
  34. compile project(":jar1"), project(":jar2")
  35. }

Note the following:

  • Aside of the java plugin, you need the war plugin who "knows" how to assemble the war file
  • The file name override: archiveName parameter in the war task. Otherwise, like in case of Maven, you end up with a war file with version etc in its name
  • providedCompile - regular dependency that will not be included in the war file - a good example is here, the servlet API is typically provided by the app server so you don't need to bundle it in the war file