简体   繁体   中英

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

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?

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();
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM