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 | 0 | 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 | 0 | if ( usePluginRegistry ) |
108 | |
{ |
109 | 0 | if ( skip || skipUpdatePluginRegistry ) |
110 | |
{ |
111 | 0 | getLog().warn( "Execution skipped" ); |
112 | 0 | return; |
113 | |
} |
114 | 0 | updatePluginVersionInRegistry( groupId, artifactId, version ); |
115 | |
} |
116 | 0 | } |
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 | 0 | pluginRegistry = getPluginRegistry( aGroupId, anArtifactId ); |
131 | |
} |
132 | 0 | catch ( IOException e ) |
133 | |
{ |
134 | 0 | throw new MojoExecutionException( "Failed to read plugin registry.", e ); |
135 | |
} |
136 | 0 | catch ( XmlPullParserException e ) |
137 | |
{ |
138 | 0 | throw new MojoExecutionException( "Failed to parse plugin registry.", e ); |
139 | 0 | } |
140 | |
|
141 | 0 | String pluginKey = ArtifactUtils.versionlessKey( aGroupId, anArtifactId ); |
142 | 0 | Plugin plugin = (Plugin) pluginRegistry.getPluginsByKey().get( pluginKey ); |
143 | |
|
144 | |
|
145 | 0 | if ( plugin != null ) |
146 | |
{ |
147 | 0 | if ( TrackableBase.GLOBAL_LEVEL.equals( plugin.getSourceLevel() ) ) |
148 | |
{ |
149 | |
|
150 | 0 | getLog().warn( "Cannot update registered version for plugin {" + aGroupId + ":" + anArtifactId |
151 | |
+ "}; it is specified in the global registry." ); |
152 | |
} |
153 | |
else |
154 | |
{ |
155 | 0 | plugin.setUseVersion( aVersion ); |
156 | |
|
157 | 0 | SimpleDateFormat format = |
158 | |
new SimpleDateFormat( org.apache.maven.plugin.registry.Plugin.LAST_CHECKED_DATE_FORMAT ); |
159 | |
|
160 | 0 | plugin.setLastChecked( format.format( new Date() ) ); |
161 | 0 | } |
162 | |
} |
163 | |
else |
164 | |
{ |
165 | 0 | plugin = new org.apache.maven.plugin.registry.Plugin(); |
166 | |
|
167 | 0 | plugin.setGroupId( aGroupId ); |
168 | 0 | plugin.setArtifactId( anArtifactId ); |
169 | 0 | plugin.setUseVersion( aVersion ); |
170 | |
|
171 | 0 | pluginRegistry.addPlugin( plugin ); |
172 | |
|
173 | 0 | pluginRegistry.flushPluginsByKey(); |
174 | |
} |
175 | |
|
176 | 0 | writeUserRegistry( aGroupId, anArtifactId, pluginRegistry ); |
177 | 0 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
private void writeUserRegistry( String aGroupId, String anArtifactId, PluginRegistry pluginRegistry ) |
185 | |
{ |
186 | 0 | File pluginRegistryFile = pluginRegistry.getRuntimeInfo().getFile(); |
187 | |
|
188 | 0 | PluginRegistry extractedUserRegistry = PluginRegistryUtils.extractUserPluginRegistry( pluginRegistry ); |
189 | |
|
190 | |
|
191 | 0 | if ( extractedUserRegistry != null ) |
192 | |
{ |
193 | 0 | Writer fWriter = null; |
194 | |
|
195 | |
try |
196 | |
{ |
197 | 0 | pluginRegistryFile.getParentFile().mkdirs(); |
198 | 0 | fWriter = WriterFactory.newXmlWriter( pluginRegistryFile ); |
199 | |
|
200 | 0 | PluginRegistryXpp3Writer writer = new PluginRegistryXpp3Writer(); |
201 | |
|
202 | 0 | writer.write( fWriter, extractedUserRegistry ); |
203 | |
} |
204 | 0 | catch ( IOException e ) |
205 | |
{ |
206 | 0 | getLog().warn( "Cannot rewrite user-level plugin-registry.xml with new plugin version of plugin: \'" |
207 | |
+ aGroupId + ":" + anArtifactId + "\'.", e ); |
208 | |
} |
209 | |
finally |
210 | |
{ |
211 | 0 | IOUtil.close( fWriter ); |
212 | 0 | } |
213 | |
} |
214 | 0 | } |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
private PluginRegistry getPluginRegistry( String aGroupId, String anArtifactId ) |
224 | |
throws IOException, XmlPullParserException |
225 | |
{ |
226 | 0 | PluginRegistry pluginRegistry = null; |
227 | |
|
228 | 0 | pluginRegistry = pluginRegistryBuilder.buildPluginRegistry(); |
229 | |
|
230 | 0 | if ( pluginRegistry == null ) |
231 | |
{ |
232 | 0 | pluginRegistry = pluginRegistryBuilder.createUserPluginRegistry(); |
233 | |
} |
234 | |
|
235 | 0 | return pluginRegistry; |
236 | |
} |
237 | |
} |