Leo Hui
Leo Hui

Reputation: 21

Java edit file inside WAR archive

I've been reading on forums on how to read/edit files inside archive but still cannot get my answer. Mainly using getResourceAsStream, and must be within its classpath.

What I need to do is, read and update the xml file with a given filepath as input, then redeploy it to Tomcat. But so far I still can't get answer to how to edit the xml files inside a AAR file within a WAR archive with a given full path as input. Can someone help me please?

for example, I would like to edit the applicationContext.xml file:

C:/webapp.WAR

From the webapp.WAR file:

webapp.WAR/WEB-INF/services/myapp.AAR

From the myapp.AAR:

myapp.AAR/applicationContext.xml

Upvotes: 2

Views: 17140

Answers (2)

Rukmani Vijayakumar
Rukmani Vijayakumar

Reputation: 41

You can edit a war as follow,

//You can loop through your war file using the following code
ZipFile warFile = new ZipFile( warFile );
for( Enumeration e = warFile.entries(); e.hasMoreElements(); ) 
{
    ZipEntry entry = (ZipEntry) e.nextElement();
    if( entry.getName().contains( yourXMLFile ) ) 
    {
        //read your xml file
        File fXmlFile = new File( entry );
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);


        /**Now write your xml file to another file and save it say, createXMLFile **/

        //Appending the newly created xml file and 
        //deleting the old one.
        Map<String, String> zip_properties = new HashMap<>(); 
        zip_properties.put("create", "false");
        zip_properties.put("encoding", "UTF-8");        

        URI uri = URI.create( "jar:" + warFile.toUri() );

        try( FileSystem zipfs = FileSystems.newFileSystem(uri, zip_properties) ) {

            Path yourXMLFile = zipfs.getPath( yourXMLFile );
            Path tempyourXMLFile = yourXMLFile;
            Files.delete( propertyFilePathInWar );

            //Path where the file to be added resides 
            Path addNewFile = Paths.get( createXMLFile );  

            //Append file to war File 
            Files.copy(addNewFile, tempyourXMLFile); 
            zipfs.close();  

        }
    }
}

Upvotes: 1

Ryan Stewart
Ryan Stewart

Reputation: 128779

You can't modify files contained in any ?AR file (WAR, JAR, EAR, AAR, ...). It's basically a zip archive, and the only way to modify it is to unzip it, make the changes, and zip it up again.

If you're trying to have a running application modify itself, then recognize that 1) many containers run from an exploded copy of an ?AR file for various reasons, and 2) it's unlikely to do you any good to modify files on the fly anyway unless you plan to restart the application after the change or write lots of code to monitor for changes and refresh your app in some way afterward. Either way, you're probably better off figuring out how to make your desired change programmatically than trying to rewrite a running application.

On the other hand, if you're not talking about having an application modify itself but instead changing a WAR file and then deploying the new version, it's just what I said above. Explode it using the jar tool, modify the exploded directory, and then compress it again with jar. Then deploy it like a new war file.

Upvotes: 5

Related Questions