IDEA SpringBoot Maven profiles 配置

IDEA SpringBoot Maven profiles 配置


IDEA版本: IntelliJ IDEA 2022.2.3
注意:切换环境之后务必点击一下刷新,推荐点击在这里插入图片描述耗时更短。


application.yaml

spring:
  profiles:
    active: @env@

多环境文件名: application-dev.yamlapplication-prod.yaml

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.14version>
        <relativePath/> 
    parent>

    <groupId>com.examplegroupId>
    <artifactId>maven-profileartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>maven-profilename>
    <description>maven-profiledescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <profiles>
        <profile>
            <id>devid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <properties>
                <env>devenv>
            properties>
        profile>
        <profile>
            <id>prodid>
            <properties>
                <env>prodenv>
            properties>
        profile>
    profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>application.yamlinclude>
                    <include>application-${env}.yamlinclude>
                includes>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

常见报错信息:

org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 3, column 13:
        active: @env@
                ^

解决办法:切换环境之后手动点击一下刷新,推荐点击在这里插入图片描述耗时更短。
IDEA SpringBoot Maven profiles 配置_第1张图片

你可能感兴趣的:(intellij-idea,spring,boot,maven,profiles)