Chapter 11
The War Plugin

The war plugin extends the JavaPlugin. It disables the default jar archive generation of the Java Plugin and adds a default war archive task. Have also a look at org.gradle.api.tasks.bundling.War.

11.1 Default Settings

The default behavior of the War plugin is to copy the content of src/main/webapp to the root of the archive. Your webapp folder may of course contain a WEB-INF sub-directory, which again may contain a web.xml file. Your compiled classes are compiled to WEB-INF/classes. All the dependencies of the runtime1 configuration are copied to WEB-INF/lib. The War plugin add two new dependency configurations: providedCompile and providedRuntime. Those new configurations have the same scope as the respective compile and runtime configurations. Except that they are not added to the war-archive. It is important to note that those provided configurations work transitively. Let’s say you add commons-httpclient:commons-httpclient:3.0 to any of the provided configurations. This dependency has a dependency on commons-codec. This means neither httpclient nor commons-codec is added to your war, even if commons-code were an explicit dependency of your compile configuration. If you don’t want this transitive behavior, simply declare your provided dependencies like commons-httpclient:commons-httpclient:3.0@jar.

11.2 Customizing

Here an example with the most important customization options:

  group = 'gradle'
  version = '1.0'
  usePlugin('war')
  targetCompatibility = '1.5'
  sourceCompatibility = '1.5'
  
  dependencies {
      addConfiguration('moreLibs')
      addFlatDirResolver('lib', "$rootDir/lib")
      compile ":compile:1.0"
      providedCompile ":providedCompile:1.0@jar"
      runtime ":runtime:1.0"
      providedRuntime ":providedRuntime:1.0@jar"
      testCompile ":junit:3.8.2"
      moreLibs ":otherConf:1.0"
  }
  
  archive_war {
      fileSet(dir: 'src/rootContent') // adds a file-set to the root of the archive
      webInf(dir: 'src/additionalWebInf') // adds a file-set to the WEB-INF dir.
      additionalLibs(dir: 'additionalLibs') // adds a file-set to the WEB-INF/lib dir.
      libConfigurations('moreLibs') // adds a configuration to the WEB-INF/lib dir.
      webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
  }
  

Of course one can configure the different file-sets with a closure to define excludes and includes.

If you want to enable the generation of the default jar archive additional to the war archive just type:

  archive_jar.enabled = true

11.3 Eclipse WTP

org.gradle.api.tasks.ide.eclipse.EclipseWtp has a default instance with the name eclipseWtp. It generates a .settings/org.eclipse.wst.common.component file.