You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Cristiano Gavião <cv...@gmail.com> on 2016/01/07 16:38:43 UTC

Strange problem with custom ArtifactHandler

Hello,

I'm facing a problem with a plugin that has a custom ArtifactHandler for 
a custom packaging type. The custom class is being registered by sisu 
and no problem was reported while running a build, except when a project 
artifact has one artifact of this custom type as a dependency.

The problem is that maven is using the DefaultArtifactHandler for the 
dependency and not the custom one and then I'm getting a 
ClassCastException in my plugin.

This is how I declared the custom ArtifactHandler. The value of constant 
is "ip.zip" :

    @Named(CommonMojoConstants.IP_PACKAGING)
    @Singleton
    @Typed(value = { ArtifactHandler.class, ExtendedArtifactHandler.class })
    public class IpArtifactHandler extends AbstractTypeHandler {

         @Inject
         public IpArtifactHandler() {
             super(CommonMojoConstants.IP_PACKAGING);
             setIncludesDependencies(true);
             setExtension(CommonMojoConstants.IP_EXTENSION);
             setLanguage(CommonMojoConstants.LANGUAGE_JAVA);
             setAddedToClasspath(true);
         }

I added a log to try understand what is happen before exception occur.

    [WARN]
    CONVERTING:org.apache.maven.artifact.handler.DefaultArtifactHandler
    | distribution.ip.node

The interesting thing is that I have another custom ArtifactHandler and 
it is working well. the unique relevant difference between both is the 
type name. one has a dot and the other just a single word: "bundle":

    [WARN]
    CONVERTING:org.lunifera.maven.lib.mojo.handlers.BundleArtifactHandler |
    org.apache.felix.scr


I want to investigate if the dot is the culprit. Could someone tell me 
which class is responsible to set the ArtifactHandler for a dependency 
object ?

thanks,

Cristiano

Re: Strange problem with custom ArtifactHandler

Posted by Robert Scholte <rf...@apache.org>.
Hi,

RepositoryUtils is a utility class, whereas an ArtifactHandler is a  
Component (i.e. injectable), so these 2 don't mix that well together.
IMHO the Artifact is way too big and too complex. It is a mixture of a  
pojo with component(s), and that's probably also the reason why there was  
an ArtifactFactory in Maven2 to construct an Artifact. (yes it is still  
there in Maven3, but deprecated)
The way Aether handles artifacts is much better, but since almost every  
plugin uses the Maven Artifact it is quite impossible to redesign this  
class.

So one could say it is a bug, but instead I think there's room for  
improvement (when using custom ArtifactHandlers)

thanks,
Robert

Op Thu, 07 Jan 2016 22:10:17 +0100 schreef Cristiano Gavião  
<cv...@gmail.com>:

> After having lost many hours I finally found where the problem is likely
> to be... :-(
>
> maven is using aether for dependency resolution, but aether has not a
> concept of packaging and extension as maven has. it only has extension.
>
> as a workaround maven is saving the packaging type inside the aether's
> DefaultArtifact properties map field. that is ok...
>
> But the values saved in aether's class is not being used properly when
> converting again to maven Artifact after resolution.
> Instead of searching for an existent ArtifactHandle instance in the
> current ArtifactHandlerManager instance a new one is being created in
> RepositoryUtils:
>
>         org.apache.maven.artifact.Artifact toArtifact( Artifact artifact  
> ){
>
>              ArtifactHandler handler = newHandler( artifact );
>
> -------------------------------------------------------------------------------------
>
>             public static ArtifactHandler newHandler( Artifact artifact )
>          {
>              String type = artifact.getProperty(
>     ArtifactProperties.TYPE, artifact.getExtension() );
>     *DefaultArtifactHandler handler = new DefaultArtifactHandler( type  
> );*
>
> It is a bug or it was intentionally done this way?
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Strange problem with custom ArtifactHandler

Posted by Cristiano Gavião <cv...@gmail.com>.
After having lost many hours I finally found where the problem is likely 
to be... :-(

maven is using aether for dependency resolution, but aether has not a 
concept of packaging and extension as maven has. it only has extension.

as a workaround maven is saving the packaging type inside the aether's 
DefaultArtifact properties map field. that is ok...

But the values saved in aether's class is not being used properly when 
converting again to maven Artifact after resolution.
Instead of searching for an existent ArtifactHandle instance in the 
current ArtifactHandlerManager instance a new one is being created in 
RepositoryUtils:

        org.apache.maven.artifact.Artifact toArtifact( Artifact artifact ){

             ArtifactHandler handler = newHandler( artifact );

-------------------------------------------------------------------------------------

            public static ArtifactHandler newHandler( Artifact artifact )
         {
             String type = artifact.getProperty(
    ArtifactProperties.TYPE, artifact.getExtension() );
    *DefaultArtifactHandler handler = new DefaultArtifactHandler( type );*

It is a bug or it was intentionally done this way?



Re: Strange problem with custom ArtifactHandler

Posted by Cristiano Gavião <cv...@gmail.com>.
Well, is not the dot the culprit...

I tried with another packaging name and I got the same result.

I still don't know who injects the ArtifactHandler in the dependency...

any idea ?



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org