SpaceTrucker
SpaceTrucker

Reputation: 13556

Maven extension is not active during build

I'm trying to create a new maven extension. However when I run a maven build with the extension declared in ${maven.projectBasedir}/.mvn/extensions.xml the ExecutionListener of the extension won't be listening.

The pom.xml of my extension is:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>test-extension</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-component-annotations</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-component-metadata</artifactId>
                <version>1.7.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate-metadata</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

The execution listener is this:

package org.example.testextension;

import org.apache.maven.execution.AbstractExecutionListener;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.ExecutionListener;
import org.codehaus.plexus.component.annotations.Component;

@Component(role =  ExecutionListener.class)
public class Foo extends AbstractExecutionListener {

    static {
        // force exception being thrown
        ((String)null).length();
    }

    public Foo() {
        System.out.println("Building World!");
        throw new RuntimeException("forcing failure");
    }


    @Override
    public void sessionStarted(ExecutionEvent event) {
        System.out.println("Hello World!");
        throw new RuntimeException("forcing failure");
    }

    @Override
    public void mojoStarted(ExecutionEvent event) {
        System.out.println("Failing World!");
        throw new RuntimeException("forcing failure");
    }
}

The generated components.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<component-set>
  <components>
    <component>
      <role>org.apache.maven.execution.ExecutionListener</role>
      <role-hint>default</role-hint>
      <implementation>org.example.testextension.Foo</implementation>
      <description />
      <isolated-realm>false</isolated-realm>
    </component>
  </components>
</component-set>

And META-INF/maven/extension.xml is:

<extension>
    <exportedPackages>
        <exportedPackage>org.example.testextension</exportedPackage>
    </exportedPackages>
    <exportedArtifacts>
        <exportedArtifact>org.apache.maven:maven-core</exportedArtifact>
        <exportedArtifact>org.codehaus.plexus:plexus-component-annotations</exportedArtifact>
    </exportedArtifacts>
</extension>

I try to run the above extension in another project with pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>maven-extension-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</project>

and having ./.mvn/extensions.xml

<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
    <extension>
        <groupId>org.example</groupId>
        <artifactId>test-extension</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </extension>
</extensions>

When running this build I can see that the extension is being tried to download from the maven repo. But nothing else is happening with the extension.

I expected to see the build failing due to the various exceptions being forced in the execution listener. However the build succeeds.

What am I missing so that the execution listener of the extension is being used in the build?

I'm using maven:

Maven home: C:\Program Files\apache\apache-maven-3.6.2\bin\..
Java version: 1.8.0_161, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_161\jre
Default locale: de_DE, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

EDIT:
Problem was also reported to Maven Issue Tracker: https://issues.apache.org/jira/browse/MNGSITE-379

Upvotes: 1

Views: 1486

Answers (1)

mnk
mnk

Reputation: 51

I have just had a similar issue, and in my case the problem was that the extension class files had a format that Maven/Guice/Sisu did not understand(target=17). No errors or warnings from Maven.

When compiling the extension for Java 11, it loaded correctly, and the ExecutionListener was triggered (Apache Maven 3.9.4).

I don't know what class format was supported by Maven 3.6.2 used in original post.

Upvotes: 1

Related Questions