1 package com.aurea.maven.plugins.util; 2 3 /* 4 * Sonatype Application Build Lifecycle 5 * Copyright (C) 2009 Sonatype, Inc. 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see http://www.gnu.org/licenses/. 19 * 20 */ 21 22 import java.util.HashSet; 23 import java.util.Set; 24 25 import org.apache.maven.artifact.Artifact; 26 import org.apache.maven.artifact.handler.ArtifactHandler; 27 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; 28 import org.apache.maven.execution.MavenSession; 29 import org.apache.maven.plugin.Mojo; 30 import org.apache.maven.plugin.MojoExecutionException; 31 import org.apache.maven.plugin.logging.Log; 32 import org.apache.maven.project.MavenProject; 33 34 /** 35 * Injects an {@link ArtifactHandler} instance, loaded from build extensions, into the current project's 36 * {@link Artifact} instance. The new handler is loaded using the project's packaging. This mojo compensates for bugs in 37 * the build-extension handling of Maven versions prior to 2.2.1. 38 * 39 * @goal inject-artifact-handler 40 * @phase initialize 41 */ 42 public class InjectArtifactHandlerMojo implements Mojo { 43 44 45 /** 46 * The current project instance. 47 * 48 * @parameter default-value="${project}" 49 * @required 50 * @readonly 51 */ 52 private MavenProject project; 53 54 /** 55 * The {@link ArtifactHandlerManager} into which any extension {@link ArtifactHandler} instances should have been 56 * injected when the extensions were loaded. 57 * 58 * @component 59 */ 60 private ArtifactHandlerManager artifactHandlerManager; 61 62 /** 63 * @parameter default-value="${session}" 64 * @required 65 * @readonly 66 */ 67 private MavenSession session; 68 69 70 private Log log; 71 72 /** 73 * {@inheritDoc} 74 */ 75 @SuppressWarnings("deprecation") 76 public void execute() throws MojoExecutionException { 77 78 getLog().info("Injecting the Artifact Handlers anyway."); 79 // 80 // Map<String, ?> handlerDescriptors = session.getContainer().getComponentDescriptorMap(ArtifactHandler.ROLE); 81 // if (handlerDescriptors != null) { 82 // getLog().info("Registering all unregistered ArtifactHandlers..."); 83 // 84 // if (artifactHandlerManager instanceof DefaultArtifactHandlerManager) { 85 // Set<String> existingHints = ((DefaultArtifactHandlerManager) artifactHandlerManager).getHandlerTypes(); 86 // if (existingHints != null) { 87 // for (String hint : existingHints) { 88 // getLog().info("DEBUG - REMOVE : " + hint + " - ext: " + ((DefaultArtifactHandlerManager) artifactHandlerManager).getArtifactHandler(hint).getExtension()); 89 // 90 // handlerDescriptors.remove(hint); 91 // } 92 // } 93 // } 94 // 95 // if (handlerDescriptors.isEmpty()) { 96 // getLog().info("All ArtifactHandlers are registered. Continuing..."); 97 // } else { 98 // Map<String, ArtifactHandler> unregisteredHandlers = new HashMap<String, ArtifactHandler>(handlerDescriptors 99 // .size()); 100 // 101 // for (String hint : handlerDescriptors.keySet()) { 102 // try { 103 // unregisteredHandlers.put(hint, (ArtifactHandler) session.lookup(ArtifactHandler.ROLE, hint)); 104 // getLog().info("Adding ArtifactHandler for: " + hint); 105 // } catch (ComponentLookupException e) { 106 // getLog().warn("Failed to lookup ArtifactHandler with hint: " + hint + ". Reason: " + e.getMessage(), e); 107 // } 108 // } 109 // 110 // artifactHandlerManager.addHandlers(unregisteredHandlers); 111 // } 112 // } 113 114 getLog().info("Setting ArtifactHandler on project-artifact: " + project.getId() + "..."); 115 116 Set<Artifact> artifacts = new HashSet<Artifact>(); 117 artifacts.add(project.getArtifact()); 118 119 Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts(); 120 if (dependencyArtifacts != null && !dependencyArtifacts.isEmpty()) { 121 artifacts.addAll(dependencyArtifacts); 122 } 123 124 for (Artifact artifact : artifacts) { 125 String type = artifact.getType(); 126 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type); 127 128 getLog().debug( 129 "Artifact: " + artifact.getId() + "\nType: " + type + "\nArtifactHandler extension: " 130 + handler.getExtension()); 131 132 artifact.setArtifactHandler(handler); 133 } 134 135 getLog().info("...done."); 136 } 137 138 /** 139 * {@inheritDoc} 140 */ 141 public Log getLog() { 142 143 return log; 144 } 145 146 147 /** 148 * {@inheritDoc} 149 */ 150 public void setLog(final Log _log) { 151 152 this.log = _log; 153 } 154 155 }