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

来源:互联网 发布:杜兰特数据 编辑:程序博客网 时间:2024/05/18 23:55



关键字:利用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 ,就可以根据不同环境打不同的包了



0 0