How to change configuration file during gradle build?

I have Java EE application. I need to deploy it to both Weblogic and JBoss application servers. For that reason, I have two versions of web.xml files for both servers. And my main goal is to change this configuration when I build project for specific servers. I have idea that I can keep one web.xml file in some directory {project}/files and for example when I build gradle for JBoss I replace an existing web.xml file with file from {project}/files. So I need to create some gradle task for this. I am new in gradle, so please give me some approximate solution how I can do that.

Upvotes: 1

Views: 1533

Answers (1)

lance-java
lance-java

Reputation: 28071

If it were me I'd follow the Gradle naming conventions and store the files at

  • src/weblogic/webapp/WEB-INF/web.xml
  • src/jboss/webapp/WEB-INF/web.xml

This structure would allow custom java classes and resources in future per servlet container if needed

Then you could create two extra tasks in build.gradle

apply plugin: 'war' 
dependencies { ... } 
task weblogicWar(type: Zip) {
   dependsOn war
   from zipTree(war.archivePath).matching {
      exclude 'WEB-INF/web.xml' 
   } 
   from 'src/weblogic/webapp' 
   archiveName = "my-app-weblogic-${version}.war"
}
task jbossWar(type: Zip) {
   dependsOn war
   from zipTree(war.archivePath).matching {
      exclude 'WEB-INF/web.xml' 
   } 
   from 'src/jboss/webapp' 
   archiveName = "my-app-jboss-${version}.war"
}
// wire the tasks into the DAG
assemble.dependsOn weblogicWar
assemble.dependsOn jbossWar

You could also do this in a loop eg:

['jboss', 'weblogic'].each { container ->
   task "${container}War"(type: Zip) {
      dependsOn war
      from zipTree(war.archivePath).matching {
         exclude 'WEB-INF/web.xml' 
      } 
      from "src/${container}/webapp" 
      archiveName = "my-app-${container}-${version}.war"
   }
   assemble.dependsOn "${container}War" 
} 

Upvotes: 2

Related Questions