javamavenfile-ioproperties-fileowner

Java Maven Project with Owner Library using multiple properties file across a shared project


I am trying to pass in property files from other projects using the Owner library. I currently have the following Java Maven projects:

Inside the Core project I have methods that require the use of URLs via a properties file stored in the Core directory. However, after the introduction of new projects, I will now need to pass project related property file into the core e.g. Project A properties file into the Core, so that the core can access the correct urls.

I am using the Owner Java library to handle property files. I currently have a similar setup to the following inside the core project:

import org.aeonbits.owner.Config;

public interface MyConfig extends Config {
    int port();
    String hostname();
    @DefaultValue("42")
    int maxThreads();
}

// Example of it being used
MyConfig config = ConfigCache.getOrCreate(MyConfig.class);
System.out.println(config.port);

The problem lies in sending new property files. In order to send a new property file, I must provide paramaters to the methods used in the core project.

myMethodFromCore(ConfigFileNameFromAnotherProject config)
config.homeUrl();

If the above example code is a method inside the core, then it must be familiar with ConfigFileNameFromAnotherProject. However, this is not possible since ConfigFileNameFromAnotherProject will live inside a Project A / Project B, thus will not be recognised in the core. Does anyone have any neat solution or ideas on how I can solve this problem?

Thank you


Solution

  • Overall I should mention that using third-party library properties to inject a value into your project is a weird situation to me, I'd suggest thinking twice if you really need to use another library to override your config, as it seems like a vulnerability as you're not in control of your configuration.

    Anyway it's not what is being asked, here is my suggestion: You could try using the reflection if you know your method names:

     Method method = config.getClass().getMethod("getHomeUrl");
     Object invokeResult = method.invoke(config);
     invokeResult.toString();
    

    The thing here is that you need to know the method name you're trying to use.