Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Get goal configuration from execution element

Let's imagine I have following mojo:

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

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>

For test this plugin I use maven-plugin-testing-harness

And my test code is:

@Test
public void test() throws Exception {
    File pom = getFile("mix/pom.xml");

    MyMojo plugin = (MyMojo) rule.lookupMojo("some-goal", pom);
    /*....*/

}

Where rule is:

@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: 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)

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?

like image 514
Artsiom Kotau Avatar asked Oct 27 '25 14:10

Artsiom Kotau


2 Answers

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

Try to use these 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 plugin testing is not well described and looks buggy...

like image 193
tjuchniewicz Avatar answered Oct 29 '25 03:10

tjuchniewicz


There are two ways to fix this issue.

Change the call lookupMojo("some-goal", pom) to lookupEmptyMojo("some-goal", pom)

Or inside build -> plugins -> plugin add an empty <configuration></configuration> section.

<plugin>
  <!-- here artifact description -->
  <configuration></configuration>
  <executions>
     <execution>
       <phase>...</phase>
       <goals><goal>some-goal</goal></goals>
       <configuration>
         <param>...</param>
       </configuration>
     </execution>
  </executions>
</plugin>
like image 33
david.tanner Avatar answered Oct 29 '25 02:10

david.tanner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!