简体   繁体   English

如何使用Maven配置文件和设置示例通过Spring Boot加载环境特定的配置和属性

[英]How to load environment specific configurations and properties with Spring boot using Maven Profiles and Settings Example

Read the mongodb user name and password from settings.xml based on profile selection in pom.xml. 根据pom.xml中的配置文件选择,从settings.xml中读取mongodb用户名和密码。 I am not getting idea how to do. 我不知道该怎么办。 Can any one give example. 谁能举个例子。

I followed reference link 我关注了参考链接

https://examples.javacodegeeks.com/enterprise-java/spring/loading-environment-specific-configurations-properties-spring-using-maven-profiles-xml-settings-file-example/ https://examples.javacodegeeks.com/enterprise-java/spring/loading-environment-specific-configurations-properties-spring-using-maven-profiles-xml-settings-file-example/

pom.xml 的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">//here showing error


<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Spring Maven Properties Example Code</name>

<properties>

    <!-- Generic properties -->
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <resource.directory>src/main/resources</resource.directory>

</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>

</parent>
<dependencies>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.11</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-filtering</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <!-- Test Artifacts -->


</dependencies>

<profiles>
    <profile>
        <id>dev</id>
        <!-- Dev Env. Properties -->
        <properties>
            <profile.name>${profile.name}</profile.name>
            <!-- Database Properties -->
            <db.driverClass>${db.driverClass}</db.driverClass>
            <db.connectionURL>${db.connectionURL}</db.connectionURL>
            <db.username>${db.username}</db.username>
            <db.password>${db.password}</db.password>

        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <!-- Test Env. Properties -->
        <properties>
            <profile.name>${profile.name}</profile.name>
            <!-- Database Properties -->
            <db.driverClass>${db.driverClass}</db.driverClass>
            <db.connectionURL>${db.connectionURL}</db.connectionURL>
            <db.username>${db.username}</db.username>
            <db.password>${db.password}</db.password>

        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <!-- Prod Env. Properties -->
        <properties>
            <profile.name>${profile.name}</profile.name>
            <!-- Database Properties -->
            <db.driverClass>${db.driverClass}</db.driverClass>
            <db.connectionURL>${db.connectionURL}</db.connectionURL>
            <db.username>${db.username}</db.username>
            <db.password>${db.password}</db.password>

        </properties>
    </profile>
</profiles>

<build>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
    </plugins>

    <resources>
        <!-- Placeholders that are found from the files located in the configured 
            resource directories are replaced with the property values found from the 
            profile specific configuration file. -->
        <resource>
            <directory>${resource.directory}</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

</build>

application.properites application.properites

db.connectionURL=${db.connectionURL}
db.username=${db.username}
db.password=${db.password}

I put the settings.xml file in current project dir. 我将settings.xml文件放在当前项目目录中。 settings.xml 的settings.xml

<profiles>
        <profile>
            <id>dev</id>
            <!-- Dev Env. Properties -->
            <properties>
                <profile.name>dev</profile.name>
                <!-- Database Properties -->
                <db.driverClass>com.mysql.jdbc.Driver</db.driverClass>
<db.connectionURL>jdbc:postgresql://localhost:5432/springbootdb</db.connectionURL>
<db.username>postgres</db.username>
<db.password>postgres</db.password>

            </properties>
        </profile>
        </profiles>

MainApplication MainApplication

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Here i am testing for postgres weather it is working or not but showing error in pom.xml file here 在这里,我正在测试postgres天气是否正常运行,但此处的pom.xml文件显示错误

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

the error is 错误是

Project build error: 
Resolving expression: '${db.password}': Detected the following recursive expression cycle in 'db.password': [db.password]

I want to test scenarios for postgres and monngodb. 我想测试postgres和monngodb的方案。

Help me out.. 帮帮我..

If you are using Spring Boot, the syntax for Maven filtering is slightly different. 如果您使用的是Spring Boot,则Maven过滤的语法略有不同。 You should use @propery@ instead of ${property} . 您应该使用@propery@而不是${property} So you should change your application.properties as follows : 因此,您应该按如下所示更改application.properties:

db.connectionURL=@db.connectionURL@
db.username=@db.username@
db.password=@db.password@

This blog gives more details 该博客提供了更多详细信息

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

相关问题 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? 使用Spring配置文件加载环境属性 - Loading environment properties using Spring profiles 使用 Maven 配置构建 Spring Boot Profile-specific Properties - Spring Boot Profile-specific Properties build using Maven Configuration 如何在spring boot jar中根据环境加载属性 - How to load properties based on environment in spring boot jar Spring Boot,特定于环境的属性位置 - Spring boot, environment specific properties location 具有外部属性的Spring Boot配置文件 - Spring boot profiles with external properties 如何使用 Spring Boot 从环境变量自定义属性绑定 - How to customize properties binding from environment variables using Spring Boot 如何使用Spring Boot / Maven更改index.html在不同配置文件中的位置? - how to change location of index.html in different profiles using spring boot/maven? 使用application.properties文件激活配置文件以进行弹簧启动测试 - Activate profiles for spring boot test using application.properties file 如何使用Spring Boot和Maven设置配置文件? - How can I set profiles with spring boot and maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM