Eko
Eko

Reputation: 1557

Override properties file from jar

I have a properties file in one of the .jar of my maven dependencies. I would like to override the values in my application so I created a file with the same name and the same package, but the values from the jar file are still being used. If I delete the properties file from the jar, the values of the file in my application are used. How can I always use the properties from my application instead of the .jar ?

Upvotes: 5

Views: 5899

Answers (4)

keybored
keybored

Reputation: 129

How about this ?

  • rename the properties file with overridden values, like this _override.properties (if the actual file is called original.properties.
  • Now, in your code, read a system property, called 'toOverrideProps', if true, to load the overridden properties file
  • when running your program, you can set this property using the -Dprop=value method This way, you have a choice on startup, to use the actual properties file or the overridden one, without conflict.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109557

Use the properties resource in the jar as template, initial file for the properties file you will use:

Path propertiesFile = Paths.get(System.getProperty("user.home"),
        ".myapp/config.properties");
Files.createDirectories(propertiesFile.getParent());

if (!Files.exists(propertiesFile)) {
    Files.copy(getResourceAsStrem("/config.properties"), propertiesFile);
}

Properties props = new Properties();
props.load(new FileInputStream(propertiesFile.toString());

Upvotes: 0

Vadim
Vadim

Reputation: 4120

As long as code takes your property file from class path it depends how your class path configured.

If you externalized your file out of any jar files - Try to put path to directory where your actual file located upfront of any other jar files in your java command -cp parameter.

If you keep your file inside your own jar file, in classpath - your jar file must be before that dependency jar file with default properties file.

Still those are not good solutions (sometime it is hard to control which path JVM will use first).

So, try to find documentation about your dependency jar - it may have a property to point from where and which properties file to use.

Upvotes: 1

Arturo Seijas
Arturo Seijas

Reputation: 304

You can use Maven Resource Plugin and parametrize your configuration file so you can pass the parameters as arguments through command line

Upvotes: 1

Related Questions