Maven AntRun 插件详解

2021-09-13  本文已影响0人  rosy_dawn

AntRun 插件只有一个插件目标:antrun:run ,提供了在 Maven 中运行 Ant 任务的能力。您甚至可以将 Ant 脚本嵌入 POM 中,但该插件的目的不是提供污染 POM 的方法,因此鼓励您将所有 Ant 任务移动到 build.xml 文件中的 <target/> 元素中,并使用 Ant 的 <ant/> 任务从 POM 调用它。这个插件的主要目的之一是促进从基于 Ant 的项目到 Maven 的迁移。一些项目目前可能无法迁移,因为它们依赖于 Maven 默认情况下不提供的自定义构建功能。

所有可选参数

示例

下面是一个使用 maven-antrun-plugin 插件的模板:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase> <!-- a lifecycle phase --> </phase>
            <configuration>
              <target>

                <!--
                  Place any Ant task here. You can add anything
                  you can add between <target> and </target> in a
                  build.xml.
                -->

              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

此外,您可以通过复制 <execution/> 部分并指定一个新阶段,向每个生命周期阶段添加脚本。最后,您可以在 <target/> 元素中指定一些 Ant <target/> property。仅未包装 Ant <target/> 中的 dependens attribute。

[...]
<configuration>
  <target name="The name of the target"
    if="The name of the property that must be set in order for this task"
    unless="The name of the property that must NOT be set in order for this task"
    description="A short description of this target's function">

    <!--
      Place any Ant task here. You can add anything
      you can add between <target> and </target> in a
      build.xml.
    -->

  </target>
<configuration>
[...]

Maven 可用的所有 property 在 target 配置中也可用。但是,您可能希望使用 Ant 任务调用外部 Ant 构建脚本。为了避免名称冲突,只将 properties 的子集(而不是全部)传递给外部 Ant 构建。这些包括 POM properties 部分中定义的所有 property。它还包括一些常用 Maven property 的前缀版本。

  maven.project.groupId
  maven.project.artifactId
  maven.project.name
  etc.

如果要使用的 Maven property 在外部文件中不可用,则必须在调用 Ant 之前重新定义该 property。

  <property name="maven.project.url" value="${project.url}"/>
  <ant antfile="build.xml"/>

一些 Ant 表达式在 Maven 中有各自的对应功能。因此,可以简单地调用对应的 Maven 功能,而不是使用 Maven AntRun 插件,以避免不必要的开销。

Ant 功能 对应的Maven功能
内置任务
Ant maven-antrun-plugin
AntCall maven-antrun-plugin
Available profiles
BUnzip2 maven-assembly-plugin
BZip2 maven-assembly-plugin
Chmod maven-assembly-plugin
Condition profiles
Copy maven-resources-plugin
Dependset maven-dependency-plugin
Ear maven-ear-plugin
Filter maven-resources-plugin Note: Filter uses the @...@ token while maven-resources-plugin uses the $... token
FixCRLF maven-resources-plugin
GenKey maven-jar-plugin
GUnzip maven-assembly-plugin
GZip maven-assembly-plugin
Jar maven-jar-plugin
Javac maven-compiler-plugin
Javadoc/Javadoc2 maven-javadoc-plugin
LoadProperties maven-resources-plugin
Manifest maven-jar-plugin
Property maven-resources-plugin
Replace maven-resources-plugin Note: Replace can specify its token while maven-resources-plugin uses the $... token
Tar maven-assembly-plugin
Unjar maven-assembly-plugin
Untar maven-assembly-plugin
Unwar maven-assembly-plugin
Unzip maven-assembly-plugin
War maven-war-plugin
Zip maven-assembly-plugin
可选任务
Antlr maven-antlr-plugin
Depend maven-dependency-plugin
EJB Tasks maven-ejb-plugin
FTP maven-deploy-plugin Note: maven-deploy-plugin can only deploy unto the FTP
JavaCC maven-compiler-plugin
JJDoc maven-compiler-plugin
JJTree maven-compiler-plugin
JUnit maven-surefire-plugin
JUnitReport maven-surefire-report-plugin
ServerDeploy maven-deploy-plugin
Setproxy maven-deploy-plugin
Translate maven-resources-plugin Note: Translate can specify its own tokens and can have a different encoding scheme for reading and writing files. maven-resources-plugin however uses the $... annotation only and has only one encoding scheme for reading and writing

引用 Maven 类路径

在 Ant 构建中为每个项目依赖项设置一个属性。每个属性名称都使用格式 groupId:artifactId:type[:classifier]。例如,要使用 groupId 为 org.apache 、artifactId 为 common-util 的 依赖 jar 的路径,可以使用以下命令。

<echo message="${org.apache:common-util:jar}"/> 

如果依赖项包括 classifer,则 classifer 将附加到 property 名称。例如,groupId 为 org.apache、artifactId 为 common-util、类型为 jar 和 classifer 为 jdk14。

<echo message="${org.apache:common-util:jar:jdk14}"/> 

从 3.0.0 版本起,不支持旧格式 maven.dependency.groupId.artifactId[.classifier].type.path

您还可以使用以下类路径引用:

例如,要使用 antrun 显示 Maven 的类路径,我们可以这样做:

<project>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-test-app</artifactId>
  <groupId>my-test-group</groupId>
  <version>1.0-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</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"/>

                <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>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

或者,我们也可以使用外部 build.xml。

<project>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-test-app</artifactId>
  <groupId>my-test-group</groupId>
  <version>1.0-SNAPSHOT</version>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</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>
    </plugins>
  </build>
</project>

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>

使用 <target/> attribute

您可以在 <target/> 配置中指定 attributes 来执行或不执行 Ant 任务,具体取决于某些条件。例如,要跳过 Ant 调用,可以添加以下内容:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase> <!-- a lifecycle phase --> </phase>
            <configuration>
              <target unless="maven.test.skip">
                <echo message="To skip me, just call mvn -Dmaven.test.skip=true"/>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

使用 Ant 默认 jar 中未包含的任务

要使用 Ant jar 中未包含的 Ant 任务,如 Ant 可选任务或自定义任务,您需要将任务运行所需的依赖项添加到插件类路径,并在需要时使用maven.plugin.classpath 引用。

<project>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-test-app</artifactId>
  <groupId>my-test-group</groupId>
  <version>1.0-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>ftp</id>
            <phase>deploy</phase>
            <configuration>
              <target>
                <ftp action="send" server="myhost" remotedir="/home/test" userid="x" password="y" depends="yes" verbose="yes">
                  <fileset dir="${project.build.directory}">
                    <include name="*.jar" />
                  </fileset>
                </ftp>
                <taskdef name="myTask" classname="com.acme.MyTask" classpathref="maven.plugin.classpath"/>
                <myTask a="b"/>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>1.4.1</version>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-commons-net</artifactId>
            <version>1.8.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>
上一篇下一篇

猜你喜欢

热点阅读