maven的动态打包功能

对于maven而言,打包是其一个非常重要的功能,不仅仅是简单的编译打包的概念,其还通过各种插件支持各种灵活的打包策略。现举一个例子讲解如何动态实现一个web项目的打包:

需求:

      现需要对一个web项目打出不同的3个包,3个包除了有公共的jar和配置文件依赖外,还各自需要依赖不同的特定的jar和特定的配置文件,而且不同的war包名字最好是不同的。

实现:

      我们可以利用 maven的profile机制和maven-war-plugin插件来完成以上需求,所有需要配置和修改的只涉及项目的pom文件,首先我们在pom文件中加入profile,不同的profile引入特定的jar依赖和动态定义${env}变量

   

<profiles>
        <profile>
            <id>one</id>
            <properties>
                <env>one</env>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.xxx</groupId>
                    <artifactId>project-one</artifactId>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>two</id>
            <properties>
                <env>two</env>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.xxx</groupId>
                    <artifactId>project-two</artifactId>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>three</id>
            <properties>
                <env>three</env>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.xxx</groupId>
                    <artifactId>project-three</artifactId>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

然后引入maven-war-plugin插件,实现打包时根据不同的${env}变量动态引入不同的资源文件,并动态设定打包的包名:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>deploy/${env}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                    <warName>project-${env}</warName>
                </configuration>
            </plugin>

然后在项目下使用  mvn clean install -DskipTests=true -P ${profile_id} 进行打包 ,${profile_id}指各个不同的profile的id

转载于:https://www.cnblogs.com/hzhuxin/p/7047508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值