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.
- Gradle vs Maven Commands
- Install Gradle Project To Maven Repo
- Maven vs Gradle Build Script Major Elements
- 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:
1 2 3 |
mvn --version |
To know version of gradle, we need to use the following gradle command:
1 2 3 4 |
gradle --version <img class="alignnone wp-image-32394 size-full" src="https://all-learning.com/wp-content/uploads/2015/07/mvn-gradle-version-450x20-1.png" alt="mvn-gradle-version-450x209" width="1200" height="628" /> |
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:
1 2 3 |
mvn package |
To create JAR/WAR/EAR in gradle, we need to use the following gradle command:
1 2 3 |
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.
Here we can observe that other than “src” folder, we have nothing. Execute gradle assemble
command now.
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.
We can observe newly generate war file at ${PROJECT_ROOT_DIR}/build/libs/ as shown below.
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:
1 2 3 |
mvn test |
1 2 3 |
gradle test |
To Skip Unit Tests
To skip JUnit tests and do required other tasks, we need to use the following commands:
1 2 3 4 5 |
mvn install -DskipTests #OR mvn install -Dmaven.test.skip=true |
1 2 3 |
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:
1 2 3 |
mvn test package |
1 2 3 |
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:
1 2 3 |
mvn clean |
1 2 3 |
gradle clean |
To Install
To compile, build and install to local maven repository, we need to use following commands.
1 2 3 |
mvn install |
1 2 3 |
gradle install |
To deploy Web Application
To deploy application WAR/EAR file into server, we need to use following commands.
1 2 3 |
mvn deploy |
To run on Jetty embedded server:
1 2 3 |
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:
1 2 3 |
gradle jettyRun |
To build WAR file, deploy and run it in an embedded jetty server:
1 2 3 |
mvn jetty:run-war |
1 2 3 |
gradle jettyRunWar |
To create JAR file
To create JAR file from compiled class files, we need to use following command.
1 2 3 |
mvn jar |
1 2 3 4 5 |
gradle jar #or gradle libs |
Eclipse IDE Commands
To Generate a Project and all Eclipse required files.
1 2 3 |
mvn eclipse:eclipse |
1 2 3 |
gradle eclipse |
To clean all Eclipse required files:
1 2 3 |
mvn eclipse:clean |
1 2 3 |
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.
-
- Add maven plugin to build.gradle file
1 2 3 |
apply plugin: 'maven' |
-
- Add GroupId and our JAR/WAR file version
Add build.gradle file root elements as shown below.
1 2 3 4 |
group = "com.journaldev" version = "1.0" |
If we don’t specify version element here, then it uses war file version declaration as shown below.
1 2 3 4 5 6 |
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.
-
- Our complete and new build.gradle file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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") } |
-
- Execute “gradle install” command
-
- Observe our Local Maven Repository
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.
- 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.
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