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 | 0 | 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 | 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 | |
|
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 | 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 | |
|
155 | 0 | if ( plugin != null ) |
156 | |
{ |
157 | 0 | if ( TrackableBase.GLOBAL_LEVEL.equals( plugin.getSourceLevel() ) ) |
158 | |
{ |
159 | |
|
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 | |
|
191 | |
|
192 | |
|
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 | |
|
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 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
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 | |
} |