Maven 使用profile来构建不同的环境

来源:互联网 发布:数据采集平台 编辑:程序博客网 时间:2024/05/20 08:43

目标: 同一份配置文件 在不同的环境中使用不同的配置数据

工具: maven

实现过程:

1.创建一个maven的web工程



2. pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">    <parent>        <artifactId>hello</artifactId>        <groupId>com.dusk</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>world</artifactId>    <packaging>war</packaging>    <name>world Maven Webapp</name>    <url>http://maven.apache.org</url>    <build>        <finalName>world</finalName>        <resources>            <resource>                <directory>src/main/resources</directory>                <filtering>true</filtering>                <excludes>                    <exclude>profile/**/*.*</exclude>                </excludes>            </resource>            <resource>                <directory>src/main/resources/profile/${env.name}</directory>            <filtering>true</filtering>        </resource>        </resources>    </build>    <profiles>        <profile>            <id>dev</id>            <properties>                <env.name>dev</env.name>            </properties>        </profile>        <profile>            <id>test</id>            <properties>                <env.name>test</env.name>            </properties>        </profile>    </profiles></project>

3. 各个环境的配置文件

test:

name=3

dev:

name=2

4. 打包命令

//测试环境mvn clean package -P test//开发环境mvn clean package -P dev

5.结果

可以查看打好的war包中的sql.properties文件内容,已经实现我们的前期要求。


6.总结

对于maven的profile,我的理解就是相当于一个变量或者占位符 在编译的时候使用你传过来的入参做相应的替换。

这种替换不仅仅可以在<filter>中也可以在<resource>中使用。







阅读全文
1 0
原创粉丝点击