Maven 插件之 maven-antrun-plugin

虽然Maven已经代替Ant,成为Java 工业领域事实上的构建标准.但在某些情况下,如果可以用Ant命令,还是很方便的. 借助 maven-antrun-plugin 插件,可以在Maven执行时,额外执行Ant脚本.

maven-antrun-plugin

打包完成后,把构建结果复制到其他位置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>package</id>
            <phase>package</phase>
            <configuration>
            <target>
                <echo message="copy package to deplay dir"/>
                <copy file="${basedir}/target/archbase-${profiles.evn}-${project.version}.war"
                    tofile="E:/Repositories/workspace/deplay/archbase.war"
                    overwrite="true"/>
            </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • <phase>package</phase> 表示插件要在Maven 的 package 时执行
  • <goal>run</goal> 这时插件内部的一个执行目标
  • <target></target> 之间可以写任何Ant支持的task

执行外部命令

再看一个列子,来自 Maven实战之antrun插件

<plugin>  
    <artifactId>maven-antrun-plugin</artifactId>  
    <executions>  
        <execution>  
            <id>package</id>  
            <phase>package</phase>  
            <goals>  
                <goal>run</goal>  
            </goals>  
            <configuration>  
                <tasks>  
                    <echo message="make ..."/>  
                    <exec dir="src/main/c" executable="make" failonerror="true" />  
                </tasks>  
            </configuration>  
        </execution>  
        <execution>  
            <id>clean</id>  
            <phase>clean</phase>  
            <goals>  
                <goal>run</goal>  
            </goals>  
            <configuration>  
                <tasks>  
                    <echo message="make clean ..."/>  
                    <exec dir="src/main/c" executable="make" failonerror="true">  
                        <arg line="clean"/>  
                    </exec>  
                </tasks>  
            </configuration>  
        </execution>  
    </executions>  
</plugin>

引用外部build.xml文件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <configuration>
                <target>
                    <property name="compile_classpath" refid="maven.compile.classpath"/>
                    <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                    <property name="test_classpath" refid="maven.test.classpath"/>
                    <property name="plugin_classpath" refid="maven.plugin.classpath"/>
                    <ant antfile="${basedir}/build.xml">
                        <target name="test"/>
                    </ant>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>


The build.xml:

<?xml version="1.0"?>
<project name="test6">
    <target name="test">
      <echo message="compile classpath: ${compile_classpath}"/>
      <echo message="runtime classpath: ${runtime_classpath}"/>
      <echo message="test classpath:    ${test_classpath}"/>
      <echo message="plugin classpath:  ${plugin_classpath}"/>
    </target>
</project>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值