Writing an Ant Maven Plugin

You can write Maven plugins based on Ant scripts.

Pom configuration

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-script-ant</artifactId>
      <version>2.2.1</version>
    </dependency>
  </dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.5</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-tools-ant</artifactId>
            <version>3.5</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Files structure

The Ant plugin consists in two files. If you want to create a touch plugin, you must have:

  • src/main/scripts/touch.mojos.xml: contains Mojo(s) descriptor informations,
  • src/main/scripts/touch.build.xml: contains the Ant xml to execute.

Ant Mojos descriptor

<pluginMetadata
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/PLUGIN-METADATA/1.1.0"
    xsi:schemaLocation="http://maven.apache.org/PLUGIN-METADATA/1.1.0 http://maven.apache.org/xsd/plugin-metadata-1.1.0.xsd">
  <mojos>
    <mojo>
      <!-- target name to call in ant script -->
      <call>touch-file</call>
      <!-- mojo goal name -->
      <goal>touch</goal>
      <parameters>
        <parameter>
          <name>name</name>
          <expression>${name}</expression>
          <required>true</required>
          <readonly>false</readonly>
          <type>java.lang.String</type>
        </parameter>
      </parameters>
    </mojo>
  </mojos>
</pluginMetadata>

Ant script

<project>
  <target name="touch-file">
    <touch mkdirs="true" file="target/${name}"/>
  </target>
</project>