1 package org.apache.maven.plugin.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
44
45
46
47
48
49
50 public class UpdatePluginRegistryMojo
51 extends AbstractMojo
52 {
53
54
55
56
57
58
59
60
61 private boolean usePluginRegistry;
62
63
64
65
66
67
68
69
70 private String groupId;
71
72
73
74
75
76
77
78
79 private String artifactId;
80
81
82
83
84
85
86
87
88 private String version;
89
90
91
92
93
94
95 private MavenPluginRegistryBuilder pluginRegistryBuilder;
96
97
98
99
100
101
102
103 private boolean skip;
104
105
106
107
108
109
110
111 private boolean skipUpdatePluginRegistry;
112
113
114 public void execute()
115 throws MojoExecutionException, MojoFailureException
116 {
117 if ( usePluginRegistry )
118 {
119 if ( skip || skipUpdatePluginRegistry )
120 {
121 getLog().warn( "Execution skipped" );
122 return;
123 }
124 updatePluginVersionInRegistry( groupId, artifactId, version );
125 }
126 }
127
128
129
130
131
132
133
134 private void updatePluginVersionInRegistry( String aGroupId, String anArtifactId, String aVersion )
135 throws MojoExecutionException
136 {
137 PluginRegistry pluginRegistry;
138 try
139 {
140 pluginRegistry = getPluginRegistry( aGroupId, anArtifactId );
141 }
142 catch ( IOException e )
143 {
144 throw new MojoExecutionException( "Failed to read plugin registry.", e );
145 }
146 catch ( XmlPullParserException e )
147 {
148 throw new MojoExecutionException( "Failed to parse plugin registry.", e );
149 }
150
151 String pluginKey = ArtifactUtils.versionlessKey( aGroupId, anArtifactId );
152 Plugin plugin = (Plugin) pluginRegistry.getPluginsByKey().get( pluginKey );
153
154
155 if ( plugin != null )
156 {
157 if ( TrackableBase.GLOBAL_LEVEL.equals( plugin.getSourceLevel() ) )
158 {
159
160 getLog().warn( "Cannot update registered version for plugin {" + aGroupId + ":" + anArtifactId
161 + "}; it is specified in the global registry." );
162 }
163 else
164 {
165 plugin.setUseVersion( aVersion );
166
167 SimpleDateFormat format =
168 new SimpleDateFormat( org.apache.maven.plugin.registry.Plugin.LAST_CHECKED_DATE_FORMAT );
169
170 plugin.setLastChecked( format.format( new Date() ) );
171 }
172 }
173 else
174 {
175 plugin = new org.apache.maven.plugin.registry.Plugin();
176
177 plugin.setGroupId( aGroupId );
178 plugin.setArtifactId( anArtifactId );
179 plugin.setUseVersion( aVersion );
180
181 pluginRegistry.addPlugin( plugin );
182
183 pluginRegistry.flushPluginsByKey();
184 }
185
186 writeUserRegistry( aGroupId, anArtifactId, pluginRegistry );
187 }
188
189
190
191
192
193
194 private void writeUserRegistry( String aGroupId, String anArtifactId, PluginRegistry pluginRegistry )
195 {
196 File pluginRegistryFile = pluginRegistry.getRuntimeInfo().getFile();
197
198 PluginRegistry extractedUserRegistry = PluginRegistryUtils.extractUserPluginRegistry( pluginRegistry );
199
200
201 if ( extractedUserRegistry != null )
202 {
203 Writer fWriter = null;
204
205 try
206 {
207 pluginRegistryFile.getParentFile().mkdirs();
208 fWriter = WriterFactory.newXmlWriter( pluginRegistryFile );
209
210 PluginRegistryXpp3Writer writer = new PluginRegistryXpp3Writer();
211
212 writer.write( fWriter, extractedUserRegistry );
213 }
214 catch ( IOException e )
215 {
216 getLog().warn( "Cannot rewrite user-level plugin-registry.xml with new plugin version of plugin: \'"
217 + aGroupId + ":" + anArtifactId + "\'.", e );
218 }
219 finally
220 {
221 IOUtil.close( fWriter );
222 }
223 }
224 }
225
226
227
228
229
230
231
232
233 private PluginRegistry getPluginRegistry( String aGroupId, String anArtifactId )
234 throws IOException, XmlPullParserException
235 {
236 PluginRegistry pluginRegistry = null;
237
238 pluginRegistry = pluginRegistryBuilder.buildPluginRegistry();
239
240 if ( pluginRegistry == null )
241 {
242 pluginRegistry = pluginRegistryBuilder.createUserPluginRegistry();
243 }
244
245 return pluginRegistry;
246 }
247 }