Maven利用profile打不同环境的包

解决方法1

  • profile配置:
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>
  • resource配置:
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/resources.${env}</directory>
        </resource>
    </resources>
  • 工程目录结构:
    工程目录结构
  • war包目录结构:
    war包目录结构

解决方法2

  • profile配置:
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <filters>
                    <filter>src/main/resources/config/db-dev.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>test</id>
            <build>
                <filters>
                    <filter>src/main/resources/config/db-dev.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <filters>
                    <filter>src/main/resources/config/db-prod.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>
  • 原理:
    filtering功能主要用来替换项目中的资源文件(.xml、.properties)当中的${...},比如${jdbc.username},那么如果配置了jdbc.username=root,在项目编译的时候,就会自动的把${jdbc.username}替换为root。

解决方法3

  • profile配置:
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
          	<properties>
                <packaging.excludes>**/logback.xml</packaging.excludes>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <packaging.excludes>**/logback-test.xml</packaging.excludes>
            </properties>
        </profile>
    </profiles>
  • maven-war-plugin配置:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven-war-plugin.version}</version>
        <configuration>
            <packagingExcludes>${packaging.excludes}</packagingExcludes>
        </configuration>
    </plugin>
  • 原理
    maven-war-plugin插件warSourceExcludespackagingExcludes参数:

warSourceExcludes:The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.

解释:warSourceDirectory是maven工程的src\main\webapp目录,warSourceExcludes参数决定不将哪些src\main\webapp目录下的文件copy到webappDirectory(一般是工程的target\${warName}目录)。

packagingExcludes:The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).

解释:在生成webappDirectory(一般是工程的target\${warName}目录)后,要将该目录打包成**.war后缀的压缩文件,packagingExcludes参数决定不将哪些webappDirectory**目录中的文件打到war包中。

两个参数是在不同阶段起作用的,需要注意的是warSourceExcludes只能排除src\main\webapp目录下的文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值