“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Recently, I reviewed the environment construction of springCloud + NacOS, hoping to integrate all commonly used components to practice my skills and enhance my family’s memory.
Project address: gitee.com/wei_rong_xi…
In the process, I found a pit, is the [IDEA + Maven] multi-environment configuration, of course, I can’t remember, just look at a few articles from the Internet, found that these articles are scattered, none of them is complete, so I specially organize this article, hope you will not be cheated again.
A list,
For Java projects, especially with springBoot, Maven and other frameworks, it usually involves multiple environments, such as development environment, test environment, production environment and so on. I remember when I was working in CICA, in addition to the three environments mentioned above, there was also a pre-release environment. At that time, the project was still using SSM (Spring + springMVC + Mybatis) environment, and JAR was imported manually. Combined with eclipse’s pain-in-the-ass packaging, it’s really painful.
Later, with the progress of technology, the project architecture gradually began to shift to Springboot, and various JAR version tools, such as Maven and Gradle, began to work. That’s when I realized how comfortable writing code could be.
Idea’s various plug-ins, as well as the performance, I feel that eclipse is several blocks away, once used can never go back.
I use Maven more than GRADle. From the beginning, I started to learn about Nexus and the differences between Package, install, and deploy. I never had to go back to importing jars manually.
The combination of IDEA and Maven can greatly improve work efficiency. We just need to manually select the environment configuration to package, and then run the code, packaging, can automatically use the environment configuration, without the need to modify the configuration file, reduce the occurrence of errors.
Two, hands-on operation
2.1 spring. Profiles. The active configuration
Suppose we have the following three environments, dev, test, and Production pro:
As shown above, our configuration file is named application-environment.yml
Springboot provides the following configuration that allows us to specify which configuration file to use:
spring:
profiles:
active: dev
Copy the code
As shown above, we have to change its value every time we switch the environment, which is pretty much the same. So we configure variables in the form of yML files. The way variables are used in YML is as follows:
@ @ variables
Copy the code
The variables I defined are as follows:
spring:
profiles:
active: '@env@'
Copy the code
Why single quotes?
If you do not add a single quotation mark, the program will not be recognized and an error will be reported, resulting in a startup failure.
2.2 How to Use @env@?
How exactly is @env@ defined in the previous section used? We need to add the following configuration to the project’s POM. XML file as shown below. If the project has a module, we just need to add it to the parent project’s POM file.
<! Configure different profiles for different production environments -->
<profiles>
<profile>
<! - development - >
<id>dev</id>
<activation>
<! -- Default development environment -->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<! -- custom variable name env as the tag, inside the tag is our configuration file environment suffix -->
<env>dev</env>
</properties>
</profile>
<profile>
<! - production - >
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
<profile>
<! - test - >
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
</profiles>
Copy the code
2.3 Important Configuration
This configuration is not mentioned in many articles on the web. I guess the reason is that their project already has this configuration, so they just add the 2.2 configuration to work.
This may not be friendly to many beginners, so I’ve listed this configuration here for the next time you need it:
<build>
<plugins>
<plugin>
<! -- Springboot uses plugins packaged with Maven -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<! -- Specify the location of the configuration file -->
<directory>src/main/resources</directory>
<includes>
<! -- Read all files under Resources, include means within a specified file, versus excludes To exclude the files under it -->
<include>**/*</include>
</includes>
<! -- Turn on the substitution tag, such as our '@env' substitution -->
<filtering>true</filtering>
</resource>
</resources>
</build>
Copy the code
2.4 Starting a Project
With all the configuration complete, we can specify which environment to use as follows:
As we specify the pro environment, start the project and look at the log output:
You can package your idea directly in Maven:
Note that the last position of the first log when packing is the pro we selected:
Third, summary
So far, the whole introduction is over. Next time when you meet, don’t look everywhere.
To summarize the process:
- Spring.profiles. Active configuration variable @ variable @ in yML file
- Pom.xml profiles, the same number of environments
- Configure the build tag content resource path and variable replacement
This tape source address: gitee.com/wei_rong_xi…