I have a Java maven project whose Jar file is used as a SDK library in other projects. This SDK is used to call other APIs which are by default in the prod environment. I want to make it run on testing environment by overriding a variable.
public class Routes {
public Map<String, String> routes;
private static String _baseUrl = "https://production-env.com";
@SuppressWarnings("serial")
public Routes() {
routes = new HashMap<String, String>() {
{
put("api.login", "/rest/login");
put("api.register", "/rest/register");
}
};
}
}
This is the Routes class which contains the base URL for prod/testing is used by other classes. I want to have the default _baseUrl
as the same one as above. But I want to be able to override it to "https://testing-env.com"
while running the Jar. And I don't want to add a public setter for that. I want to be able to do so by passing some arguments while running the Jar file.
CodePudding user response:
As @Thomas mentioned in comment:
// Get system property with default if property does not exist
public class Routes {
private static String _baseUrl = System.getProperty("base.url", "https://production-env.com");
public Map<String, String> routes;
@SuppressWarnings("serial")
public Routes() {
routes = new HashMap<String, String>() {
{
put("api.login", "/rest/login");
put("api.register", "/rest/register");
}
};
}
}
Set property when executing jar
java -Dbase.url=https://dev-env.com -jar my.jar