测试复杂的 Mojo 参数

注意:此示例改进了用于测试复杂 Mojo 参数的说明书。

在真正的插件开发中,您将使用特定的 Maven 对象,例如MavenProjectArtifactRepositoryMavenSettings。您可以通过定义存根来使用它们。

假设您在 maven-my-plugin pom 中有以下依赖项:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-artifact</artifactId>
      <version>2.0.8</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-project</artifactId>
      <version>2.0.8</version>
    </dependency>
    ...
  </dependencies>
</project>

您将在MyMojo中添加以下内容:

public class MyMojo
    extends AbstractMojo
{
    /**
     * The Maven Project.
     */
    @Component
    protected MavenProject project;

    /**
     * Local Repository.
     */
    @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
    protected ArtifactRepository localRepository;

    /**
     * The Maven Settings.
     */
    @Component
    private Settings settings;

    ...
}

创建存根

您需要创建存根对象来运行MyMojoTest#testSomething()。按照惯例,包名应该反映存根,即在我们的例子中org.apache.maven.plugin.my.stubs

public class MyProjectStub
    extends MavenProjectStub
{
    /**
     * Default constructor
     */
    public MyProjectStub()
    {
        MavenXpp3Reader pomReader = new MavenXpp3Reader();
        Model model;
        try
        {
            model = pomReader.read( ReaderFactory.newXmlReader( new File( getBasedir(), "pom.xml" ) ) );
            setModel( model );
        }
        catch ( Exception e )
        {
            throw new RuntimeException( e );
        }

        setGroupId( model.getGroupId() );
        setArtifactId( model.getArtifactId() );
        setVersion( model.getVersion() );
        setName( model.getName() );
        setUrl( model.getUrl() );
        setPackaging( model.getPackaging() );

        Build build = new Build();
        build.setFinalName( model.getArtifactId() );
        build.setDirectory( getBasedir() + "/target" );
        build.setSourceDirectory( getBasedir() + "/src/main/java" );
        build.setOutputDirectory( getBasedir() + "/target/classes" );
        build.setTestSourceDirectory( getBasedir() + "/src/test/java" );
        build.setTestOutputDirectory( getBasedir() + "/target/test-classes" );
        setBuild( build );

        List compileSourceRoots = new ArrayList();
        compileSourceRoots.add( getBasedir() + "/src/main/java" );
        setCompileSourceRoots( compileSourceRoots );

        List testCompileSourceRoots = new ArrayList();
        testCompileSourceRoots.add( getBasedir() + "/src/test/java" );
        setTestCompileSourceRoots( testCompileSourceRoots );
    }

    /** {@inheritDoc} */
    public File getBasedir()
    {
        return new File( super.getBasedir() + "/src/test/resources/unit/project-to-test/" );
    }
}
public class SettingsStub
    extends Settings
{
    /** {@inheritDoc} */
    public List getProxies()
    {
        return Collections.EMPTY_LIST;
    }
}

配置项目到测试的pom

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-my-plugin</artifactId>
        <configuration>
          <!-- Specify where this pom will output files -->
          <outputDirectory>target/test-harness/project-to-test</outputDirectory>

          <!-- By default <<<${basedir}/target/local-repo", where basedir refers
               to the basedir of maven-my-plugin. -->
          <localRepository>${localRepository}</localRepository>
          <!-- The defined stubs -->
          <project implementation="org.apache.maven.plugin.my.stubs.MyProjectStub"/>
          <settings implementation="org.apache.maven.plugin.my.stubs.SettingsStub"/>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>