문제

I try create custom format for maven assembly plugin. I use following instruction how create extentions for maven 3: Creating a Custom Build Extension for Maven 3.0

My pom.xml for extention:

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

And empty class:

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

}

And use in my maven project:

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

But mvn failed:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2:assembly (default-cli) on project cl3: A type incompatibility occured while executing org.apache.maven.plugins:maven-assembly-plugin:2.2:assembly: my.maven.extention.pkg.PkgArchiver cannot be cast to org.codehaus.plexus.archiver.Archiver

I don't undertand. Because AbstractArchiver implements org.codehaus.plexus.archiver.Archiver and my class extends this abstract class. My propose: maven use different class loaders for different plugins. It is my first plugin for maven and I don't understand how I can fix it.

Thanks in advance.

도움이 되었습니까?

해결책

I found answer. I try to extend maven-assembly-plugin and I haven not to create extention or plugin, but add my jar to dependencies in this plugin.

I've create simple maven project, 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>

And add component description 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>

And remove anotation from PkgArchiver class. And use this project in dependencies of 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>

다른 팁

You need to add a third component implementing the role PlexusIoResourceCollection.

The following descriptor works for me and gets maven to handle the extension xpi as an archive (zip) extension

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

In the client project you need to use it as plugin dependency

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

Using it as build extension leads to cast problems I haven't investigated.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top