Gitee link

AutoPackageDemo, automatic recognition of packaging environment

Spring Boot version: 2.3.4.RELEASE

Maven project

scenario

When we package a project and need to switch environments, we usually change the specified environment in application.yml:

Spring: Profiles: # active: dev # Development environment active: Pro # Production environmentCopy the code

purpose

We want to avoid frequent configuration file changes and instead add the specified environment to the packaging directive, like this:

Spring: Profiles: Active: @ActivatedProperties @ # Automatically identify the environmentCopy the code

Package instruction:

mvn clean package -Dmaven.test.skip=true -P pro

implementation

All you need to do is modify pom.xml

pom.xml

<? The XML version = "1.0" encoding = "utf-8"? > < 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 < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Cc < / groupId > < artifactId > autoPackageDemo < / artifactId > < version > 1.0.0 < / version > < the parent > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <dependencies> <! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>dev</id> <properties> <activatedProperties>dev</activatedProperties> </properties> <activation> <! -- Dev development configuration is used by default --> <activeByDefault>true</activeByDefault> </activation> </profile> <! <profile> < ID >pro</ ID > <properties> </activatedProperties> Pro </activatedProperties> </properties> </profile> <! If there are other circumstances, continue to add --> <! -... --> </profiles> <build> <! <resources> <resource> <directory> SRC /main/resources</directory> <filtering>true</filtering> </resource>  </resources> <! <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Configuration file:

Application. Yml:

Server: port: 8888 Spring: Profiles: Active: @ActivatedProperties @ # Automatic environment identificationCopy the code

Application – dev. Yml:

myvalue: dev
Copy the code

Application – pro. Yml:

myvalue: pro
Copy the code

test

Create a new interface to test:

package com.cc.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Value("${myvalue}") private String myvalue; @getMapping ("/env") public String env() {return "myValue; }}Copy the code

Results:

  • The dev environment is started by default in the compiler, and the result is:

    • The current startup environment is: dev
  • Specify jar package for pro environment, request result is:

    • The current startup environment is pro

Note: After packaging, if the compiler is running disorganized, try maven clean and Maven Install to clean up the old cache.