maven根据profile读取指定环境的配置文件

本文介绍了如何在Maven中利用Profile来根据不同的环境读取相应的配置文件,如`deploy.properties`。通过设置<directory>、<includes>、<filtering>等属性,实现资源配置的过滤和替换。在构建过程中,可以使用mvn clean package -PprofileId命令,选择对应的Profile打包项目,确保在不同环境下正确运行。

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



关键字:利用maven的resources、filter和profile实现不同环境使用不同配置文件 

基本概念说明(resources、filter和profile): 
1.profiles定义了各个环境的变量id 
2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值 
3.resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值 


在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(local),测试环境测试(dev),线上生产使用(product)时,需要不停的去修改这些配制文件,次数一多,相当麻烦。现在,利用maven的filter和profile功能,我们可实现在编译阶段简单的指定一个参数就能切换配制,提高效率


<profiles>

        <!--default开发分支-->

        <profile>

            <id>local</id>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

            <build>

                <filters>

                    <filter>src/main/resources/local/deploy.properties</filter>

                </filters>

                <resources>

                    <resource>

                        <directory>src/main/resources/common/</directory>

                        <filtering>true</filtering>

                    </resource>

                    <resource>

                        <directory>src/main/resources/local/</directory>

                        <filtering>true</filtering>

                    </resource>

                </resources>

            </build>

        </profile>

        <profile>

            <id>test</id>

            <activation>

                <activeByDefault>false</activeByDefault>

            </activation>

            <build>

                <filters>

                    <filter>src/main/resources/test/deploy.properties</filter>

                </filters>

                <resources>

                    <resource>

                        <directory>src/main/resources/common/</directory>

                        <filtering>true</filtering>

                    </resource>

                    <resource>

                        <directory>src/main/resources/test/</directory>

                        <filtering>true</filtering>

                    </resource>

                </resources>

            </build>

        </profile>

    </profiles>


- 先指定 src/main/resources下所有文件及文件夹为资源文件
在不同的profile下资源文件不同


<build>

        <resources>

            <resource>

                <directory>src/main/resources/</directory>

                <filtering>true</filtering>

                <includes>

                    <include>deploy/*</include>

                </includes>

                <targetPath>${project.build.directory}</targetPath>

            </resource>

        </resources>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

deploy文件夹下这些文件中的${key}会被替换掉为真正的值







#!/bin/bash

base="${deploy.base}"

name="${project.name}"

java_home="${deploy.java.home}"

bin_home="${deploy.bin.home}"

conf_home="${deploy.conf.home}/resin"

deploy="${deploy.base}"

webapp="${deploy}/${project.name}"

servers=(${deploy.。。.server})

resin="${deploy.resin.home}"


而${deploy.base} ..这些是在deploy.properites中定义





详解


我们分析下<resource>下面的属性

<directory>: 配置那个目录下的文件通过${key}会被替换成属性值,resource目录下,我们一般放jdbc连接,或配置文件

<includes>:指定那个目录下那个文件

<filtering>:这个配置的意思是过滤上面指定属性文件中的占位符,占位符是${变量名称}这样的形式,maven会自动读取配置文件,然后解析其中的占位符,使用上面pom文件中定义的属性进行替换

<exclueds>:在resource目录下,有很多文件,但用些文件不希望替换,则可以通过<excluede>指定

<filters>:这里的filters与<profile>的filter意思一样,都是指属性文件地址,这个如果上面定义<profile>的时候指定了,这里就不需要了,但有些开发习惯是在<profile>不定义,然后在<build>里指定。




在dispather-servlet下



<context:component-scanbase-package="com.。。。.webapp"/>

        <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <propertyname="locations">

            <list>

                <value>classpath:dubbo.properties</value>

            </list>

        </property>

    </bean>

只有简单配置这些,在使用maven命令的时候 mvn clean package -PprofileId ,就可以根据不同环境打不同的包了



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值