7.Maven隔离

使用Maven进行环境隔离的SpringBoot配置
文章介绍了在企业项目开发中,如何利用Maven的环境隔离功能来管理不同环境(如开发、测试、生产)的配置。通过创建不同的配置文件,设置Maven的profiles,并在SpringBoot应用中动态激活所需配置,实现无缝切换。

1.简介
在企业项目开发时,开发环境和生产环境的配置信息是不一样的,比如连接MySQL、Redis等配置,如果每次部署或者开发都要更改配置文件就太麻烦了,这时就可以使用Maven的环境隔离。

2.隔离
(1).创建配置文件
在这里插入图片描述

(2).pom配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>META-INF/*</include>
                    <include>*</include>
                </includes>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <build.profile.id>dev</build.profile.id>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <build.profile.id>test</build.profile.id>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <build.profile.id>prd</build.profile.id>
            </properties>
        </profile>
    </profiles>

</project>

(3).在SpringBoot配置文件中配置需要启用的配置文件

server:
  port: 8080

spring:
  profiles:
    active: @build.profile.id@

(4).选择prd配置启动项目
在这里插入图片描述
可以看到,激活配置项为prd。

2023-03-19 18:34:40.125  INFO 10509 --- [           main] c.e.s.SpringBootDemoApplication          : Starting SpringBootDemoApplication using Java 1.8.0_144 on wenleideMacBook-Pro.local with PID 10509 (/Users/wenlei/Documents/code/study/springboot/target/classes started by wenlei in /Users/wenlei/Documents/code/study/springboot)
2023-03-19 18:34:40.128  INFO 10509 --- [           main] c.e.s.SpringBootDemoApplication          : The following 1 profile is active: "prd"
2023-03-19 18:34:40.983  INFO 10509 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-03-19 18:34:40.988  INFO 10509 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-03-19 18:34:40.988  INFO 10509 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68]
2023-03-19 18:34:41.103  INFO 10509 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-03-19 18:34:41.103  INFO 10509 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 906 ms
2023-03-19 18:34:41.457  INFO 10509 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-03-19 18:34:41.467  INFO 10509 --- [           main] c.e.s.SpringBootDemoApplication          : Started SpringBootDemoApplication in 1.658 seconds (JVM running for 2.304)
[DEBUG] ======================================================================= [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.112 s [INFO] Finished at: 2025-03-25T18:17:31+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] NullPointerException java.lang.NullPointerException at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.getProjectAndSubModules (LifecycleDependencyResolver.java:102) at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.getProjects (LifecycleDependencyResolver.java:93) at org.apache.maven.lifecycle.internal.MojoExecutor.ensureDependenciesAreResolved (MojoExecutor.java:395) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:327) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:175) at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:76) at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:163) at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:160) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:260) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:172) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:100) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:821) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:270) at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) [ERROR] [DEBUG] Shutting down adapter factory; available factories [file-lock, rwlock-local, semaphore-local, noop]; available name mappers [discriminating, file-gav, file-hgav, gav, static] [DEBUG] Shutting down 'file-lock' factory [DEBUG] Shutting down 'rwlock-local' factory [DEBUG] Shutting down 'semaphore-local' factory [DEBUG] Shutting down 'noop' factory
03-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值