0
点赞
收藏
分享

微信扫一扫

Maven Profile按环境打包

它能干什么呢?

在哪里声明呢?

1. Per Project

Defined in the POM itself (pom.xml).

2. Per User

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

3. Global

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

4. Profile descriptor

不支持3.0,详情请看: https://cwiki.apache.org/MAVEN/maven-3x-compatibility-notes.html#Maven3.xCompatibilityNotes-profiles.xml

虽然有这么多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

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

举报

相关推荐

0 条评论