Profile之springboot、Maven

什么是profile
       

想必大家都有这种经历,我们开发项目的时候要有多个环境,如开发环境、测试环境、生产环境,他们的配置文件一般不同,如数据库地址。当我们要向各个环境发布程序时,需要人工处理这些配置文件,这显然麻烦且易错。有了profile,一切问题就简单了。简单讲profile就是一组配置,不同profile提供不同组合的配置,程序运行时可以选择使用哪些profile来适应环境。

1. application-{profile}.properties文件

我们先来搭建一个简单的springboot项目快速了解多profile的使用。项目结构非常简单:

在这里插入图片描述
       除了application.properties还有多个application-{profile}.properties(格式必须为这样),在每个配置文件中项目启动的端口是不一样的。

       在application.properties使用spring.profiles.active=prod来指定生效的配置文件为application-prod.properties.启动项目后可以在控制台看到启动端口为application-prod.properties里配置的server.port=8084在这里插入图片描述

1.1 profile的多种激活方式

第一种就是上面的在配置文件中通过spring.profiles.active=来指定,注意可以激活多个profile,如spring.profiles.active=prod,dev,如果都存在某值,执行last win策略。
通过命令行方式。优先级高于第一种的spring.profiles.active
       执行java -jar xxx.jar,可以观察到服务端口被设置为8082。
       执行java -jar xxx.jar –spring.profiles.active=test,可以观察到服务端口被设置为8083,也就是(test)环境的配置
       
通过虚拟机参数。-Dspring.profiles.active=dev使用编程的方式激活。

@SpringBootApplication
public class Application {
   public static void main(String[] args) {
       ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
       applicationContext.getEnvironment().setActiveProfiles("dev");
   }
}


1.2 spring.profiles.include属性

在这里插入图片描述

     

意思是无论执行哪个profile,都会去加载application-mq.properties里的配置

注意:

只会去加载application-mq.properties里独有的配置,如果application-mq.properties也有server.port字段 和 主配置文件的配置项冲突或者重复  并不会生效。


       可以利用这种方式简化配置文件的书写,如在application-mq.properties书写各个环境都有的mq配置信息,不必都夹杂在application.properties里。

1.3 总结

  1.  application.properties文件是必定要加载的,而且是先加载的
  2. 无论是通过哪种方式指定的。当加载完application文件之后才加载指定的profiles文件
  3. 、如果application文件和指定的profile文件有相同的配置或冲突的配置项,则以profile中的为基准
  4. application文件中写通用的配置项
  5. profile文件中写特定环境的配置项
  6. spring.profiles.include指定公共的配置项(起到了分离的作用)

这样可以简化配置文件的书写。

2. @Profile

通过@Profile注解,我们可以根据所激活的不同的环境,生成不同的bean。
如只在dev环境下启动swagger2:

/**
 * swagger2的配置,只在开发环境dev下存在
 */
@Configuration
@EnableSwagger2
@Profile("dev")
public class Swagger2Config {

}

@profile官方博客介绍

2.1 注解可以使用的位置
@component 或 @Configuration修饰的类上作为元注解修饰自定义注解
任何@Bean修饰的方法上

2.2 自定义注解
       @Profile注解需要接受一个字符串,作为场景名。这样每个地方都需要记住这个字符串。Spring的@Profile注解支持定义在其他注解之上,以创建自定义场景注解。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Profile("dev")
public @interface Dev{
}

       这样就创建了一个@Dev注解,该注解可以标识bean使用于@Dev这个场景。后续就不再需要使用@Profile(“dev”)的方式(这样即可以简化代码)。

2.3 注意
@Profile({“p1”, “!p2”})的意思为p1的profile生效或p2的profile为生效时就会去生成修饰的类
如果要使用不同环境下生成不同配置的同一个bean,方法名不要相同,即尽量不要重载。

3. 和maven的profile一起使用
       

    在开发过程中,我们的项目会存在不同的运行环境,比如开发环境、测试环境、生产环境,而我们的项目在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,这样来回修改,很容易出错,而且浪费劳动力。
       在前面的文章profile之springboot,springboot为我们提供了一种解决方案,而maven也提供了一种更加灵活的解决方案,就是profile功能。

1. 原理
1.1 先看一段pom文件中的profile定义

<profiles>
        <profile>
            <!--不同环境Profile的唯一id-->
            <id>dev</id>
            <properties>
                <!--profiles.active是自定义的字段(名字随便起),自定义字段可以有多个-->
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
    </profiles>

     可以看到定义了多个profile,每个profile都有唯一的id,也包含properties属性。这里为每个profile都定义一个名为profiles.active的properties,每个环境的值不同。当我们打包项目时,激活不同的环境,profiles.active字段就会被赋予不同的值。

1.2 结合resource属性
       这个profiles.active字段可以应用到许多地方,及其灵活。可以在配置文件里被引用(参考此博客);也可以结合pom文件里的resource和filter属性,作为文件名的一部分或者文件夹名的一部分,下面会详细讲解这个用法。

注意:maven的profile用法有许多种,但基本原理就是根据激活环境的不同,自定义字段被赋予不同的值。

2. 应用演示
2.1 项目结构

在这里插入图片描述
       这里定义了dev,prod,test三个文件夹,用来演示maven中profile的使用。注意,每个文件夹里还定义了application-{xxx}.properties件,这里相当于结合springboot的Profile的使用,是我比较推荐的方式,和本文maven的profile使用无关系,在application.properties都有spring.profiles.active=xxx去加载对应的application-{xxx}.properties。

pom文件里的关键配置为


可以看到我们利用resource属性来配置打包时,根据激活的环境来选取要打包的文件夹。我们使用maven命令

mvn clean package
prod环境被默认激活,打包后的包结构为

在这里插入图片描述

可以看到prod文件夹下的配置文件被打包进去,通过激活不同的profile也就实现了动态切换配置文件。

2.2 激活方式
profile的激活方式有很多种

1. 通过maven命令参数

即在使用maven打包时通过-P参数,-P后跟上profile的唯一id,如mvn clean package -Ptest
打包时test的profile被激活,打包后的包结构为:在这里插入图片描述
2. 通过pom文件里的activation属性

<profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <!--activation用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失-->
            <activation>
                <!--配置默认激活-->
                <activeByDefault>true</activeByDefault>
                
                <!--通过jdk版本-->
                <!--当jdk环境版本为1.5时,此profile被激活-->
                <jdk>1.5</jdk>
                <!--当jdk环境版本1.5或以上时,此profile被激活-->
                <jdk>[1.5,)</jdk>

 

                <!--根据当前操作系统-->
                <os>
                    <name>Windows XP</name>
                    <family>Windows</family>
                    <arch>x86</arch>
                    <version>5.1.2600</version>
                </os>

                <!--通过系统环境变量,name-value自定义-->
                <property>
                    <name>env</name>
                    <value>test</value>
                </property>

                <!--通过文件的存在或缺失-->
                <file>
                    <missing>target/generated-sources/axistools/wsdl2java/
                        com/companyname/group</missing>
                    <exists/>
                </file>
            </activation>
        </profile>


       这里我写了多种方式,可以通过activeByDefault、jdk版本、操作系统、系统环境变量(在win10我试了不成功,win7可以,不知道为啥)、文件的存在或缺失,实际项目可以根据需要选取一种即可。这种的优先级低于maven命令参数指定的方式。

3. settings.xml中使用activeProfiles指定(了解即可)
即mave目录下的settings.xml也可以添加下面的代码来指定激活哪个profile。

<activeProfiles>  
     <activeProfile>profileTest1</activeProfile>  
</activeProfiles>  
       值得注意的是

1. setting.xml在当前系统用户的.m2文件夹有(如没有可手动拷贝过去也会生效),针对的当前用户的profile配置,在maven的安装目录下“conf/settings.xml”,针对的是全局的profile配置。

2.profile也可以定义在setting.xml文件中,但是这种方式个人感觉并不实用的,不推荐。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用 Maven 进行 springboot 多模块单元测试和 Jacoco 代码覆盖率统计。 对于多模块项目,需要在父 POM 文件中添加 Jacoco 插件配置,以及指定子模块的测试目录: ```xml <modules> <module>module1</module> <module>module2</module> <module>module3</module> </modules> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report-aggregate</id> <phase>test</phase> <goals> <goal>report-aggregate</goal> </goals> </execution> </executions> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>unit-test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>false</skipTests> <skip>false</skip> <forkCount>1</forkCount> <reuseForks>true</reuseForks> <argLine>${jacoco.agent.argLine}</argLine> <excludedGroups>org.junit.experimental.categories.ExcludeCategory</excludedGroups> <testFailureIgnore>true</testFailureIgnore> <includes> <include>**/*Test.java</include> </includes> </configuration> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> </dependency> </dependencies> </plugin> </plugins> </build> </profile> </profiles> <modules> <module>module1</module> <module>module2</module> <module>module3</module> </modules> <profiles> <profile> <id>unit-test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>false</skipTests> <skip>false</skip> <forkCount>1</forkCount> <reuseForks>true</reuseForks> <argLine>${jacoco.agent.argLine}</argLine> <excludedGroups>org.junit.experimental.categories.ExcludeCategory</excludedGroups> <testFailureIgnore>true</testFailureIgnore> <includes> <include>**/*Test.java</include> </includes> </configuration> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> </dependency> </dependencies> </plugin> </plugins> </build> </profile> </profiles> ``` 这个配置会在编译和测试时启动 Jacoco 代理,同时在测试后生成 Jacoco 报告。 同时,可以使用 Maven Profiles 来区分测试和生产环境的配置,例如上面的配置中就定义了一个名为 `unit-test` 的 Profile。在执行单元测试时,可以使用命令 `mvn clean test -Punit-test` 来指定使用 `unit-test` Profile。 对于单个模块的项目,可以在模块的 POM 文件中添加 Jacoco 插件配置: ```xml <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> ``` 这个配置会在编译和测试时启动 Jacoco 代理,同时在测试后生成 Jacoco 报告。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值