echo_sxy

echo_sxy

Version Switching - Using Profile

Profile is a commonly used feature in Spring that allows different configuration files to be loaded based on different environment configurations, thereby achieving different configuration logic.

Spring Profile#

In Spring Boot, profiles are implemented through configuration files. Different configuration files can be loaded in different environments to achieve different configuration logic. Specifically, Spring Boot supports the following types of configuration files:

  • application.properties
  • application.yml
  • application-{profile}.properties
  • application-{profile}.yml

application.properties and application.yml are common configuration files that are loaded in all environments. application-{profile}.properties and application-{profile}.yml are configuration files loaded based on different profiles. When the application starts, Spring Boot determines which configuration file to load based on the current environment variable. For example, if the current environment variable is dev, the application-dev.properties or application-dev.yml file will be loaded.

Usage#

In actual development, profiles can be used to achieve the following functionalities:

  • Differentiate between different environments, such as development, testing, and production environments.
  • Configure different database connection information, such as using a local MySQL database in the development environment, a remote MySQL database in the testing environment, and an Alibaba Cloud RDS database in the production environment.
  • Configure different log levels, such as using DEBUG level in the development environment, INFO level in the testing environment, and WARN level in the production environment.

Switching through Configuration Files#

Create three different configuration files in the project's resources directory: application-prod.properties, application-test.properties, and application-dev.properties. The current profile needs to be configured in the project's configuration file using one of the following methods:

  • Configure in the application.properties file.

spring.profiles.active=dev

  • Configure in the startup command.

java -jar myproject.jar --spring.profiles.active=dev

Maven Profile#

The principle of Maven profile is to define a set of profiles, and define different sets of property values in different profiles. By selecting a profile, different sets of properties can take effect.
Add different profiles in the pom.xml file:

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <!-- default Spring profiles -->
      <profiles.active>dev</profiles.active>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <!-- default Spring profiles -->
      <profiles.active>prod</profiles.active>
    </properties>
  </profile>
</profiles>

Then, configure the following in the pom.xml file. With these configurations, the configuration files under src/main/resources/${profiles.active} will be used.

<build>
  <resources>
    <!-- Common resource files -->
    <resource>
      <directory>src/main/resources</directory>
    </resource>
    <!-- Environment-specific resource files -->
    <resource>
      <directory>src/main/resources/${profiles.active}</directory>
      <!-- Filter is a feature provided by the maven-resources-plugin, which replaces placeholders (${...}) in property files (*.properties) with environment variables, properties defined in the pom file, and properties in specified external configuration files. In this case, the maven-resources-plugin uses @@ as the placeholder delimiter. -->
      <filtering>true</filtering>
      <includes>
        <include>/*.*</include>
      </includes>
    </resource>
  </resources>
</build>

This allows you to switch profiles in IntelliJ IDEA.
The configuration adds two profiles, dev and prod, with activeByDefault indicating the default selection.

  • Maven Run: Specify the profile in Maven using -P, for example, mvn spring-boot -Pdev.
  • Maven Package: Specify the profile in Maven using -P, for example, mvn package -Pdev -DskipTests.

During the process of building a WAR package, the resource files are processed by the maven-resources-plugin. Configure the following in the build section of the pom.xml file:

<plugins>
  <plugin>
    <!-- Not related to profiles in this article, but included for completeness -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <!-- Compiler plugin, used to set the JDK version, otherwise the default version is very low -->
    <configuration>
      <source>${java.version}</source>
      <target>${java.version}</target>
      <testSource>${java.version}</testSource>
      <testTarget>${java.version}</testTarget>
    </configuration>
    <version>${maven-compiler-plugin.version}</version>
  </plugin>

 <!-- Resource file processing plugin, must be configured -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>${maven-resources-plugin.version}</version>
    <configuration>
      <delimiters>
        <delimiter>@</delimiter>
      </delimiters>
      <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
  </plugin>
  
</plugins>

Extension#

The maven installation package also contains profiles in the conf/settings.xml configuration file.
As we know, the pom.xml also has the profiles attribute, but the difference between the profiles in settings.xml and the profiles in pom.xml is that the profiles in settings.xml only have four sub-elements: activation, repositories, pluginRepositories, and properties. They are usually used to configure repository information.

<profiles>
   <profile>
     <!-- Unique identifier for the profile -->
     <id>prod</id>
     <!-- Automatic triggering conditions for the profile -->
     <activation />
     <!-- Extended property list -->
     <properties />
     <!-- Remote repository list -->
     <repositories />
     <!-- Plugin repository list -->
     <pluginRepositories />
   </profile>
 </profiles>

<!-- Multiple profiles can be configured. Usually used for global configuration -->
<activeProfiles>
    <!--make the profile active all the time -->
      <activeProfile>ang-thirdparty-nexus</activeProfile>
      <activeProfile>ang-central-nexus</activeProfile>
      <activeProfile>aliyun-nexus</activeProfile>
      <activeProfile>zcg-nexus</activeProfile>
  </activeProfiles>
Loading...
Ownership of this page data is guaranteed by blockchain and smart contracts to the creator alone.