Maven Profile按环境打包

栏目: Java · 发布时间: 5年前

内容简介:在日常开发中,我们项目的开发环境和生产环境以及测试环境往往是不同的,比如:数据库的url等。在项目上生产环境时,就需要修改这些参数,给开发造成不便。 为了解决该问题,Maven 2.0引入了构建配置文件的概念(build profiles)。假如你的生产环境和开发环境所需环境配置不同,生产环境配置文件是它可以在每个项目的

在日常开发中,我们项目的开发环境和生产环境以及测试环境往往是不同的,比如:数据库的url等。在项目上生产环境时,就需要修改这些参数,给开发造成不便。 为了解决该问题,Maven 2.0引入了构建配置文件的概念(build profiles)。

它能干什么呢?

假如你的生产环境和开发环境所需环境配置不同,生产环境配置文件是 pro.properties ,开发环境配置文件是 dev.properties ,那么用maven profile , 你可以实现打包开发环境jar包的时候只将 dev.properties 打包并使用,生产环境打包同理。

在哪里声明呢?

它可以在每个项目的 pom.xml 文件中声明,也可以在maven的用户 setting.xml 下声明,也可以在maven全局环境下设置 setting.xml ,详情如下。

1. Per Project

Defined in the POM itself ( pom.xml ).

2. Per User

Defined in theMaven-settings ( %USER_HOME%/.m2/settings.xml )

3. Global

Defined in the global Maven-settings (${maven.home}/conf/settings.xml)

4. Profile descriptor

不支持3.0,详情请看: cwiki.apache.org/MAVEN/maven…

虽然有这么多define的方式,但是我们一般使用的是第一种 defined in the pom ,因为不见得每个项目的生产环境都一模一样,当然这个也是因个人情况而异。

实战

1. 项目结构

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── fantj
│   │   └── resources
│   │       └── conf
│   │           ├── dev
│   │           │   └── application.properties
│   │           ├── pro
│   │           │   └── application.properties
│   │           └── test
│   │               └── application.properties
│   └── test
│       └── java
复制代码

2. pom.xml

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.env>dev</profile.env>
            </properties>
            <activation>
                <activeByDefault>dev</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profile.env>pro</profile.env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.env>test</profile.env>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>conf/**</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/conf/${profile.env}</directory>
            </resource>
        </resources>
    </build>
复制代码

3. 三个application.properties

dev/application.properties

env=dev
db.url=192.168.0.166  
db.username=db-dev 
db.password=db-dev
复制代码

pro/application.properties

env=pro
db.url=47.xxx.xxx.xxx  
db.username=db-pro
db.password=db-pro
复制代码

test/application.properties

env=test
db.url=127.0.0.1 
db.username=db-test
db.password=db-test
复制代码

4. 打包

mvn clean install -P pro
复制代码
Maven Profile按环境打包

可以看到只将 pro/application.properties 进行了编译。

如果你喜欢我的文章,那麻烦请关注我的公众号,公众号重点分析架构师技术,该公众号还处于初始阶段,谢谢大家的支持。

Maven Profile按环境打包
关注公众号,回复 java架构

获取架构视频资源(后期还会分享不同的优质资源噢)。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

MacTalk 人生元编程

MacTalk 人生元编程

池建强 / 人民邮电出版社 / 2014-2-1 / 45

《MacTalk·人生元编程》是一本随笔文集,主要内容来自作者的微信公众平台“MacTalk By 池建强”。本书撰写于2013年,书中时间线却不止于此。作者以一个70 后程序员的笔触,立于Mac 之上,讲述技术与人文的故事,有历史,有明天,有技术,有人生。70 多篇文章划分为六大主题:Mac、程序员与编程、科技与人文、人物、工具、职场。篇篇独立成文,可拆可合,随时阅读。 此外,作者还对原来......一起来看看 《MacTalk 人生元编程》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

html转js在线工具
html转js在线工具

html转js在线工具