Compare Maven vs Gradle Commands


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


Compare Maven and Gradle Build Script Major Elements:


MAVEN ELEMENTSGRADLE ELEMENTS
<groupId>Gradle’s “group” Element
<artifactId>Gradle’s “baseName” Element
<version>Gradle’s “version” Element

Maven and Gradle Commands In Brief:

The following table lists all useful, frequently used and important commands.
MAVEN COMMANDGRADLE COMMAND
mvn packagegradle assemble
mvn testgradle test
mvn cleangradle clean
mvn –helpgradle –help
mvn installgradle install
mvn –versiongradle –version
#1
1
mvn --version
To know version of gradle, we need to use the following Gradle command:
1
gradle --version


#2

1
mvn package
To create JAR/WAR/EAR, we need to use the following Gradle command:
1
gradle assemble
#3

1
mvn test
1
gradle test

#4

1
mvn -DskipTests=true install
1
gradle -x test install
#5

1
mvn test package
1
gradle build


#6

1
mvn clean
1
gradle clean

To deploy WebApplication
To deploy application WAR/EAR file into server, we need to use following commands.
1
mvn deploy
To run on Jetty Embedded Server
1
mvn jetty:run
Gradle has separate commands for each server to run created WAR/WAR file.
To run our WebApplication with already created WAR file in an Embedded Jetty Server:
1
gradle jettyRun
To build WAR file, deploy and run it in an Embedded Jetty Server:
1
mvn jetty:run-war
1
gradle jettyRunWar
NOTE:- We will see one separate post for WebApplication Development and run with an Embedded Jetty Server. Please see my upcoming posts.
To create JAR file
To create JAR file from compiled class files, we need to use following command.
1
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
mvn eclipse:eclipse
1
gradle eclipse
To clean all Eclipse required files
1
mvn eclipse:clean
1
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 Simple Application” 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
    apply plugin: 'maven'
  • Add GroupId and our JAR/WAR file version
  • Add build.gradle file file root elements as shown below
    1
    2
    group = "com.journaldev"
    version = "1.0"
    If we don’s specify version element here, then it uses war file version declaration as shown below
    1
    2
    3
    4
    war {
        baseName = 'SpringMVCExample'
        version = '1.0.0-BUILD-SNAPSHOT'
    }
    That’s it dude.
  • 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
    31
    32
    33
    34
    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
For more details refer - here

No comments:

Post a Comment