maven根据不同环境打包不同文件

一, 我们平时在项目开发过程中会根据不同的环境打包不同的文件。如数据库连接文件,三方接口文件,日志文件等等,他们在开发,测试和线上环境的配置不一样,部署测试环境或线上环境都要改相应文件,很繁琐。可以利用maven maven-resources-plugin插件帮我们简化这一过程。
二.配置pom文件:

 <build>
  <plugins>
  <!-- 不同环境的配置文件选择 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <!-- 覆盖原有文件 -->
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                            <!-- 待处理的资源定义 -->
                            <resources>
                                <resource>
                                    <!-- 指定resources插件处理哪个目录下的资源文件 -->
                                    <directory>src/main/resources/${package.environment}</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                        <inherited></inherited>
                    </execution>
                </executions>
            </plugin>


  </plugins>
   <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>test/**</exclude>
                    <exclude>product/**</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
  </build>
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
    </profiles>

三.src/main/resources目录下创建不同环境下的文件
1.本地开发环境直接在此目录下添加;
2.测试环境在此目录下创建名为”test”的文件夹,下面放的是测试环境部署文件;
3.线上环境在此目录下创建名为”product”的文件夹,下面放的是线上环境部署文件。
这里写图片描述
四.打包
1.本地打包,默认maven打包
这里写图片描述
这里写图片描述
2.测试环境打包,添加clean install -Ptest maven 参数
这里写图片描述
这里写图片描述
3.生产环境打包,添加clean install -Pproduct
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值