Sunday 7 June 2015

Gradle basics : Building war file using gradle

Gradle is very similar to the commonly used build management/dependency systems like ant scripts, pom.xml or apache ivy. But what attracted me towards gradle is its simplicity and availability of inbuild plugins which definitely makes our life easier. Also Gradle is readable , unlike its xml variants. 



Installing Gradle is pretty easy : 

Step 1: Download Gradle from the link : download link

Step 2: Unzip gradle to some folder of your choice .

Step 3: You can see the bin folder inside the unzipped file contents. You need to add this folder to the classpath so that you can run gradle command from any where on your system 

Step 4: now try running gradle from the command prompt. A similar output is shown: 


Writing your Gradle Build Script :

Gradle uses a Domain Specific Language (DSL) based on Groovy to declare builds. Groovy syntax is very similar to Java syntax, so it increases the readability of our build scripts. 
The Build script is a file named build.gradle.

The sample build.gradle file for my spring mvc application looks like this. Place the gradle file into the root of your web project folder.



apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

repositories {
    mavenLocal()
    mavenCentral()
}

war {
    baseName = 'assortedmind-gradle'
    version =  '0.1.0'
}

dependencies {
    compile 'org.springframework:spring-webmvc:4.1.1.RELEASE'
    compile 'org.springframework:spring-context:4.1.1.RELEASE'
    compile 'org.springframework:spring-aop:4.1.1.RELEASE'
    compile 'org.springframework:spring-beans:4.1.1.RELEASE'
    compile 'org.springframework:spring-core:4.1.1.RELEASE'
    compile 'org.springframework:spring-expression:4.1.1.RELEASE'
    providedCompile 'javax.servlet:servlet-api:2.5'
  
}

task deploy(type: Copy) {
    from 'build/libs'
    into 'C:/Anushya/myJboss-jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}


You might feel confused in the beginning, but later you will be overwhelmed by how simple this file really is...


A gradle script is a collection of tasks.  Gradle comes with inbuilt plugins , which are actually a collection of pre-defined tasks.  In our file,  the line 

apply plugin: 'java'
 is all it needs to add build related tasks. Type gradle task in the command line , and you can see the tasks that is been added as part of Java plugin. 

























Note :  To use the Java plugin for gradle effectively, make sure that you place your source files in src/main/java folder and non-java resources in src/main/resources. In this way , gradle will take care of packaging your war file by placing the class files and resources files in right manner. 

The Dependencies{ }  block lets you mention your compile time and runtime dependencies. 

The repositories{ } block lets you mention which repository needs to be referenced during tasks execution. First it will look for the jar files in local repository , and if not available , will check for maven central repository.

Type gradle build to build a war file. The war file will be present in the build/lib/ folder. 

My deploy task copies the war file into my jboss standalone deployment folder.

The complete web application project structure is as follows : 





















Finally the wrapper task lets you run this gradle script in machines that does not have gradle installed.  Run the task and it will install gradle for you. 



No comments:

Post a Comment