Example usage for org.apache.maven.artifact.handler ArtifactHandler ROLE

List of usage examples for org.apache.maven.artifact.handler ArtifactHandler ROLE

Introduction

In this page you can find the example usage for org.apache.maven.artifact.handler ArtifactHandler ROLE.

Prototype

String ROLE

To view the source code for org.apache.maven.artifact.handler ArtifactHandler ROLE.

Click Source Link

Usage

From source file:com.redhat.tools.nexus.maven.plugin.buildhelper.InjectArtifactHandlerMojo.java

License:Open Source License

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {
    ArtifactVersion currentVersion = ri.getApplicationVersion();
    ArtifactVersion maxUsageVersion = new DefaultArtifactVersion("2.2.0");

    if (maxUsageVersion.compareTo(currentVersion) < 0) {
        getLog().debug(/*from ww w.  j a  va2  s  .  co m*/
                "This version of Maven does not require injection of custom ArtifactHandlers using this code. Skipping.");
        return;
    }

    Map<String, ?> handlerDescriptors = session.getContainer().getComponentDescriptorMap(ArtifactHandler.ROLE);
    if (handlerDescriptors != null) {
        getLog().debug("Registering all unregistered ArtifactHandlers...");

        if (artifactHandlerManager instanceof DefaultArtifactHandlerManager) {
            Set<String> existingHints = ((DefaultArtifactHandlerManager) artifactHandlerManager)
                    .getHandlerTypes();
            if (existingHints != null) {
                for (String hint : existingHints) {
                    handlerDescriptors.remove(hint);
                }
            }
        }

        if (handlerDescriptors.isEmpty()) {
            getLog().debug("All ArtifactHandlers are registered. Continuing...");
        } else {
            Map<String, ArtifactHandler> unregisteredHandlers = new HashMap<String, ArtifactHandler>(
                    handlerDescriptors.size());

            for (String hint : handlerDescriptors.keySet()) {
                try {
                    unregisteredHandlers.put(hint,
                            (ArtifactHandler) session.lookup(ArtifactHandler.ROLE, hint));
                    getLog().info("Adding ArtifactHandler for: " + hint);
                } catch (ComponentLookupException e) {
                    getLog().warn("Failed to lookup ArtifactHandler with hint: " + hint + ". Reason: "
                            + e.getMessage(), e);
                }
            }

            artifactHandlerManager.addHandlers(unregisteredHandlers);
        }
    }

    getLog().debug("...done.\nSetting ArtifactHandler on project-artifact: " + project.getId() + "...");

    Set<Artifact> artifacts = new HashSet<Artifact>();
    artifacts.add(project.getArtifact());

    Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    if (dependencyArtifacts != null && !dependencyArtifacts.isEmpty()) {
        artifacts.addAll(dependencyArtifacts);
    }

    for (Artifact artifact : artifacts) {
        String type = artifact.getType();
        ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);

        getLog().debug("Artifact: " + artifact.getId() + "\nType: " + type + "\nArtifactHandler extension: "
                + handler.getExtension());

        artifact.setArtifactHandler(handler);
    }

    getLog().debug("...done.");
}

From source file:org.jetbrains.maven.embedder.MavenEmbedder.java

License:Apache License

@SuppressWarnings({ "unchecked" })
private Map findArtifactTypeHandlers(MavenProject project) {
    // end copied from DefaultLifecycleExecutor.findExtensions
    Map result = new HashMap();
    for (Object each : project.getBuildPlugins()) {
        Plugin eachPlugin = (Plugin) each;

        if (eachPlugin.isExtensions()) {
            try {
                PluginManager pluginManager = getComponent(PluginManager.class);
                pluginManager.verifyPlugin(eachPlugin, project, mySettings, myLocalRepository);
                result.putAll(pluginManager.getPluginComponents(eachPlugin, ArtifactHandler.ROLE));
            } catch (Exception e) {
                MavenEmbedderLog.LOG.info(e);
                continue;
            }/*from  w  w  w .ja va2  s .c o  m*/

            for (Object o : result.values()) {
                ArtifactHandler handler = (ArtifactHandler) o;
                if (project.getPackaging().equals(handler.getPackaging())) {
                    project.getArtifact().setArtifactHandler(handler);
                }
            }
        }
    }
    return result;
}