Java 使用JUnit测试Maven插件并使用测试POM

Java 使用JUnit测试Maven插件并使用测试POM,java,maven,Java,Maven,我正在开发一个定制的Maven插件。对于测试,我使用JUnit测试和MojoRule。测试本身运行良好(我可以读取测试pom中定义的插件配置)。但是,我想测试对项目工件的访问 到目前为止,我还不知道如何告诉我的测试类: 将单独的目录视为测试项目的源文件夹 在我的插件之前执行package 将MavenProject作为类中的字段获取(在运行时为空) 我的魔咒课是这样的: /** * Identifies potential actions from the project's source

我正在开发一个定制的Maven插件。对于测试,我使用JUnit测试和
MojoRule
。测试本身运行良好(我可以读取测试pom中定义的插件配置)。但是,我想测试对项目工件的访问

到目前为止,我还不知道如何告诉我的测试类:

  • 将单独的目录视为测试项目的源文件夹
  • 在我的插件之前执行
    package
  • MavenProject
    作为类中的字段获取(在运行时为空)
我的魔咒课是这样的:

/**
 * Identifies potential actions from the project's source code.
 *
 * @goal identify
 */
public class IdentifyMojo extends AbstractMojo {

  @Parameter(defaultValue = "${project}")
  private MavenProject project;

  @Override
  public void execute() {
    getLog().info("Project: " + project);
    for (Artifact nextArtifact : project.getArtifacts()) {
      getLog().info("Next artifact: " + nextArtifact);
    }
  }

}
public class IdentifyMojoTest {

  private static final String TEST_POM_FILE_PATH = "src/test/resources";
  private static final String TEST_POM_FILE_NAME = "test-pom.xml";

  private MojoRule rule = new MojoRule();

  @Ignore("Requires mvn install")
  @Test
  public void execute_loadWithPlexus_noExceptions() throws Exception {
    // Arrange
    String goalName = "identify";
    File testPom = getTestPom();

    // Act
    IdentifyMojo identifyMojo = (IdentifyMojo) rule.lookupMojo(goalName, testPom);
    identifyMojo.execute();

    // Assert

  }

  @Rule
  public MojoRule getRule() {
    return rule;
  }

  @NotNull
  @Contract(" -> new")
  private File getTestPom() {
    return new File(TEST_POM_FILE_PATH, TEST_POM_FILE_NAME);
  }

}
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>test-project</artifactId>

  <build>
    <plugins>
      <plugin>
        <groupId>com.example.maven</groupId>
        <artifactId>example-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
测试类如下所示:

/**
 * Identifies potential actions from the project's source code.
 *
 * @goal identify
 */
public class IdentifyMojo extends AbstractMojo {

  @Parameter(defaultValue = "${project}")
  private MavenProject project;

  @Override
  public void execute() {
    getLog().info("Project: " + project);
    for (Artifact nextArtifact : project.getArtifacts()) {
      getLog().info("Next artifact: " + nextArtifact);
    }
  }

}
public class IdentifyMojoTest {

  private static final String TEST_POM_FILE_PATH = "src/test/resources";
  private static final String TEST_POM_FILE_NAME = "test-pom.xml";

  private MojoRule rule = new MojoRule();

  @Ignore("Requires mvn install")
  @Test
  public void execute_loadWithPlexus_noExceptions() throws Exception {
    // Arrange
    String goalName = "identify";
    File testPom = getTestPom();

    // Act
    IdentifyMojo identifyMojo = (IdentifyMojo) rule.lookupMojo(goalName, testPom);
    identifyMojo.execute();

    // Assert

  }

  @Rule
  public MojoRule getRule() {
    return rule;
  }

  @NotNull
  @Contract(" -> new")
  private File getTestPom() {
    return new File(TEST_POM_FILE_PATH, TEST_POM_FILE_NAME);
  }

}
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>test-project</artifactId>

  <build>
    <plugins>
      <plugin>
        <groupId>com.example.maven</groupId>
        <artifactId>example-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
测试POM如下所示:

/**
 * Identifies potential actions from the project's source code.
 *
 * @goal identify
 */
public class IdentifyMojo extends AbstractMojo {

  @Parameter(defaultValue = "${project}")
  private MavenProject project;

  @Override
  public void execute() {
    getLog().info("Project: " + project);
    for (Artifact nextArtifact : project.getArtifacts()) {
      getLog().info("Next artifact: " + nextArtifact);
    }
  }

}
public class IdentifyMojoTest {

  private static final String TEST_POM_FILE_PATH = "src/test/resources";
  private static final String TEST_POM_FILE_NAME = "test-pom.xml";

  private MojoRule rule = new MojoRule();

  @Ignore("Requires mvn install")
  @Test
  public void execute_loadWithPlexus_noExceptions() throws Exception {
    // Arrange
    String goalName = "identify";
    File testPom = getTestPom();

    // Act
    IdentifyMojo identifyMojo = (IdentifyMojo) rule.lookupMojo(goalName, testPom);
    identifyMojo.execute();

    // Assert

  }

  @Rule
  public MojoRule getRule() {
    return rule;
  }

  @NotNull
  @Contract(" -> new")
  private File getTestPom() {
    return new File(TEST_POM_FILE_PATH, TEST_POM_FILE_NAME);
  }

}
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>test-project</artifactId>

  <build>
    <plugins>
      <plugin>
        <groupId>com.example.maven</groupId>
        <artifactId>example-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

4.0.0
测试项目
com.example.maven
maven插件示例

通过将此元素添加到插件的
块,我成功地运行了测试:

<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub"/>

这使得Mojo打印“Project:MavenProject:null:null:null@”

显然,测试工具根本不提供任何默认值,因此我必须自己在配置中注入
MavenProject
的实例。然而,这也解决了我的其他问题,因为我不必运行
目标,只需让我的
MavenProjectStub
返回我在c中构建的一些工件颂歌


来源:

通过将此元素添加到插件的
块,我成功地运行了测试:

<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub"/>

这使得Mojo打印“Project:MavenProject:null:null:null@”

显然,测试工具根本不提供任何默认值,因此我必须自己在配置中插入
MavenProject
的实例。然而,这也解决了我的其他问题,因为我不必运行
目标,只需让我的
MavenProjectStub
返回我在代码中构建的一些工件

资料来源: