Maven插件maven-antrun-plugin的使用


以下引用官方的介紹http://maven.apache.org/plugins/maven-antrun-plugin/

一、什么是maven-antrun-plugin?

該插件提供從Maven內運行Ant任務的功能。您甚至可以將您的Ant腳本嵌入POM!

這個插件不是提供污染POM的手段意圖,因此它鼓勵所有Ant任務移動到build.xml文件並使用Ant的POM的<ant/> task調用它(或者說所有在build.xml文件的Ant任務移動到POM中,並使用<ant/> task調用它)。

這個插件的主要目的之一是方便從Ant基礎項目遷移到Maven。某些項目可能無法遷移,因為它們依賴於默認情況下Maven不提供的自定義構建功能。

二、坐標

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>

三、簡單使用,參考官網http://maven.apache.org/plugins/maven-antrun-plugin/usage.html

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <phase> <!-- Maven的生命周期階段 --> </phase>
            <configuration>
              <target>

                <!--
                  將任務Ant任務放在這里,還可以在這里添加一個build.xml文件
                -->

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

當然,如上所示的配置其實還少了幾個屬性,不過這不影響運行,因為默認是繼承超級POM的。

提示:

1、Maven的生命周期階段參考:http://www.cnblogs.com/EasonJim/p/6816340.html

2、在<target>中的標簽只有搞過Ant集成的才會比較熟悉,如果想要使用其中的標簽,參考官方提供的文檔:http://ant.apache.org/manual/tasksoverview.html

以下將展示官方實際項目上的POM:

http://maven.apache.org/plugins/maven-antrun-plugin/examples/classpaths.html

http://maven.apache.org/plugins/maven-antrun-plugin/examples/tasksAttributes.html

http://maven.apache.org/plugins/maven-antrun-plugin/examples/customTasks.html

<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>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"/>

                <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>

注意:<id>為唯一的標識,可以是任意。

四、Ant插件只有一個目標(goal)

既:antrun:run,參考:http://maven.apache.org/plugins/maven-antrun-plugin/run-mojo.html

五、其它Ant集成例子

參考:http://maven.apache.org/plugins/maven-antrun-plugin/tasks/tasks.html

 

總結:

1、簡單的說這個Ant插件就是為了方便把之前在Ant集成的任務遷移到Maven中去、

2、在Maven中的<target>中可以很方便的使用Ant的標簽。

3、通過<phase>可以很方便的指定Maven的生命周期。並且指定生命周期之后,可以在生命周期運行時直接觸發<target>中的任務。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM