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:


mvn --version

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


gradle --version
mvn-gradle-version-450x209

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:


mvn package

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


gradle assemble

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:


mvn test

gradle test

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


mvn install -DskipTests
#OR
mvn install -Dmaven.test.skip=true

gradle -x test install

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:


mvn test package

gradle build

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:


mvn clean

gradle clean

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


mvn install

gradle install

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


mvn deploy

To run on Jetty embedded server:


mvn jetty:run

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:


gradle jettyRun

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


mvn jetty:run-war

gradle jettyRunWar

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


mvn jar

gradle jar
#or
gradle libs

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


mvn eclipse:eclipse

gradle eclipse

To clean all Eclipse required files:


mvn eclipse:clean

gradle cleanEclipse

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

apply plugin: 'maven'
    1. Add GroupId and our JAR/WAR file version

Add build.gradle file root elements as shown below.


group = "com.journaldev"
version = "1.0"

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


war {
    baseName="SpringMVCExample"
    version = '1.0.0-BUILD-SNAPSHOT'
}

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

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "war"
apply plugin: 'maven'
sourceCompatibility = 1.7
group = "com.journaldev"
version = "1.0"
war {
    baseName="SpringMVCExample"
    version = '1.0.0-BUILD-SNAPSHOT'
}
repositories {
    mavenCentral()
}
dependencies {
    compile("org.springframework:spring-context:4.0.0.RELEASE")
    compile("org.springframework:spring-webmvc:4.0.0.RELEASE")
    compile("org.aspectj:aspectjrt:1.7.4")
	compile("javax.inject:javax.inject:1")
    compile("javax.servlet:servlet-api:2.5")
    compile("javax.servlet:jstl:1.2")
    compile("javax.servlet.jsp:jsp-api:2.1")
    compile("org.slf4j:slf4j-api:1.7.5")
    compile("org.slf4j:jcl-over-slf4j:1.7.5")
    compile("org.slf4j:slf4j-log4j12:1.7.5")
    compile("log4j:log4j:1.2.15")
    testCompile("junit:junit:4.7")
}
    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: