Overview

This post walks you through how to create wrapper for Maven and Gradle build tools.

Wrapper is a script that invokes a declared version of Maven or Gradle. Developers can get up and running with a Maven or Gradle project quickly without installing them manually which saves a lot of time and building the projects become portable.

Maven Wrapper

Install Maven

If you have not installed Maven already, download it from http://maven.apache.org/download.cgi. You can choose either .zip or .tar.gz format. Once you have downloaded it, extract the package in a directory you prefer. On Windows, I installed it in D:\opt.

Edit system environement variables and add MAVEN_HOME variable. In order use mvn command in any directory while using CMD or Terminal, add also %MAVEN_HOME%\bin to PATH variable.

Add MAVEN_HOME variable

Add Maven to Path variable

Add Maven to Path variable

You can verify your Maven installation by running mvn -v command on CMD.

Verify Maven installation

Create Maven Wrapper

We are now going to generate wrapper. First go to your project root folder. Assuming your project is in D:\workspace\projects\my-app:

  > D:
  > cd workspace\projects\my-app
  > mvn -N io.takari:maven:0.7.7:wrapper
  D:\workspace\projects\my-app>mvn -N io.takari:maven:0.7.7:wrapper
  [INFO] Scanning for projects...
  [INFO]
  [INFO] ------------------< org.apache.maven:standalone-pom >-------------------
  [INFO] Building Maven Stub Project (No POM) 1
  [INFO] --------------------------------[ pom ]---------------------------------
  [INFO]
  [INFO] --- maven:0.7.7:wrapper (default-cli) @ standalone-pom ---
  [INFO]
  [INFO] Maven Wrapper version 0.5.6 has been successfully set up for your project.
  [INFO] Using Apache Maven: 3.6.3
  [INFO] Repo URL in properties file: https://repo.maven.apache.org/maven2
  [INFO]
  [INFO] ------------------------------------------------------------------------
  [INFO] BUILD SUCCESS
  [INFO] ------------------------------------------------------------------------
  [INFO] Total time:  3.447 s
  [INFO] Finished at: 2020-03-16T06:53:57+08:00
  [INFO] ------------------------------------------------------------------------

It should have generated a folder .mvn and commands mvnw for UNIX/Linux and mvnw.cmd for Windows. To check the generated wrapper:

> dir
 Volume in drive D is DATA
 Volume Serial Number is EC0A-13F8

 Directory of D:\workspace\projects\my-app

16/03/2020  06:53 AM    <DIR>          .
16/03/2020  06:53 AM    <DIR>          ..
16/03/2020  06:53 AM    <DIR>          .mvn
16/03/2020  06:53 AM            10,069 mvnw
16/03/2020  06:53 AM             6,607 mvnw.cmd
               2 File(s)        16,676 bytes
               3 Dir(s) 899,194,486,784 bytes free

Gradle Wrapper

Install Gradle

If you have not installed Gradle already, download it from https://gradle.org/releases/. Choose binary-only. Once you have downloaded it, extract the package in a directory you prefer. On Windows, I installed it in D:\opt.

Edit system environement variables and add GRADLE_HOME variable. In order use gradle command in any directory while using CMD or Terminal, add also %GRADLE_HOME%\bin to PATH variable.

Add GRADLE_HOME variable

Add Gradle to Path variable

Add Gradle to Path variable

You can verify your Gradle installation by running gradle -v command on CMD.

Verify Gradle installation

Create Gradle Wrapper

We are now going to generate wrapper. In the same project, we can generate the wrapper: D:\workspace\projects\my-app:

> gradle wrapper
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 22s
1 actionable task: 1 executed

It should have generated a folders .gradle and gradle and commands gradlew for UNIX/Linux and gradlew.cmd for Windows. To check the generated wrapper:

 > dir
 Volume in drive D is DATA
 Volume Serial Number is EC0A-13F8

 Directory of D:\workspace\projects\my-app

16/03/2020  07:35 AM    <DIR>          .
16/03/2020  07:35 AM    <DIR>          ..
16/03/2020  07:35 AM    <DIR>          .gradle
16/03/2020  06:53 AM    <DIR>          .mvn
16/03/2020  07:35 AM    <DIR>          gradle
16/03/2020  07:35 AM             5,305 gradlew
16/03/2020  07:35 AM             2,269 gradlew.bat
16/03/2020  06:53 AM            10,069 mvnw
16/03/2020  06:53 AM             6,607 mvnw.cmd
              4 File(s)         24,250 bytes
              5 Dir(s) 899,193,925,632 bytes free

Note: .gradle is not required and should be excluded when pushing the codes to the repository.

Conclusion

Whichever build system you are using, it is apparent that project build becomes easier as you would not need to install them manually. You can also easily distribute your project and so long as there is JDK installed in one’s machine, building the project is extremely easy.