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