Maven 扩展在构建期间未激活

问题描述 投票:0回答:1

我正在尝试创建一个新的 Maven 扩展。但是,当我使用

${maven.projectBasedir}/.mvn/extensions.xml
中声明的扩展运行 Maven 构建时,扩展的
ExecutionListener
将不会监听。

我的扩展的

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>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>

执行监听器是这样的:

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");
    }
}

生成的

components.xml
为:

<?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>

并且

META-INF/maven/extension.xml
是:

<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>

我尝试在另一个项目中使用

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>

并拥有

./.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>

运行此构建时,我可以看到正在尝试从 Maven 存储库下载扩展。但扩展没有发生任何其他事情。

我预计会由于执行侦听器中强制出现各种异常而导致构建失败。然而构建成功了。

我缺少什么以便在构建中使用扩展的执行监听器?

我正在使用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"

编辑:
问题也报告给 Maven Issue Tracker: https://issues.apache.org/jira/browse/MNGSITE-379

java maven build maven-extension
1个回答
0
投票

我刚刚遇到了类似的问题,就我而言,问题是扩展类文件的格式 Maven/Guice/Sisu 不理解(target=17)。 Maven 没有错误或警告。

编译 Java 11 扩展时,它正确加载,并且触发了 ExecutionListener (Apache Maven 3.9.4)。

我不知道原始帖子中使用的 Maven 3.6.2 支持什么类格式。

© www.soinside.com 2019 - 2024. All rights reserved.