Gradle build tool

Sharing my own experience with you on Gradle

Gradle is nice tool to build applications like Maven and ant since it brings best of Maven and ANT… One thing that I don’t like with Gradle is too slow! It is slowest one when compared with ANT and Maven but it is simplest one either 😉

Gradle may be future of building JAVA apps. One day, it might be more popular and will be replaced with Maven by many companies.

Gradle uses the “Groovy” programming language! Don’t panic! You don’t need to know Groovy in order to write build script in Gradle! You just need to understand the Groovy and that is it! However, if you wanna learn about Groovy then here is link for you: Gradle

Let’s talk more about Groovy

  • Groovy runs in the JVM! This means that The Groovy can interact with JAVA easily!
  • You can use JAVA in Gradle along with Groovy…
  • Gradle has taks which is smiliar to ANT Targets…
  • Groovy is case-sensitive!

Let’s look at below sample code which is written by me on Groovy:

defaultTasks 'finished'
// defaultTasks 'hello', 'helloAgain'

task hello >> {
	println '    Hello'
}

task helloAgain >> {
	println '    Hello'
}

task startProcess >> {
	println '    Building!'
}

task step2 >> {
	println '    step 2 a'
}

step2 >> {
	println '    step 2 b'
}

step2.doFirst {
	println '    step 2 first'
}

task doSomethingInTheMiddle ( dependsOn: ['startProcess', 'step2']) >> {
	println '    this is the middle step'
}

task finished(dependsOn: ['doSomethingInTheMiddle', 'step2']) >> {
	println '    done'
	// We can use JAVA
	System.out.println("This is a JAVA statement");
}

What is output of this code?

:startProcess
Building!
:step2
step 2 first
step 2 a
step 2 b
:doSomethingInTheMiddle
this is the middle step
:finished
done

Output: This is a JAVA statement

If you can understand the output of above example without being advance Groovy geek. Then, You can write below Groovy build script like me:

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

// plugin has tasks we will call them as shown
defaultTasks  'eclipse', 'clean', 'test', 'javadoc', 'war', 'deployToTomcat'
// I don't need to call compileJava task since test task depends on that

// we will be using Maven Repository to get dependencies
repositories{
	mavenCentral()
}

/*
dependencies{
	compile group: 'org.springframework',
	name: 'spring-webmvc',
	version: '3.1.1.RELEASE'
}
*/

dependencies {
	compile 'junit:junit:3.8.1'
	compile 'org.springframework:spring-webmvc:3.1.1.RELEASE'
	compile 'javax.validation:validation-api:1.0.0.GA'
	compile 'org.hibernate:hibernate-validator:4.3.1.Final'
	compile 'javax.servlet:servlet-api:2.5'
	compile 'com.lowagie:itext:2.1.7'
	compile 'org.apache.poi:poi:3.9'

	runtime 'org.springframework.webflow:spring-webflow:2.3.2.RELEASE'
	runtime 'jstl:jstl:1.2'
}

war {
   archiveName = 'gradlewebapp.war'
   webInf { from 'src/main/resources/application.xml' }
   webInf { from 'src/main/resources/*.properties' }
}

task deployToTomcat (type: Copy, dependsOn: 'war') {
   from 'build/libs/gradlewebapp.war'
   into '/Users/ekoca/apps/tomcat7/webapps'
}

Sorry I don’t have time to explain everything in this Gradle build script but I would like to answer your questions related to this build script! So if you have question, please feel free to ask!

One more thing, Gradle will output the result into the build directory unlike Maven (Maven outputs into “target” folder!)

Here is the good source to understand “Groovy” User Guide
DSL Refference

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s