Gradle vs Maven With Examples

In this post, we are going to discuss gradle vs maven. We will also see how to run gradle commands from Command Prompt”.

We have already compared maven vs gradle in What is Gradle and Gradle Tutorial posts. Here we will look more into gradle commands and corresponding maven commands.

We are going to use Gradle Spring MVC example program to demonstrate most of the gradle commands in this post.

Gradle vs Maven

Both gradle and maven are build and dependency management tools. We will look into following points in comparing gradle vs maven.

  1. Gradle vs Maven Commands
  2. Install Gradle Project To Maven Repo
  3. Maven vs Gradle Build Script Major Elements
  4. Maven vs Gradle Commands In Brief

Gradle vs Maven Commands

As we all are already very much familiar with maven build tool, it’s better to compare maven commands with gradle commands to learn gradle easily.

In this section, we will try to take each maven command and find out the counterpart in gradle.

Like Maven, We can run gradle commands by using some IDEs like Eclipse IDE, Spring STS IDE, IntelliJ IDEA etc. and also from command prompt.

To know version: First and foremost thing we should know is our software version so that we can understand which features we can use in our application development.

To know version of maven, we need to use the following maven command:

To know version of gradle, we need to use the following gradle command:

To create JAR/WAR/EAR
To deploy our application into any Web or Application servers, we need to create our application JAR/WAR/EAR files.

To create JAR/WAR/EAR, we need to use the following maven command:

To create JAR/WAR/EAR in gradle, we need to use the following gradle command:

Before executing this command, open command prompt at our project root directory and execute “dir” command to see the content of the current working directory.

mvn-gradle-version-450x209

Here we can observe that other than “src” folder, we have nothing. Execute gradle assemble command now.

mvn-gradle-version-450x209

Here we can see “BUILD SUCCESSFUL”. This command, first compiles all source files and then creates war file. Now again execute “dir” command to see the content of the current working directory.

mvn-gradle-version-450x209

We can observe newly generate war file at ${PROJECT_ROOT_DIR}/build/libs/ as shown below.

mvn-gradle-version-450x209

We can observe compiled *.class files at ${PROJECT_ROOT_DIR}/build/classes/main/[our-package-structure]/*.class.

To run Unit Tests
To run only JUnit tests without creating our application JAR/WAR/EAR file, we need to use the following commands:

To compile and run unit tests:

To Skip Unit Tests
To skip JUnit tests and do required other tasks, we need to use the following commands:

To run JUnits and create JAR/WAR/EAR
To run JUnit tests first then create our application JAR/WAR/EAR files, we need to use the following commands:

To compile, tests and assemble:

NOTE: In both maven and gradle, we can pipe two or more commands and execute them at once as shown above.

To clean
It’s always recommended to clean everything before compile and build our application code.

To delete build directory:

To Install
To compile, build and install to local maven repository, we need to use following commands.

To deploy Web Application
To deploy application WAR/EAR file into server, we need to use following commands.

To run on Jetty embedded server:

Gradle has separate commands for each server to run created WAR/WAR file.

To run our Web Application with already created WAR file in an embedded Jetty server:

To build WAR file, deploy and run it in an embedded jetty server:

To create JAR file
To create JAR file from compiled class files, we need to use following command.

Eclipse IDE Commands
To Generate a Project and all Eclipse required files.

To clean all Eclipse required files:

Install Gradle Project To Maven Repo

We can use gradle install command to build and install our application JAR/WAR file to our local maven repository.

We cannot execute “gradle install” command with our previous gradle project example from Gradle Spring MVC” because we have not specified any maven plugins in build.gradle file.

Now we will change our build.gradle file to include this functionality. Please follow these simple steps.

    1. Add maven plugin to build.gradle file

    1. Add GroupId and our JAR/WAR file version

Add build.gradle file root elements as shown below.

If we don’t specify version element here, then it uses war file version declaration as shown below.

That’s it. Now our gradle install will copy application jar/war file to maven repository too.

    1. Our complete and new build.gradle file

    1. Execute “gradle install” command
    2. gradle-install-example1-450x162
    1. Observe our Local Maven Repository
    2. gradle-install-example

Maven vs Gradle Build Script Major Elements

If we observe both maven’s pom.xml and gradle’s build.gradle file, we can find the following things.

Maven Elements Gradle Elements
<groupId> Gradle’s “group” Element
<artifactId> Gradle’s “baseName” Element
<version> Gradle’s “version” Element
  • Like Maven, we can integrate existing ANT tasks in Gradle and use them in our projects.
  • Like Maven, we can create our own project specific Gradle tasks and use them in our projects.
  • We can even Integrate Maven tasks with Gradle build tool.

Maven vs Gradle commands

The following table lists all important maven and gradle commands.

Maven Command Gradle Command
mvn package gradle assemble
mvn test gradle test
mvn clean gradle clean
mvn –help gradle –help
mvn install gradle install
mvn –version gradle –version

It’s a brief comparison between Maven vs Gradle build tools. It’s not enough to include everything in one post to list all commands with suitable examples. I hope we have given enough basics to dig into these two tools further to become an expert.

Reference: Official user guide

By admin

Leave a Reply

%d bloggers like this: