Export War file from Eclipse Web Project with Gradle

If you are a newbie about gradle and you need to export war file from Eclipse Web Project without changing anything in project layout, you may need to spend some more time after you study the Gradle Tutorials and War Plugin. Even if it is a small snippet, I spent some time so  think sharing simple gradle script to export war file from Eclipse web project may save time for some guys. Below lines are more meaningful after you read the Gradle Tutorials and War Plugin.

// we need to use war plugin
apply plugin: 'war'

//default webAppDirName in gradle is not "WebContent" but 
// it is default in eclise so change it according to eclipse layout
// it encapsulates all files under webAppDirName
webAppDirName = 'WebContent'

// build directory of gradle, it is default "build" and
// we don't need to change it. I changed because I dont want eclipse
// and gradle build to be mixed. 
buildDir = 'gradle_build'

// src is not default java source dir in gradle 
// so change srcDir and resources srcDir 
//according to eclipse project layout  
sourceSets {
  main {
     java {
	      srcDir 'src'
     resources {
       srcDir 'src'

//I have just realized that we did not need repositories definiton below
//But it should be tested again after removing below lines
repositories {
   flatDir { dirs "WebContent/WEB-INF/lib","libs"}

// In eclipse project, we generally add all dependecies under
//WEB-INF/lib for runtime.
// I also used libs directory for only compile time,
// we added two directory below to make gradle build successfully.
//It is defined as "providedCompile" which says gradle that do not 
//add these libraries to
//war file explicitly because it will be available in runtime.
//btw, we already let gradle add all files to war under WebContent 
dependencies {
  providedCompile fileTree(dir:'WebContent/WEB-INF/lib', include:'*.jar')
  providedCompile fileTree(dir:'libs', include:'*.jar')

here is the gist link

Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *