maven--profile--使用/教程/示例

本文详细介绍了Maven Profile的使用,包括在pom.xml和settings.xml中声明Profile,通过命令行或环境变量激活Profile,以及Profile的默认激活条件。此外,还讲解了Profile与SpringBoot Profile的区别,并提供了结合SpringBoot配置多环境打包的实例,演示了如何根据Profile打包不同环境的配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文网址:maven--profile--使用/教程/示例_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文用示例说明maven的profile的使用。

mavan的profile作用

        Maven中的profile能让你为不同的环境定义不同的构建;

        Maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。

一个Profiles下面允许出现的元素

<project>
    <profiles>
        <profile>
            <id>...</id>
            <activation>...</activation>
            <properties>...</properties>
            <build>
                <defaultGoal>...</defaultGoal>
                <finalName>...</finalName>
                <resources>...</resources>
                <testResources>...</testResources>
                <plugins>...</plugins>
            </build>
            <reporting>...</reporting>
            <modules>...</modules>
            <dependencies>...</dependencies>
            <dependencyManagement>...</dependencyManagement>
            <distributionManagement>...</distributionManagement>
            <repositories>...</repositories>
            <pluginRepositories>...</pluginRepositories>
        </profile>
    </profiles>
</project> 

profile的位置

可以在以下位置声明profile:

  1. pom.xml:很显然,pom.xml中声明的profile只对当前项目有效。
  2. 用户settings.xml:用户目录下.m2/settings.xml中的profile对本机上该用户所有的Maven项目有效。
  3. 全局settings.xml:Maven安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效。
  4. profiles.xml(Maven 2):还可以在项目根目录下使用一个额外的profiles.xml文件来声明profile,不过该特性已经在Maven 3中被移除。建议用户将这类profile移到settings.xml中。

激活profile的方式 

激活方式是有优先级的,优先级由高到低如下:

  1. 命令
  2. 默认项

命令激活

        通过maven 的-P参数激活指定的profile,参数的值是profile的id,多个profile以逗号分割,如果不想激活某个默认的profile,就在它的id前加个!

示例:mvn clean package -Dmaven.test.skip=true -Ptest,dev,!prod

说明:-Dmaven.test.skip=true 表示跳过测试,可提高打包速度

隐式激活

不同类型的隐式激活方式可以组合使用,如根据同时指定根据操作系统类型和JDK版本来激活profile,只有但两个条件都匹配是才激活之。

默认激活

<profiles> 
    <profile> 
        <id>profileTest1</id> 
        <properties> 
            <hello>world</hello> 
        </properties> 
        <activation> 
            <activeByDefault>true</activeByDefault> 
        </activation> 
    </profile> 
    <profile> 
        <id>profileTest2</id> 
        <properties> 
            <hello>andy</hello> 
        </properties> 
    </profile> 
</profiles> 

        可以在profile中的activation元素中指定激活条件,当没有指定条件,然后指定activeByDefault为true的时候就表示当没有指定其他profile为激活状态时,该profile就默认会被激活。

        所以当我们调用mvn package的时候上面的profileTest1将会被激活,但是当我们使用mvn package –P profileTest2的时候将激活profileTest2,而这个时候profileTest1将不会被激活。 

根据环境激活

根据操作系统激活

<profiles>
    <profile>
        <activation>
            <os>
                <!-- 不必指定所有信息 -->
                <name>linux</name>
                <family>unix</family>
                <arch>amd64</arch>
                <version>3.19.0-30-generic</version>
            </os>
      </activation>
    </profile>
</profiles>

关于OS值的更多信息可以参考:官网 

根据JDK版本激活

<!-- 如果jdk的版本为1.8则激活该profile -->
<profiles>
  <profile>
    <activation>
      <jdk>1.8</jdk>
    </activation>
    </profile>
</profiles>

也可以通过[1.6,1.8)匹配多个jdk版本,关于匹配模式的详细信息可以参考:官网

根据环境变量激活

<!-- 如果环境变量里有`debug`且它的值为`true`则激活 -->
<!-- 也可以不指定`<value>`元素, 则只有环境变量里有`debug`就激活 -->
<profiles>
  <profile>
    <activation>
      <property>
        <name>debug</name>
        <value>true</value>
      </property>
    </activation>
  </profile>
</profiles>

mvn -U clean package -Ddebug=true

通过判断文件是否存在激活 

<profiles>
  <profile>
    <activation>
      <file>
        <missing>/path/to/missing/file</missing>
        <exists>/path/to/exists/file</exists>
      </file>
    </activation>
  </profile>
</profiles>

与SpringBoot整合

简介

maven的profile与SpringBoot的profile的区别

SpringBoot的profile

  1. 作用:运行的时候,把哪几个配置包含进来。
  2. 如果有多个环境,需要在运行jar包的时候通过参数来指定,比如:java -jar test-1.0.0-SNAPSHOT.jar --spring.profiles.active=dev

maven的profile

  1. 作用:
    1. 指定打包的时候只打包哪个或哪几个环境
      1. maven是打包的工具,它的profile可以指定打包的时候只打包哪个或哪几个环境。例如:我只想打包test,就只把resources下边test的配置打包进去。
    2. 因为profile下边可以写dependencies等标签,所以可以在不同环境中使用不同的依赖。
      1. 比如:开发了个新功能,这个新功能是在自己的包里添加的(存放在maven私库中)则在开发对应的profile中升级此依赖的版本;等开发结束后,将测试对应的profile也升级此依赖的版本;测试通过后,则将生产对应的profile也升级此依赖的版本。
  2. 运行jar包的时候就不需要指定参数了。

实例 

pom.xml

配置如下两项:

  1. profiles节点
  2. build -> resources节点
<?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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo_SpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo_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-web</artifactId>
        </dependency>
    </dependencies>

    <!--配置多环境打包-->
    <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <!--自定义的属性-->
                <profile.active>dev</profile.active>
            </properties>
            <activation>
                <!--如果不指定,则默认使用dev开发环境配置-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>prod</id>
            <properties>
                <profile.active>prod</profile.active>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- profile对资源的操作 -->
        <resources>
            <!--打包的时候先去掉所有的配置文件-->
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application*.yml</exclude>
                </excludes>
            </resource>
            <!--添加需要包含的文件-->
            <resource>
                <directory>src/main/resources</directory>
                <!-- 是否替换yml或者properties里@xx@表示的maven properties属性值 -->
                <filtering>true</filtering>
                <!--在打包的时候,根据-P参数,加上需要的yml配置文件-->
                <includes>
                    <include>application.yml</include>
                    <include>application-${profile.active}.yml</include>
                    <!--<include>**/application-${profile.active}.yml</include>-->
                </includes>
            </resource>
        </resources>
    </build>
</project>

这样配置之后,Idea的maven可以展示出profiles选项:

application.yml

spring:
  profiles:
    active: @profile.active@

.yml或者.properties通过@xxx@的方式引入pom.xml里边的内容。

比如,如果上一步中箭头处选择了test,则本处spring.profiles.active就是test

applicaiton-xxx.yml

 里边可以先不写东西。 

测试

选择prod,然后打包:

结果:(确实只将相应的配置文件打包进来了)

 查看demo_SpringBoot-0.0.1-SNAPSHOT.jar

 

总结

  • 用Idea先选profiles(比如test),然后再打包,相当于执行命令:mvn package -Ptest
  • 本处如果我在Idea里边把profiles留空,什么都不选,打包之后的配置文件只有application.yml。按理说,我已经将dev作为默认配置了,应该有applicaiton.yml和application-dev.yml才对,本处暂没有解决。

其他网址

使用 Maven Profile 和 Filtering 打各种环境的包 - SegmentFault 思否
Maven学习总结(49)——Maven Profile详解_科技D人生-CSDN博客
Maven profile介绍_winwill2012的博客-CSDN博客

其他网址

SpringBoot不同profile过滤配置文件_JonathanHsia-CSDN博客
maven profile 和 spring boot profile的区别_看写写-CSDN博客
maven的profile标签的使用_HELLOW,文浩-CSDN博客_maven的profile标签

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT利刃出鞘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值