17.5.2. 多值的Mojo参数

插件的参数可以接受多于一个的值。看一下Example 17.7, “一个带有多值参数的插件”中的ZipMojo。参数includesexcludes都是多值的String数组,它们为一个创建ZIP文件的组件指定了包含和排除模式。

Example 17.7. 一个带有多值参数的插件

package org.sonatype.mavenbook.plugins

/**
 * Zips up the output directory.
 * @goal zip
 * @phase package
 */
public class ZipMojo extends AbstractMojo
{
    /**
     * The Zip archiver.
     * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#zip}"
     */
    private ZipArchiver zipArchiver;

    /**
     * Directory containing the build files.
     * @parameter expression="/data/hudson-temporal-data/hudson-orchestrator-home/workspace/Book-To-Production/content-zh/target"
     */
    private File buildDirectory;

    /**
     * Base directory of the project.
     * @parameter expression="/data/hudson-temporal-data/hudson-orchestrator-home/workspace/Book-To-Production/content-zh"
     */
    private File baseDirectory;

    /**
     * A set of file patterns to include in the zip.
     * @parameter alias="includes"
     */
    private String[] mIncludes;

    /**
     * A set of file patterns to exclude from the zip.
     * @parameter alias="excludes"
     */
    private String[] mExcludes;

    public void setExcludes( String[] excludes ) { mExcludes = excludes; }

    public void setIncludes( String[] includes ) { mIncludes = includes; }

    public void execute()
        throws MojoExecutionException
    {
        try {
            zipArchiver.addDirectory( buildDirectory, includes, excludes );
            zipArchiver.setDestFile( new File( baseDirectory, "output.zip" ) );
            zipArchiver.createArchive();
        } catch( Exception e ) {
            throw new MojoExecutionException( "Could not zip", e );
        }
    }
}

要配置一个多值的Mojo参数,你应该为这类参数使用一组元素。如果这个多值参数的名称是includes,你就可以使用一个includes元素,它包含一组include子元素。如果这个多值参数的名称是excludes,你就应该使用带有exclude子元素的excludes元素。要配置ZipMojo使其忽略所有以.txt及波浪号结尾的文件,你可以使用如下的插件配置。


<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.sonatype.mavenbook.plugins</groupId>
        <artifactId>zip-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/*.txt</exclude>
            <exclude>**/*~</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>