Search code examples
warjythonresourcebundle

Script to modify files inside WAR


I need to update the resource files inside a WAR for different environments (eg Database info)

So i need to write a script which will update these files inside the WAR. Please let me know if the above is possible using Jython Scripts or Suggest any other script where this is achievable.


Solution

  • While .war file format is simply the same as .zip file format you can use zipfile module to work with those files. Unfortunately ZipFile objects do not allow modifying files in existing .zip files, so you will have to copy all files from source .war and modify content of selected file. Example:

    def modify_war(war):
        if os.path.exists(war):
            zf_in = zipfile.ZipFile(war, 'r')
            zf_out = zipfile.ZipFile(war[:-4] + '_new.war', 'w')
            try:
                for item in zf_in.infolist():
                    content = zf_in.read(item.filename)
                    if item.filename == 'META-INF/MANIFEST.MF':
                        content += '\nModified-By: mn\n'
                    zf_out.writestr(item, content)
            finally:
                zf_in.close()
                zf_out.close()