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.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
48
49
50
51
52 @Mojo( name = "updateRegistry", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
53 public class UpdatePluginRegistryMojo
54 extends AbstractMojo
55 {
56
57
58
59
60 @Parameter( defaultValue = "${settings.usePluginRegistry}", required = true, readonly = true )
61 private boolean usePluginRegistry;
62
63
64
65
66 @Parameter( defaultValue = "${project.groupId}", required = true, readonly = true )
67 private String groupId;
68
69
70
71
72 @Parameter( defaultValue = "${project.artifactId}", required = true, readonly = true )
73 private String artifactId;
74
75
76
77
78 @Parameter( defaultValue = "${project.artifact.version}", required = true, readonly = true )
79 private String version;
80
81
82
83
84 @Component
85 private MavenPluginRegistryBuilder pluginRegistryBuilder;
86
87
88
89
90
91
92 @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
93 private boolean skip;
94
95
96
97
98
99
100 @Parameter( defaultValue = "false", property = "maven.plugin.update.registry.skip" )
101 private boolean skipUpdatePluginRegistry;
102
103
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
120
121
122
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
145 if ( plugin != null )
146 {
147 if ( TrackableBase.GLOBAL_LEVEL.equals( plugin.getSourceLevel() ) )
148 {
149
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
181
182
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
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
218
219
220
221
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 }