Coverage Report - org.apache.maven.plugin.descriptor.PluginDescriptor
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginDescriptor
0%
0/87
0%
0/28
1.457
 
 1  
 package org.apache.maven.plugin.descriptor;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.artifact.ArtifactUtils;
 23  
 import org.apache.maven.plugin.lifecycle.Lifecycle;
 24  
 import org.apache.maven.plugin.lifecycle.LifecycleConfiguration;
 25  
 import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader;
 26  
 import org.codehaus.classworlds.ClassRealm;
 27  
 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
 28  
 import org.codehaus.plexus.util.IOUtil;
 29  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 30  
 
 31  
 import java.io.FileNotFoundException;
 32  
 import java.io.IOException;
 33  
 import java.io.InputStream;
 34  
 import java.io.InputStreamReader;
 35  
 import java.util.Collections;
 36  
 import java.util.HashMap;
 37  
 import java.util.Iterator;
 38  
 import java.util.List;
 39  
 import java.util.Map;
 40  
 import java.util.Set;
 41  
 
 42  
 /**
 43  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 44  
  * @version $Id: PluginDescriptor.java 609576 2008-01-07 11:45:20Z vsiveton $
 45  
  */
 46  0
 public class PluginDescriptor
 47  
     extends ComponentSetDescriptor
 48  
 {
 49  
     private String groupId;
 50  
 
 51  
     private String artifactId;
 52  
 
 53  
     private String version;
 54  
 
 55  
     private String goalPrefix;
 56  
 
 57  
     private String source;
 58  
 
 59  0
     private boolean inheritedByDefault = true;
 60  
 
 61  
     private List artifacts;
 62  
 
 63  
     private Map lifecycleMappings;
 64  
 
 65  
     private ClassRealm classRealm;
 66  
 
 67  
     // calculated on-demand.
 68  
     private Map artifactMap;
 69  
 
 70  
     private Set introducedDependencyArtifacts;
 71  
 
 72  
     private String name;
 73  
 
 74  
     private String description;
 75  
 
 76  
     // ----------------------------------------------------------------------
 77  
     //
 78  
     // ----------------------------------------------------------------------
 79  
 
 80  
     public List getMojos()
 81  
     {
 82  0
         return getComponents();
 83  
     }
 84  
 
 85  
     public void addMojo( MojoDescriptor mojoDescriptor )
 86  
         throws DuplicateMojoDescriptorException
 87  
     {
 88  0
         MojoDescriptor existing = null;
 89  
         // this relies heavily on the equals() and hashCode() for ComponentDescriptor,
 90  
         // which uses role:roleHint for identity...and roleHint == goalPrefix:goal.
 91  
         // role does not vary for Mojos.
 92  0
         List mojos = getComponents();
 93  
 
 94  0
         if ( mojos != null && mojos.contains( mojoDescriptor ) )
 95  
         {
 96  0
             int indexOf = mojos.indexOf( mojoDescriptor );
 97  
 
 98  0
             existing = (MojoDescriptor) mojos.get( indexOf );
 99  
         }
 100  
 
 101  0
         if ( existing != null )
 102  
         {
 103  0
             throw new DuplicateMojoDescriptorException( getGoalPrefix(), mojoDescriptor.getGoal(), existing
 104  
                 .getImplementation(), mojoDescriptor.getImplementation() );
 105  
         }
 106  
         else
 107  
         {
 108  0
             addComponentDescriptor( mojoDescriptor );
 109  
         }
 110  0
     }
 111  
 
 112  
     public String getGroupId()
 113  
     {
 114  0
         return groupId;
 115  
     }
 116  
 
 117  
     public void setGroupId( String groupId )
 118  
     {
 119  0
         this.groupId = groupId;
 120  0
     }
 121  
 
 122  
     public String getArtifactId()
 123  
     {
 124  0
         return artifactId;
 125  
     }
 126  
 
 127  
     public void setArtifactId( String artifactId )
 128  
     {
 129  0
         this.artifactId = artifactId;
 130  0
     }
 131  
 
 132  
     // ----------------------------------------------------------------------
 133  
     // Dependencies
 134  
     // ----------------------------------------------------------------------
 135  
 
 136  
     public static String constructPluginKey( String groupId, String artifactId, String version )
 137  
     {
 138  0
         return groupId + ":" + artifactId + ":" + version;
 139  
     }
 140  
 
 141  
     public String getPluginLookupKey()
 142  
     {
 143  0
         return groupId + ":" + artifactId;
 144  
     }
 145  
 
 146  
     public String getId()
 147  
     {
 148  0
         return constructPluginKey( groupId, artifactId, version );
 149  
     }
 150  
 
 151  
     public static String getDefaultPluginArtifactId( String id )
 152  
     {
 153  0
         return "maven-" + id + "-plugin";
 154  
     }
 155  
 
 156  
     public static String getDefaultPluginGroupId()
 157  
     {
 158  0
         return "org.apache.maven.plugins";
 159  
     }
 160  
 
 161  
     /**
 162  
      * Parse maven-...-plugin.
 163  
      *
 164  
      * @todo move to plugin-tools-api as a default only
 165  
      */
 166  
     public static String getGoalPrefixFromArtifactId( String artifactId )
 167  
     {
 168  0
         if ( "maven-plugin-plugin".equals( artifactId ) )
 169  
         {
 170  0
             return "plugin";
 171  
         }
 172  
         else
 173  
         {
 174  0
             return artifactId.replaceAll( "-?maven-?", "" ).replaceAll( "-?plugin-?", "" );
 175  
         }
 176  
     }
 177  
 
 178  
     public String getGoalPrefix()
 179  
     {
 180  0
         return goalPrefix;
 181  
     }
 182  
 
 183  
     public void setGoalPrefix( String goalPrefix )
 184  
     {
 185  0
         this.goalPrefix = goalPrefix;
 186  0
     }
 187  
 
 188  
     public void setVersion( String version )
 189  
     {
 190  0
         this.version = version;
 191  0
     }
 192  
 
 193  
     public String getVersion()
 194  
     {
 195  0
         return version;
 196  
     }
 197  
 
 198  
     public void setSource( String source )
 199  
     {
 200  0
         this.source = source;
 201  0
     }
 202  
 
 203  
     public String getSource()
 204  
     {
 205  0
         return source;
 206  
     }
 207  
 
 208  
     public boolean isInheritedByDefault()
 209  
     {
 210  0
         return inheritedByDefault;
 211  
     }
 212  
 
 213  
     public void setInheritedByDefault( boolean inheritedByDefault )
 214  
     {
 215  0
         this.inheritedByDefault = inheritedByDefault;
 216  0
     }
 217  
 
 218  
     public List getArtifacts()
 219  
     {
 220  0
         return artifacts;
 221  
     }
 222  
 
 223  
     public void setArtifacts( List artifacts )
 224  
     {
 225  0
         this.artifacts = artifacts;
 226  
 
 227  
         // clear the calculated artifactMap
 228  0
         artifactMap = null;
 229  0
     }
 230  
 
 231  
     public Map getArtifactMap()
 232  
     {
 233  0
         if ( artifactMap == null )
 234  
         {
 235  0
             artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() );
 236  
         }
 237  
 
 238  0
         return artifactMap;
 239  
     }
 240  
 
 241  
     public boolean equals( Object object )
 242  
     {
 243  0
         if ( this == object )
 244  
         {
 245  0
             return true;
 246  
         }
 247  
 
 248  0
         return getId().equals( ( (PluginDescriptor) object ).getId() );
 249  
     }
 250  
 
 251  
     public int hashCode()
 252  
     {
 253  0
         return 10 + getId().hashCode();
 254  
     }
 255  
 
 256  
     public MojoDescriptor getMojo( String goal )
 257  
     {
 258  0
         if ( getMojos() == null )
 259  
         {
 260  0
             return null; // no mojo in this POM
 261  
         }
 262  
 
 263  
         // TODO: could we use a map? Maybe if the parent did that for components too, as this is too vulnerable to
 264  
         // changes above not being propogated to the map
 265  
 
 266  0
         MojoDescriptor mojoDescriptor = null;
 267  0
         for ( Iterator i = getMojos().iterator(); i.hasNext() && mojoDescriptor == null; )
 268  
         {
 269  0
             MojoDescriptor desc = (MojoDescriptor) i.next();
 270  0
             if ( goal.equals( desc.getGoal() ) )
 271  
             {
 272  0
                 mojoDescriptor = desc;
 273  
             }
 274  0
         }
 275  0
         return mojoDescriptor;
 276  
     }
 277  
 
 278  
     public Lifecycle getLifecycleMapping( String lifecycle )
 279  
         throws IOException, XmlPullParserException
 280  
     {
 281  0
         if ( lifecycleMappings == null )
 282  
         {
 283  0
             LifecycleMappingsXpp3Reader reader = new LifecycleMappingsXpp3Reader();
 284  0
             InputStreamReader r = null;
 285  
             LifecycleConfiguration config;
 286  
 
 287  
             try
 288  
             {
 289  0
                 InputStream resourceAsStream = classRealm.getResourceAsStream( "/META-INF/maven/lifecycle.xml" );
 290  0
                 if ( resourceAsStream == null )
 291  
                 {
 292  0
                     throw new FileNotFoundException( "Unable to find /META-INF/maven/lifecycle.xml in the plugin" );
 293  
                 }
 294  0
                 r = new InputStreamReader( resourceAsStream );
 295  0
                 config = reader.read( r, true );
 296  
             }
 297  
             finally
 298  
             {
 299  0
                 IOUtil.close( r );
 300  0
             }
 301  
 
 302  0
             Map map = new HashMap();
 303  
 
 304  0
             for ( Iterator i = config.getLifecycles().iterator(); i.hasNext(); )
 305  
             {
 306  0
                 Lifecycle l = (Lifecycle) i.next();
 307  0
                 map.put( l.getId(), l );
 308  0
             }
 309  
 
 310  0
             lifecycleMappings = map;
 311  
         }
 312  0
         return (Lifecycle) lifecycleMappings.get( lifecycle );
 313  
     }
 314  
 
 315  
     public void setClassRealm( ClassRealm classRealm )
 316  
     {
 317  0
         this.classRealm = classRealm;
 318  0
     }
 319  
 
 320  
     public ClassRealm getClassRealm()
 321  
     {
 322  0
         return classRealm;
 323  
     }
 324  
 
 325  
     public void setIntroducedDependencyArtifacts( Set introducedDependencyArtifacts )
 326  
     {
 327  0
         this.introducedDependencyArtifacts = introducedDependencyArtifacts;
 328  0
     }
 329  
 
 330  
     public Set getIntroducedDependencyArtifacts()
 331  
     {
 332  0
         return introducedDependencyArtifacts != null ? introducedDependencyArtifacts : Collections.EMPTY_SET;
 333  
     }
 334  
 
 335  
     public void setName( String name )
 336  
     {
 337  0
         this.name = name;
 338  0
     }
 339  
 
 340  
     public String getName()
 341  
     {
 342  0
         return name;
 343  
     }
 344  
 
 345  
     public void setDescription( String description )
 346  
     {
 347  0
         this.description = description;
 348  0
     }
 349  
 
 350  
     public String getDescription()
 351  
     {
 352  0
         return description;
 353  
     }
 354  
 }