从执行元素获取目标配置

从执行元素获取目标配置

本文介绍了Maven:从执行元素获取目标配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下mojo:

Let's imagine I have following mojo:

@Mojo(name = "some-goal")
public class MyMojo {
    @Parameter(required = true)
    protected ComplexObject param;
    /*...*/
}

我在pom中也有插件的描述符:

Also I have plugin's descriptor in 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

For test this plugin I use 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() {
    }
};

但是当我运行测试时,它失败并出现异常:

But when I run test it fails with Exception:

org.apache.maven.plugin.testing.ConfigurationException:找不到工件ID为​​{plugin-name}的插件的配置元素.

org.apache.maven.plugin.testing.ConfigurationException: Cannot find a configuration element for a plugin with an artifactId of {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 的源代码时,我注意到它仅从根插件元素读取配置.

When I debug source of maven-plugin-testing-harness I noticed that it read configuration from root plugin element only.

如何强制其从执行元素中读取配置?

How can I force it to read configuration from execution element?

推荐答案

添加空的<configuration></configuration>块来测试插件配置对我有帮助.

Adding empty <configuration></configuration> block to test plugin configuration helped me.

尝试使用这些部门:

<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插件测试没有很好地描述,看起来有问题...

Maven plugin testing is not well described and looks buggy...

这篇关于Maven:从执行元素获取目标配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:02