Explode WAR file with Distribution plugin

I have a multi project build with 2 modules.

root

  • myWebApp
  • distribution

myWebApp uses the ‘war’ plugin and creates a war file (obviously)

I would like to create a distribution zip file containing the exploded contents of the war file in a particular location, along with the contents from distribution/src/main/dist.

With this build.gradle for ‘distribution’ I am able to get the war files themselves:

plugins {
  id 'distribution'
}

configurations {
  warFiles
}

dependencies {
  warFiles project(path: ':myWebApp', configuration: 'archives')
}

distributions {
  main {
    contents {
      into('somedirectory/webapps') {
        from (project.configurations.warFiles)
      }
    }
  }
} 

This puts “myWebApp.war” into ‘someDirectory/webapps’.

Is there a way to unzip myWebApp.war? I tried using zipTree:

distributions {
  main {
    contents {
      into('somedirectory/webapps') {
        from (project.zipTree(project.configurations.warFiles))
      }
    }
  }

but that gives an error:

What went wrong:
A problem occurred evaluating project ‘:distribution’.
Cannot convert the provided notation to a File or URI: configuration ‘:distribution:warFiles’.
The following types/formats are supported:
- A String or CharSequence path, for example ‘src/main/java’ or ‘/usr/include’.
- A String or CharSequence URI, for example ‘file:/usr/include’.
- A File instance.
- A Path instance.
- A Directory instance.
- A RegularFile instance.
- A URI or URL instance.
- A TextResource instance.

The solution is to add a configuration in myWebApp that resolves to a directory containing the exploded web application.

In “myWebApp”, build.gradle:

plugins {
  id 'war'
  id 'eclipse'
}
configurations {
  explodedWebApp
}
dependencies {
    // snip
}

war {
    archiveFileName = 'myWebApp.war'
}

task explodeWar(type: Sync) {
   into "$buildDir/exploded/myWebApp"
   with war
}

artifacts {
  explodedWebApp file("$buildDir/exploded"), { builtBy explodeWar }
}

Then, in distribution’s build.gradle, change the dependencies to refer to the explodedWebApp configuration:

dependencies {
  warFiles project(path: ':myWebApp, configuration: 'explodedWebApp')
}

The resulting distribution now contains the exploded web application in somedirectory/webapps

I my case I’m using from zipTree(configurations.warFiles.singleFile)

1 Like