1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > maven pom profiles

maven pom profiles

时间:2023-02-06 01:07:20

相关推荐

maven pom profiles

maven pom profiles

1、profiles是什么?有什么作用

在maven构建的项目都存在一个pom.xml的项目对象模型配置文件,用于约束项目(如:jar包管理、构建管理等)。profiles是pom.xml中的一个配置项。

我们在开发项目时一般都会区分线上环境和测试环境,这两个环境需要切换以适应不同的环境需求

正式环境的配置,一般放置于src/main/resources下,而测试环境放置于/src/test/resources下面。

profile的主要作用就是区分正式环境和测试环境的配置

2、如何配置

<profiles><profile><id>release</id><build><resources><resource><directory>src/main/resources</directory><includes><include>**/*</include></includes></resource></resources></build></profile><profile><id>test</id><build><resources><resource><directory>src/test/resources</directory><includes><include>config/*.properties</include><include>log4j.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include></includes><excludes><exclude>log4j.xml</exclude></excludes></resource></resources></build><activation><activeByDefault>true</activeByDefault></activation></profile></profiles>

如上, 我配置了两个profile一个release用于正式环境发布,test用于测试环境使用;默认开启测试环境,activation->true

测试环境与正式环境只是部分的配置不同,我们需要公用这部分配置。这就使用到了includes and excludes,用引入和排除配置文件

3、区别构建发布包

构建测试包:

maven package -P test -Dmaven.test.skip=true

1

构建正式包:

maven package -P release -Dmaven.test.skip=true

其他示例

<!-- 定义配置文件 --><profiles><!-- 配置文件 id 名字 properties 参数 --><profile><id>dev</id><!--默认激活当前配置--><activation><activeByDefault>true</activeByDefault></activation><properties><profiles.active>dev</profiles.active><!-- 众多参数配置 --></properties></profile><profile><id>pro</id><properties><profiles.active>pro</profiles.active><!-- 众多参数配置 --></properties></profile><profile><id>test</id><properties><profiles.active>test</profiles.active><!-- 众多参数配置 --></properties></profile></profiles><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><resources><!-- 配置一个 resource directory 为对象目录excludes->exclude build时排除includes->include build时包含--><resource><directory>src/main/resources/</directory><excludes><exclude>config/dev/*</exclude><exclude>config/pro/*</exclude><exclude>config/test/*</exclude></excludes><includes><!--如果有其他定义通用文件,需要包含进来--><include>views/*</include></includes></resource><resource><!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包--><directory>src/main/resources/config/${profiles.active}</directory></resource></resources></build>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。