Maven 插件之 maven-antrun-plugin

来源:互联网 发布:python高手之路 第3版 编辑:程序博客网 时间:2024/06/15 13:56

虽然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>
1 0