如何在maven-antrun-plugin中使用if条件标签

来源:互联网 发布:武林外传画画老人知乎 编辑:程序博客网 时间:2024/05/17 19:56

场景是这样的

项目打包时,需要调用maven-antrun-plugin执行一个脚本,本地环境(windows)与服务器环境(Linux)需要执行的文件不同,因此需要对OS环境进行判断。

各处找文档

在网上搜到很多ant的条件标签,如if、condition等,加在maven-antrun-plugin怎么着都不报,报找不到这个标签的错误(failed to create task or type if)。

终于在stackoverflow上找到了一篇文章How to execute tasks conditionally using the maven-antrun-plugin 解决了我的问题。

最终我的配置如下

首先pom.xml增加一个依赖,这个依赖是必须的,if等标签在这个依赖中定义

<dependency>    <groupId>ant-contrib</groupId>    <artifactId>ant-contrib</artifactId>    <version>1.0b3</version></dependency>

maven-antrun-plugin配置如下:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-antrun-plugin</artifactId>    <executions>        <execution>            <phase>prepare-package</phase>            <goals>                <goal>run</goal>            </goals>            <configuration>                <tasks>                    <!--这句很重要,ant会加载antcontrib.properties中定义的标签-->                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"                    classpathref="maven.runtime.classpath" />                    <if>                        <contains string="${os.name}" substring="Windows"/>                        <then>                            <echo>${os.name} do not run build.sh</echo>                        </then>                        <else>                            <exec executable="./build.sh"></exec>                        </else>                    </if>                </tasks>            </configuration>        </execution>    </executions></plugin>

大功告成!!!

0 0
原创粉丝点击