1 package org.apache.maven.plugin.descriptor; 2 3 18 19 import org.apache.maven.artifact.ArtifactUtils; 20 import org.apache.maven.plugin.lifecycle.Lifecycle; 21 import org.apache.maven.plugin.lifecycle.LifecycleConfiguration; 22 import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader; 23 import org.codehaus.classworlds.ClassRealm; 24 import org.codehaus.plexus.component.repository.ComponentSetDescriptor; 25 import org.codehaus.plexus.util.IOUtil; 26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 27 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 43 public class PluginDescriptor 44 extends ComponentSetDescriptor 45 { 46 private String groupId; 47 48 private String artifactId; 49 50 private String version; 51 52 private String goalPrefix; 53 54 private String source; 55 56 private boolean inheritedByDefault = true; 57 58 private List artifacts; 59 60 private Map lifecycleMappings; 61 62 private ClassRealm classRealm; 63 64 private Map artifactMap; 66 67 private Set introducedDependencyArtifacts; 68 69 private String name; 70 71 private String description; 72 73 77 public List getMojos() 78 { 79 return getComponents(); 80 } 81 82 public void addMojo( MojoDescriptor mojoDescriptor ) 83 throws DuplicateMojoDescriptorException 84 { 85 MojoDescriptor existing = null; 86 List mojos = getComponents(); 90 91 if ( mojos != null && mojos.contains( mojoDescriptor ) ) 92 { 93 int indexOf = mojos.indexOf( mojoDescriptor ); 94 95 existing = (MojoDescriptor) mojos.get( indexOf ); 96 } 97 98 if ( existing != null ) 99 { 100 throw new DuplicateMojoDescriptorException( getGoalPrefix(), mojoDescriptor.getGoal(), existing 101 .getImplementation(), mojoDescriptor.getImplementation() ); 102 } 103 else 104 { 105 addComponentDescriptor( mojoDescriptor ); 106 } 107 } 108 109 public String getGroupId() 110 { 111 return groupId; 112 } 113 114 public void setGroupId( String groupId ) 115 { 116 this.groupId = groupId; 117 } 118 119 public String getArtifactId() 120 { 121 return artifactId; 122 } 123 124 public void setArtifactId( String artifactId ) 125 { 126 this.artifactId = artifactId; 127 } 128 129 133 public static String constructPluginKey( String groupId, String artifactId, String version ) 134 { 135 return groupId + ":" + artifactId + ":" + version; 136 } 137 138 public String getPluginLookupKey() 139 { 140 return groupId + ":" + artifactId; 141 } 142 143 public String getId() 144 { 145 return constructPluginKey( groupId, artifactId, version ); 146 } 147 148 public static String getDefaultPluginArtifactId( String id ) 149 { 150 return "maven-" + id + "-plugin"; 151 } 152 153 public static String getDefaultPluginGroupId() 154 { 155 return "org.apache.maven.plugins"; 156 } 157 158 163 public static String getGoalPrefixFromArtifactId( String artifactId ) 164 { 165 if ( "maven-plugin-plugin".equals( artifactId ) ) 166 { 167 return "plugin"; 168 } 169 else 170 { 171 return artifactId.replaceAll( "-?maven-?", "" ).replaceAll( "-?plugin-?", "" ); 172 } 173 } 174 175 public String getGoalPrefix() 176 { 177 return goalPrefix; 178 } 179 180 public void setGoalPrefix( String goalPrefix ) 181 { 182 this.goalPrefix = goalPrefix; 183 } 184 185 public void setVersion( String version ) 186 { 187 this.version = version; 188 } 189 190 public String getVersion() 191 { 192 return version; 193 } 194 195 public void setSource( String source ) 196 { 197 this.source = source; 198 } 199 200 public String getSource() 201 { 202 return source; 203 } 204 205 public boolean isInheritedByDefault() 206 { 207 return inheritedByDefault; 208 } 209 210 public void setInheritedByDefault( boolean inheritedByDefault ) 211 { 212 this.inheritedByDefault = inheritedByDefault; 213 } 214 215 public List getArtifacts() 216 { 217 return artifacts; 218 } 219 220 public void setArtifacts( List artifacts ) 221 { 222 this.artifacts = artifacts; 223 224 artifactMap = null; 226 } 227 228 public Map getArtifactMap() 229 { 230 if ( artifactMap == null ) 231 { 232 artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() ); 233 } 234 235 return artifactMap; 236 } 237 238 public boolean equals( Object object ) 239 { 240 if ( this == object ) 241 { 242 return true; 243 } 244 245 return getId().equals( ( (PluginDescriptor) object ).getId() ); 246 } 247 248 public int hashCode() 249 { 250 return 10 + getId().hashCode(); 251 } 252 253 public MojoDescriptor getMojo( String goal ) 254 { 255 258 MojoDescriptor mojoDescriptor = null; 259 for ( Iterator i = getMojos().iterator(); i.hasNext() && mojoDescriptor == null; ) 260 { 261 MojoDescriptor desc = (MojoDescriptor) i.next(); 262 if ( goal.equals( desc.getGoal() ) ) 263 { 264 mojoDescriptor = desc; 265 } 266 } 267 return mojoDescriptor; 268 } 269 270 public Lifecycle getLifecycleMapping( String lifecycle ) 271 throws IOException , XmlPullParserException 272 { 273 if ( lifecycleMappings == null ) 274 { 275 LifecycleMappingsXpp3Reader reader = new LifecycleMappingsXpp3Reader(); 276 InputStreamReader r = null; 277 LifecycleConfiguration config; 278 279 try 280 { 281 InputStream resourceAsStream = classRealm.getResourceAsStream( "/META-INF/maven/lifecycle.xml" ); 282 if ( resourceAsStream == null ) 283 { 284 throw new FileNotFoundException ( "Unable to find /META-INF/maven/lifecycle.xml in the plugin" ); 285 } 286 r = new InputStreamReader ( resourceAsStream ); 287 config = reader.read( r, true ); 288 } 289 finally 290 { 291 IOUtil.close( r ); 292 } 293 294 Map map = new HashMap (); 295 296 for ( Iterator i = config.getLifecycles().iterator(); i.hasNext(); ) 297 { 298 Lifecycle l = (Lifecycle) i.next(); 299 map.put( l.getId(), l ); 300 } 301 302 lifecycleMappings = map; 303 } 304 return (Lifecycle) lifecycleMappings.get( lifecycle ); 305 } 306 307 public void setClassRealm( ClassRealm classRealm ) 308 { 309 this.classRealm = classRealm; 310 } 311 312 public ClassRealm getClassRealm() 313 { 314 return classRealm; 315 } 316 317 public void setIntroducedDependencyArtifacts( Set introducedDependencyArtifacts ) 318 { 319 this.introducedDependencyArtifacts = introducedDependencyArtifacts; 320 } 321 322 public Set getIntroducedDependencyArtifacts() 323 { 324 return introducedDependencyArtifacts != null ? introducedDependencyArtifacts : Collections.EMPTY_SET; 325 } 326 327 public void setName( String name ) 328 { 329 this.name = name; 330 } 331 332 public String getName() 333 { 334 return name; 335 } 336 337 public void setDescription( String description ) 338 { 339 this.description = description; 340 } 341 342 public String getDescription() 343 { 344 return description; 345 } 346 } 347 | Popular Tags |