简体   繁体   English

如何使用 maven pom.xml 或 in.properties 文件中其他文件的变量?

[英]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.我想使用 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使用此命令启动您的 jar

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.并在您的 jdbc.properties 中设置选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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