View Javadoc
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   * @since 2.0
50   * @deprecated plugin registry has been removed from Maven 3, this goal will be removed in next release
51   */
52  @Mojo( name = "updateRegistry", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
53  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         if ( usePluginRegistry )
108         {
109             if ( skip || skipUpdatePluginRegistry )
110             {
111                 getLog().warn( "Execution skipped" );
112                 return;
113             }
114             updatePluginVersionInRegistry( groupId, artifactId, version );
115         }
116     }
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             pluginRegistry = getPluginRegistry( aGroupId, anArtifactId );
131         }
132         catch ( IOException e )
133         {
134             throw new MojoExecutionException( "Failed to read plugin registry.", e );
135         }
136         catch ( XmlPullParserException e )
137         {
138             throw new MojoExecutionException( "Failed to parse plugin registry.", e );
139         }
140 
141         String pluginKey = ArtifactUtils.versionlessKey( aGroupId, anArtifactId );
142         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         if ( plugin != null )
146         {
147             if ( TrackableBase.GLOBAL_LEVEL.equals( plugin.getSourceLevel() ) )
148             {
149                 // do nothing. We don't rewrite the globals, under any circumstances.
150                 getLog().warn( "Cannot update registered version for plugin {" + aGroupId + ":" + anArtifactId
151                     + "}; it is specified in the global registry." );
152             }
153             else
154             {
155                 plugin.setUseVersion( aVersion );
156 
157                 SimpleDateFormat format =
158                     new SimpleDateFormat( org.apache.maven.plugin.registry.Plugin.LAST_CHECKED_DATE_FORMAT );
159 
160                 plugin.setLastChecked( format.format( new Date() ) );
161             }
162         }
163         else
164         {
165             plugin = new org.apache.maven.plugin.registry.Plugin();
166 
167             plugin.setGroupId( aGroupId );
168             plugin.setArtifactId( anArtifactId );
169             plugin.setUseVersion( aVersion );
170 
171             pluginRegistry.addPlugin( plugin );
172 
173             pluginRegistry.flushPluginsByKey();
174         }
175 
176         writeUserRegistry( aGroupId, anArtifactId, pluginRegistry );
177     }
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         File pluginRegistryFile = pluginRegistry.getRuntimeInfo().getFile();
187 
188         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         if ( extractedUserRegistry != null )
192         {
193             Writer fWriter = null;
194 
195             try
196             {
197                 pluginRegistryFile.getParentFile().mkdirs();
198                 fWriter = WriterFactory.newXmlWriter( pluginRegistryFile );
199 
200                 PluginRegistryXpp3Writer writer = new PluginRegistryXpp3Writer();
201 
202                 writer.write( fWriter, extractedUserRegistry );
203             }
204             catch ( IOException e )
205             {
206                 getLog().warn( "Cannot rewrite user-level plugin-registry.xml with new plugin version of plugin: \'"
207                     + aGroupId + ":" + anArtifactId + "\'.", e );
208             }
209             finally
210             {
211                 IOUtil.close( fWriter );
212             }
213         }
214     }
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         PluginRegistry pluginRegistry = null;
227 
228         pluginRegistry = pluginRegistryBuilder.buildPluginRegistry();
229 
230         if ( pluginRegistry == null )
231         {
232             pluginRegistry = pluginRegistryBuilder.createUserPluginRegistry();
233         }
234 
235         return pluginRegistry;
236     }
237 }