Home > Enterprise >  How to use variables from other file in maven pom.xml or in .properties file?
How to use variables from other file in maven pom.xml or in .properties file?

Time:03-21

I want to use database username and password from some other file in pom.xml. I am aware that one way is to use this is to use properties file in resources folder, but then how do I refer the variables from some other file in properties file also?

CodePudding user response:

I think you can do that using the example below :

 @Configuration
public class DataSourceConfig {
    @Bean
    public DataSource getDataSource() {
        Properties properties = null;
        InputStream inputStream = null;
        DataSourceBuilder dataSourceBuilder =null;
        try {
        properties = new Properties();
        inputStream = new FileInputStream("./src/main/resources/config.properties");
        properties.load(inputStream);
        dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.url(properties.getProperty("url"));
        dataSourceBuilder.username(properties.getProperty("user"));
        dataSourceBuilder.password(properties.getProperty("password"));
        }
        catch(Exception ex) {
            System.out.println("CONFIG EXCEPTION :" ex);
        }
        return dataSourceBuilder.build();
    }
}

CodePudding user response:

Start your jar with this command

java -jar -Dspring.config.location=file:///Users/home/jdbc.properties  ./app.jar

 OR use Env variable 

export SPRING_CONFIG_LOCATION=file:///Users/home/jdbc.properties

and set option in your jdbc.properties.

  • Related