1

我尝试为 Maven 程序集插件创建自定义格式。我使用以下说明如何为 maven 3 创建扩展:为 Maven 3.0 创建自定义构建扩展

我的 pom.xml 用于扩展:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.maven.extention</groupId>
    <artifactId>pkg</artifactId>
    <version>0.0.1</version>
    <name>maven pkg archiver</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-archiver</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-compat</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-component-metadata</artifactId>
                <version>1.5.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate-metadata</goal>
                            <goal>generate-test-metadata</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

和空班:

import java.io.IOException;

import org.codehaus.plexus.archiver.AbstractArchiver;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.component.annotations.Component;

@Component(role = Archiver.class, hint = "pkg")
public class PkgArchiver extends AbstractArchiver {

    @Override
    protected void close() throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    protected void execute() throws ArchiverException, IOException {
        // TODO Auto-generated method stub

    }

    @Override
    protected String getArchiveType() {
        return "pkg";
    }

}

并在我的 Maven 项目中使用:

<extensions>
    <extension>
        <groupId>my.maven.extention</groupId>
        <artifactId>pkg</artifactId>
        <version>0.0.1</version>
        </extension>
</extensions>

但是 mvn 失败了:

[错误] 无法在项目 cl3 上执行目标 org.apache.maven.plugins:maven-assembly-plugin:2.2:assembly (default-cli):执行 org.apache.maven.plugins:maven-assembly 时发生类型不兼容-plugin:2.2:assembly: my.maven.extention.pkg.PkgArchiver 不能转换为 org.codehaus.plexus.archiver.Archiver

我不明白。因为 AbstractArchiver 实现了 org.codehaus.plexus.archiver.Archiver 并且我的类扩展了这个抽象类。我的建议:maven 为不同的插件使用不同的类加载器。这是我的第一个 maven 插件,我不明白如何修复它。

提前致谢。

4

2 回答 2

1

我找到了答案。我尝试扩展 maven-assembly-plugin 并且我没有创建扩展或插件,而是将我的 jar 添加到此插件中的依赖项中。

我创建了简单的 Maven 项目,pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.maven.extention</groupId>
    <artifactId>pkg</artifactId>
    <version>0.0.1</version>
    <name>maven pkg archiver</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-archiver</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>

并添加组件描述src/main/resources/META-INF/plexus/components.xml

<component-set>
    <components>
        <component>
            <role>org.codehaus.plexus.archiver.Archiver</role>
            <role-hint>pkg</role-hint>
            <implementation>cmy.maven.extention.pkg.PkgArchiver</implementation>
            <instantiation-strategy>per-lookup</instantiation-strategy> 
        </component>
    </components>
</component-set>

并从中删除注释PkgArchiver class。并在 maven-assembly-plugin 的依赖项中使用这个项目。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <descriptors>
            <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>my.maven.extention</groupId>
            <artifactId>pkg</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>
</plugin>
于 2010-12-22T16:53:36.963 回答
0

您需要添加第三个组件来实现角色 PlexusIoResourceCollection。

以下描述符适用于我,并让 maven 将扩展 xpi 作为存档 (zip) 扩展处理

<?xml version="1.0" encoding="UTF-8"?>
<component-set>
    <components>

        <component>
            <role>org.codehaus.plexus.archiver.Archiver</role>
            <role-hint>xpi</role-hint>
               <implementation>org.codehaus.plexus.archiver.zip.ZipArchiver</implementation>
            <instantiation-strategy>per-lookup</instantiation-strategy>
        </component>

        <component>
            <role>org.codehaus.plexus.archiver.UnArchiver</role>
            <role-hint>xpi</role-hint>
            <implementation>org.codehaus.plexus.archiver.zip.ZipUnArchiver</implementation>
            <instantiation-strategy>per-lookup</instantiation-strategy>
        </component>

        <component>
            <role>org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection</role>
            <role-hint>xpi</role-hint>
            <implementation>org.codehaus.plexus.archiver.zip.PlexusIoZipFileResourceCollection</implementation>
            <instantiation-strategy>per-lookup</instantiation-strategy>
        </component>

    </components>
</component-set>

在客户端项目中,您需要将其用作插件依赖项

            <artifactId>maven-assembly-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>your.group.id</groupId>
                    <artifactId>xpi-archiver</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                </dependency>
            </dependencies>

使用它作为构建扩展会导致我没有调查过的演员问题。

于 2016-02-08T12:17:16.707 回答