The attributes of different profiles can be directly configured in the top-level pom.xml, which has the advantage that each module can share the configured attributes.
The first step is to create a new Spring Boot project, modify pom.xml, and add the <profiles/> configuration as follows:
<profiles> <profile> <id>dev</id> <properties> <username>zhangsan</username> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <username>lisi</username> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles>
Two profiles, dev and prod, are defined here, of which dev is activated by default. Properties are set in each profile via <properties/>.
The second step is to modify the compilation settings. Here < filtering >true</ filtering > specifies to use the properties under profile in pom.xml to replace the property references in ${xxx} under src/main/resources/:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
The third step is to create a new src/main/resources/application.properties in a module, the content is:
username=${username}
The fourth step is to use the -P parameter to specify the profile in the compilation command, such as: mvn clean install -DskipTests -Pdev, to complete the compilation of the specified profile environment.
Look at the target/classes/application.properties file in the module, and find that when -Pdev is used, username=zhangsan in the file, and when -Pprod is used, username=lisi in the file