让我们想象一下我跟随mojo:
@Mojo(name = "some-goal")
public class MyMojo {
@Parameter(required = true)
protected ComplexObject param;
/*...*/
}
另外我在pom中有插件的描述符:
<plugin>
<!-- here artifact description -->
<executions>
<execution>
<phase>...</phase>
<goals><goal>some-goal</goal></goals>
<configuration>
<param>...</param>
</configuration>
</execution>
</executions>
</plugin>
为了测试这个插件我使用maven-plugin-testing-harness
我的测试代码是:
@Test
public void test() throws Exception {
File pom = getFile("mix/pom.xml");
MyMojo plugin = (MyMojo) rule.lookupMojo("some-goal", pom);
/*....*/
}
规则是:
@Rule
public MojoRule rule = new MojoRule() {
@Override
protected void before() throws Throwable {
}
@Override
protected void after() {
}
};
但是当我运行测试时,它会因异常而失败:
org.apache.maven.plugin.testing.ConfigurationException:无法找到artifactId为{plugin-name}的插件的配置元素。
at org.apache.maven.plugin.testing.AbstractMojoTestCase.extractPluginConfiguration(AbstractMojoTestCase.java:619)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.extractPluginConfiguration(AbstractMojoTestCase.java:582)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.lookupMojo(AbstractMojoTestCase.java:353)
at org.apache.maven.plugin.testing.MojoRule.lookupMojo(MojoRule.java:164)
当我调试maven-plugin-testing-harness的源代码时,我注意到它只从root插件元素读取配置。
如何强制它从执行元素读取配置?
添加空的<configuration></configuration>
块来测试插件配置对我有帮助。
尝试使用这些deps:
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.3.9</version>
</dependency>
Maven插件测试没有很好的描述,看起来有点儿...
有两种方法可以解决此问题。
将调用lookupMojo("some-goal", pom)
更改为lookupEmptyMojo("some-goal", pom)
或者在build -> plugins -> plugin
里面添加一个空的<configuration></configuration>
部分。
<plugin>
<!-- here artifact description -->
<configuration></configuration>
<executions>
<execution>
<phase>...</phase>
<goals><goal>some-goal</goal></goals>
<configuration>
<param>...</param>
</configuration>
</execution>
</executions>
</plugin>