Coverage Report - org.apache.maven.plugin.plugin.UpdatePluginRegistryMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
UpdatePluginRegistryMojo
0%
0/48
0%
0/14
4.25
 
 1  
 package org.apache.maven.plugin.plugin;
 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.AbstractMojo;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.MojoFailureException;
 26  
 import org.apache.maven.plugin.registry.MavenPluginRegistryBuilder;
 27  
 import org.apache.maven.plugin.registry.Plugin;
 28  
 import org.apache.maven.plugin.registry.PluginRegistry;
 29  
 import org.apache.maven.plugin.registry.PluginRegistryUtils;
 30  
 import org.apache.maven.plugin.registry.TrackableBase;
 31  
 import org.apache.maven.plugin.registry.io.xpp3.PluginRegistryXpp3Writer;
 32  
 import org.apache.maven.plugins.annotations.Component;
 33  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 34  
 import org.apache.maven.plugins.annotations.Mojo;
 35  
 import org.apache.maven.plugins.annotations.Parameter;
 36  
 import org.codehaus.plexus.util.IOUtil;
 37  
 import org.codehaus.plexus.util.WriterFactory;
 38  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 39  
 
 40  
 import java.io.File;
 41  
 import java.io.IOException;
 42  
 import java.io.Writer;
 43  
 import java.text.SimpleDateFormat;
 44  
 import java.util.Date;
 45  
 
 46  
 /**
 47  
  * Update the user plugin registry (if it's in use) to reflect the version we're installing.
 48  
  *
 49  
  * @version $Id: UpdatePluginRegistryMojo.java 1345787 2012-06-03 21:58:22Z hboutemy $
 50  
  * @since 2.0
 51  
  */
 52  
 @Mojo( name = "updateRegistry", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
 53  0
 public class UpdatePluginRegistryMojo
 54  
     extends AbstractMojo
 55  
 {
 56  
     /**
 57  
      * Indicates whether the <code>plugin-registry.xml</code> file is used by Maven or not
 58  
      * to manage plugin versions.
 59  
      */
 60  
     @Parameter( defaultValue = "${settings.usePluginRegistry}", required = true, readonly = true )
 61  
     private boolean usePluginRegistry;
 62  
 
 63  
     /**
 64  
      * The group id of the project currently being built.
 65  
      */
 66  
     @Parameter( defaultValue = "${project.groupId}", required = true, readonly = true )
 67  
     private String groupId;
 68  
 
 69  
     /**
 70  
      * The artifact id of the project currently being built.
 71  
      */
 72  
     @Parameter( defaultValue = "${project.artifactId}", required = true, readonly = true )
 73  
     private String artifactId;
 74  
 
 75  
     /**
 76  
      * The version of the project currently being built.
 77  
      */
 78  
     @Parameter( defaultValue = "${project.artifact.version}", required = true, readonly = true )
 79  
     private String version;
 80  
 
 81  
     /**
 82  
      * Plexus component for retrieving the plugin registry info.
 83  
      */
 84  
     @Component
 85  
     private MavenPluginRegistryBuilder pluginRegistryBuilder;
 86  
 
 87  
     /**
 88  
      * Set this to "true" to skip invoking any goals or reports of the plugin.
 89  
      *
 90  
      * @since 2.8
 91  
      */
 92  
     @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
 93  
     private boolean skip;
 94  
 
 95  
     /**
 96  
      * Set this to "true" to skip updating the plugin registry.
 97  
      *
 98  
      * @since 2.8
 99  
      */
 100  
     @Parameter( defaultValue = "false", property = "maven.plugin.update.registry.skip" )
 101  
     private boolean skipUpdatePluginRegistry;
 102  
 
 103  
     /** {@inheritDoc} */
 104  
     public void execute()
 105  
         throws MojoExecutionException, MojoFailureException
 106  
     {
 107  0
         if ( usePluginRegistry )
 108  
         {
 109  0
             if ( skip || skipUpdatePluginRegistry )
 110  
             {
 111  0
                 getLog().warn( "Execution skipped" );
 112  0
                 return;
 113  
             }
 114  0
             updatePluginVersionInRegistry( groupId, artifactId, version );
 115  
         }
 116  0
     }
 117  
 
 118  
     /**
 119  
      * @param aGroupId not null
 120  
      * @param anArtifactId not null
 121  
      * @param aVersion not null
 122  
      * @throws MojoExecutionException if any
 123  
      */
 124  
     private void updatePluginVersionInRegistry( String aGroupId, String anArtifactId, String aVersion )
 125  
         throws MojoExecutionException
 126  
     {
 127  
         PluginRegistry pluginRegistry;
 128  
         try
 129  
         {
 130  0
             pluginRegistry = getPluginRegistry( aGroupId, anArtifactId );
 131  
         }
 132  0
         catch ( IOException e )
 133  
         {
 134  0
             throw new MojoExecutionException( "Failed to read plugin registry.", e );
 135  
         }
 136  0
         catch ( XmlPullParserException e )
 137  
         {
 138  0
             throw new MojoExecutionException( "Failed to parse plugin registry.", e );
 139  0
         }
 140  
 
 141  0
         String pluginKey = ArtifactUtils.versionlessKey( aGroupId, anArtifactId );
 142  0
         Plugin plugin = (Plugin) pluginRegistry.getPluginsByKey().get( pluginKey );
 143  
 
 144  
         // if we can find the plugin, but we've gotten here, the useVersion must be missing; fill it in.
 145  0
         if ( plugin != null )
 146  
         {
 147  0
             if ( TrackableBase.GLOBAL_LEVEL.equals( plugin.getSourceLevel() ) )
 148  
             {
 149  
                 // do nothing. We don't rewrite the globals, under any circumstances.
 150  0
                 getLog().warn( "Cannot update registered version for plugin {" + aGroupId + ":" + anArtifactId
 151  
                     + "}; it is specified in the global registry." );
 152  
             }
 153  
             else
 154  
             {
 155  0
                 plugin.setUseVersion( aVersion );
 156  
 
 157  0
                 SimpleDateFormat format =
 158  
                     new SimpleDateFormat( org.apache.maven.plugin.registry.Plugin.LAST_CHECKED_DATE_FORMAT );
 159  
 
 160  0
                 plugin.setLastChecked( format.format( new Date() ) );
 161  0
             }
 162  
         }
 163  
         else
 164  
         {
 165  0
             plugin = new org.apache.maven.plugin.registry.Plugin();
 166  
 
 167  0
             plugin.setGroupId( aGroupId );
 168  0
             plugin.setArtifactId( anArtifactId );
 169  0
             plugin.setUseVersion( aVersion );
 170  
 
 171  0
             pluginRegistry.addPlugin( plugin );
 172  
 
 173  0
             pluginRegistry.flushPluginsByKey();
 174  
         }
 175  
 
 176  0
         writeUserRegistry( aGroupId, anArtifactId, pluginRegistry );
 177  0
     }
 178  
 
 179  
     /**
 180  
      * @param aGroupId not null
 181  
      * @param anArtifactId not null
 182  
      * @param pluginRegistry not null
 183  
      */
 184  
     private void writeUserRegistry( String aGroupId, String anArtifactId, PluginRegistry pluginRegistry )
 185  
     {
 186  0
         File pluginRegistryFile = pluginRegistry.getRuntimeInfo().getFile();
 187  
 
 188  0
         PluginRegistry extractedUserRegistry = PluginRegistryUtils.extractUserPluginRegistry( pluginRegistry );
 189  
 
 190  
         // only rewrite the user-level registry if one existed before, or if we've created user-level data here.
 191  0
         if ( extractedUserRegistry != null )
 192  
         {
 193  0
             Writer fWriter = null;
 194  
 
 195  
             try
 196  
             {
 197  0
                 pluginRegistryFile.getParentFile().mkdirs();
 198  0
                 fWriter = WriterFactory.newXmlWriter( pluginRegistryFile );
 199  
 
 200  0
                 PluginRegistryXpp3Writer writer = new PluginRegistryXpp3Writer();
 201  
 
 202  0
                 writer.write( fWriter, extractedUserRegistry );
 203  
             }
 204  0
             catch ( IOException e )
 205  
             {
 206  0
                 getLog().warn( "Cannot rewrite user-level plugin-registry.xml with new plugin version of plugin: \'"
 207  
                     + aGroupId + ":" + anArtifactId + "\'.", e );
 208  
             }
 209  
             finally
 210  
             {
 211  0
                 IOUtil.close( fWriter );
 212  0
             }
 213  
         }
 214  0
     }
 215  
 
 216  
     /**
 217  
      * @param aGroupId not null
 218  
      * @param anArtifactId not null
 219  
      * @return the plugin registry instance
 220  
      * @throws IOException if any
 221  
      * @throws XmlPullParserException if any
 222  
      */
 223  
     private PluginRegistry getPluginRegistry( String aGroupId, String anArtifactId )
 224  
         throws IOException, XmlPullParserException
 225  
     {
 226  0
         PluginRegistry pluginRegistry = null;
 227  
 
 228  0
         pluginRegistry = pluginRegistryBuilder.buildPluginRegistry();
 229  
 
 230  0
         if ( pluginRegistry == null )
 231  
         {
 232  0
             pluginRegistry = pluginRegistryBuilder.createUserPluginRegistry();
 233  
         }
 234  
 
 235  0
         return pluginRegistry;
 236  
     }
 237  
 }