modify properties file in META-INF
I'm using maven and storing a properties file in src/main/resources
which I can read fine like:
properties.loadFromXML(this.getClass().getClassLoader().
getResourceAsStream("META-INF/properties.xml");
Is it possible to then write to this file when it is packaged up as a jar? I've tried the following:
properties.storeToXML(new FileOutputStream(new File(this.getClass().getClassLoader().
getResource("META-INF/properties.xml").toURI())), "");
This works 开发者_开发技巧in Eclipse because it is saving the file to target/classes/META-INF
, but not when packaged as a jar, so is it possible to achieve the same thing?
Is it possible to then write to this file when it is packaged up as a jar?
Short answer: no, it is not possible, a jar file is a file, not a directory. And actually, you generally don't write properties files back to a jar file.
You should put that properties file on the classpath on the local file system, outside a JAR.
It's always possible to modify a packaged file by unpacking, rewriting and re-packing. Sometimes this is the easiest approach.
A Jar file is essentially a renamed .ZIP
file. Java has classes for accessing files within a .Zip file, and you could (if sufficiently motivated) write yourself a program to do this.
Alternatively, I'm pretty sure there are ant
tasks that can do this too (think creative use of the jar
task), and there are POM plugins available to run ant tasks from Maven.
精彩评论