1 | |
package org.apache.maven.plugins.scmpublish; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.io.FileUtils; |
23 | |
import org.apache.commons.io.FilenameUtils; |
24 | |
import org.apache.commons.io.IOUtils; |
25 | |
import org.apache.maven.plugin.MojoExecutionException; |
26 | |
import org.apache.maven.plugin.MojoFailureException; |
27 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
28 | |
import org.apache.maven.plugins.annotations.Mojo; |
29 | |
import org.apache.maven.plugins.annotations.Parameter; |
30 | |
import org.apache.maven.scm.CommandParameter; |
31 | |
import org.apache.maven.scm.CommandParameters; |
32 | |
import org.apache.maven.scm.ScmBranch; |
33 | |
import org.apache.maven.scm.ScmException; |
34 | |
import org.apache.maven.scm.ScmFileSet; |
35 | |
import org.apache.maven.scm.command.add.AddScmResult; |
36 | |
import org.apache.maven.scm.command.checkin.CheckInScmResult; |
37 | |
import org.apache.maven.scm.command.remove.RemoveScmResult; |
38 | |
|
39 | |
import java.io.BufferedReader; |
40 | |
import java.io.File; |
41 | |
import java.io.FileInputStream; |
42 | |
import java.io.FileOutputStream; |
43 | |
import java.io.IOException; |
44 | |
import java.io.InputStreamReader; |
45 | |
import java.io.OutputStreamWriter; |
46 | |
import java.io.PrintWriter; |
47 | |
import java.util.ArrayList; |
48 | |
import java.util.Arrays; |
49 | |
import java.util.Collection; |
50 | |
import java.util.HashSet; |
51 | |
import java.util.List; |
52 | |
import java.util.Set; |
53 | |
import java.util.TreeSet; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Mojo ( name = "publish", defaultPhase = LifecyclePhase.POST_SITE, aggregator = true ) |
61 | 0 | public class ScmPublishPublishMojo |
62 | |
extends AbstractScmPublishMojo |
63 | |
{ |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Parameter ( property = "scmpublish.dryRun" ) |
69 | |
private boolean dryRun; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Parameter ( property = "scmpublish.skipCheckin" ) |
75 | |
private boolean skipCheckin; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
@Parameter ( property = "scmpublish.checkinComment", defaultValue = "Site checkin for project ${project.name}" ) |
81 | |
private String checkinComment; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 0 | private final static String[] NORMALIZE_EXTENSIONS = { "html", "css", "js" }; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
@Parameter |
92 | |
protected String[] extraNormalizeExtensions; |
93 | |
|
94 | |
private File relativize( File base, File file ) |
95 | |
{ |
96 | 0 | return new File( base.toURI().relativize( file.toURI() ).getPath() ); |
97 | |
} |
98 | |
|
99 | |
protected boolean requireNormalizeNewlines( File f ) |
100 | |
throws IOException |
101 | |
{ |
102 | 0 | List<String> extensions = Arrays.asList( NORMALIZE_EXTENSIONS ); |
103 | 0 | if ( extraNormalizeExtensions != null ) |
104 | |
{ |
105 | 0 | extensions.addAll( Arrays.asList( extraNormalizeExtensions ) ); |
106 | |
} |
107 | |
|
108 | 0 | return FilenameUtils.isExtension( f.getName(), extensions ); |
109 | |
} |
110 | |
|
111 | |
private void normalizeNewlines( File f ) |
112 | |
throws IOException |
113 | |
{ |
114 | 0 | File tmpFile = null; |
115 | 0 | BufferedReader in = null; |
116 | 0 | PrintWriter out = null; |
117 | |
try |
118 | |
{ |
119 | 0 | tmpFile = File.createTempFile( "maven-site-scm-plugin-", ".tmp" ); |
120 | 0 | FileUtils.copyFile( f, tmpFile ); |
121 | 0 | in = new BufferedReader( new InputStreamReader( new FileInputStream( tmpFile ), siteOutputEncoding ) ); |
122 | 0 | out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), siteOutputEncoding ) ); |
123 | |
String line; |
124 | 0 | while ( ( line = in.readLine() ) != null ) |
125 | |
{ |
126 | 0 | if ( in.ready() ) |
127 | |
{ |
128 | 0 | out.println( line ); |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | out.print( line ); |
133 | |
} |
134 | |
} |
135 | |
} |
136 | |
finally |
137 | |
{ |
138 | 0 | IOUtils.closeQuietly( out ); |
139 | 0 | IOUtils.closeQuietly( in ); |
140 | 0 | FileUtils.deleteQuietly( tmpFile ); |
141 | 0 | } |
142 | 0 | } |
143 | |
|
144 | |
private void normalizeNewLines( Set<File> files ) |
145 | |
throws MojoFailureException |
146 | |
{ |
147 | 0 | for ( File f : files ) |
148 | |
{ |
149 | |
try |
150 | |
{ |
151 | 0 | if ( requireNormalizeNewlines( f ) ) |
152 | |
{ |
153 | 0 | normalizeNewlines( f ); |
154 | |
} |
155 | |
} |
156 | 0 | catch ( IOException e ) |
157 | |
{ |
158 | 0 | throw new MojoFailureException( "Failed to normalize newlines in " + f.getAbsolutePath(), e ); |
159 | 0 | } |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | |
public void scmPublishExecute() |
164 | |
throws MojoExecutionException, MojoFailureException |
165 | |
{ |
166 | 0 | if ( siteOutputEncoding == null ) |
167 | |
{ |
168 | 0 | getLog().warn( "No output encoding, defaulting to UTF-8." ); |
169 | 0 | siteOutputEncoding = "utf-8"; |
170 | |
} |
171 | |
|
172 | |
|
173 | 0 | List<File> inventory = ScmPublishInventory.readInventory( inventoryFile ); |
174 | |
|
175 | |
|
176 | 0 | Collection<File> newInventory = |
177 | |
ScmPublishInventory.listInventoryFiles( checkoutDirectory, scmProvider.getScmSpecificFilename() ); |
178 | |
|
179 | 0 | Set<File> deleted = new HashSet<File>( inventory ); |
180 | 0 | deleted.removeAll( newInventory ); |
181 | |
|
182 | 0 | Set<File> added = new HashSet<File>( newInventory ); |
183 | 0 | added.removeAll( inventory ); |
184 | |
|
185 | 0 | Set<File> updated = new HashSet<File>( newInventory ); |
186 | 0 | updated.retainAll( inventory ); |
187 | |
|
188 | 0 | logInfo( "Publish files: %d addition(s), %d update(s), %d delete(s)", added.size(), updated.size(), |
189 | |
deleted.size() ); |
190 | |
|
191 | 0 | if ( isDryRun() ) |
192 | |
{ |
193 | 0 | for ( File addedFile : added ) |
194 | |
{ |
195 | 0 | logInfo( "Added %s", addedFile.getAbsolutePath() ); |
196 | |
} |
197 | 0 | for ( File deletedFile : deleted ) |
198 | |
{ |
199 | 0 | logInfo( "Deleted %s", deletedFile.getAbsolutePath() ); |
200 | |
} |
201 | 0 | for ( File updatedFile : updated ) |
202 | |
{ |
203 | 0 | logInfo( "Updated %s", updatedFile.getAbsolutePath() ); |
204 | |
} |
205 | 0 | return; |
206 | |
} |
207 | |
|
208 | 0 | if ( !added.isEmpty() ) |
209 | |
{ |
210 | 0 | normalizeNewLines( added ); |
211 | |
|
212 | 0 | addFiles( added ); |
213 | |
} |
214 | |
|
215 | 0 | if ( !deleted.isEmpty() ) |
216 | |
{ |
217 | 0 | deleteFiles( deleted ); |
218 | |
} |
219 | |
|
220 | 0 | normalizeNewLines( updated ); |
221 | |
|
222 | 0 | checkinFiles(); |
223 | 0 | } |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
protected void checkinFiles() |
231 | |
throws MojoExecutionException |
232 | |
{ |
233 | 0 | if ( skipCheckin ) |
234 | |
{ |
235 | 0 | return; |
236 | |
} |
237 | |
|
238 | 0 | ScmFileSet updatedFileSet = new ScmFileSet( checkoutDirectory ); |
239 | |
try |
240 | |
{ |
241 | 0 | logInfo( "Checkin to the scm" ); |
242 | |
|
243 | 0 | CheckInScmResult checkinResult = |
244 | |
scmProvider.checkIn( scmRepository, updatedFileSet, new ScmBranch( scmBranch ), checkinComment ); |
245 | 0 | if ( !checkinResult.isSuccess() ) |
246 | |
{ |
247 | 0 | logError( "checkin operation failed: %s", |
248 | |
checkinResult.getProviderMessage() + " " + checkinResult.getCommandOutput() ); |
249 | 0 | throw new MojoExecutionException( "Failed to checkin files: " + checkinResult.getProviderMessage() + " " |
250 | |
+ checkinResult.getCommandOutput() ); |
251 | |
} |
252 | 0 | logInfo( "Checkin %d file(s) to revision: %s", checkinResult.getCheckedInFiles().size(), |
253 | |
checkinResult.getScmRevision() ); |
254 | |
} |
255 | 0 | catch ( ScmException e ) |
256 | |
{ |
257 | 0 | throw new MojoExecutionException( "Failed to perform checkin SCM", e ); |
258 | 0 | } |
259 | 0 | } |
260 | |
|
261 | |
protected void deleteFiles( Collection<File> deleted ) |
262 | |
throws MojoExecutionException |
263 | |
{ |
264 | 0 | if ( skipDeletedFiles ) |
265 | |
{ |
266 | 0 | logInfo( "deleting files is skipped" ); |
267 | 0 | return; |
268 | |
} |
269 | 0 | List<File> deletedList = new ArrayList<File>(); |
270 | 0 | for ( File f : deleted ) |
271 | |
{ |
272 | 0 | deletedList.add( relativize( checkoutDirectory, f ) ); |
273 | |
} |
274 | 0 | ScmFileSet deletedFileSet = new ScmFileSet( checkoutDirectory, deletedList ); |
275 | |
try |
276 | |
{ |
277 | 0 | getLog().debug( "deleting files: " + deletedList ); |
278 | |
|
279 | 0 | RemoveScmResult deleteResult = |
280 | |
scmProvider.remove( scmRepository, deletedFileSet, "Deleting obsolete site files." ); |
281 | 0 | if ( !deleteResult.isSuccess() ) |
282 | |
{ |
283 | 0 | logError( "delete operation failed: %s", |
284 | |
deleteResult.getProviderMessage() + " " + deleteResult.getCommandOutput() ); |
285 | 0 | throw new MojoExecutionException( "Failed to delete files: " + deleteResult.getProviderMessage() + " " |
286 | |
+ deleteResult.getCommandOutput() ); |
287 | |
} |
288 | |
} |
289 | 0 | catch ( ScmException e ) |
290 | |
{ |
291 | 0 | throw new MojoExecutionException( "Failed to delete removed files to SCM", e ); |
292 | 0 | } |
293 | 0 | } |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
protected void addFiles( Collection<File> added ) |
303 | |
throws MojoFailureException, MojoExecutionException |
304 | |
{ |
305 | 0 | List<File> addedList = new ArrayList<File>(); |
306 | 0 | Set<File> createdDirs = new HashSet<File>(); |
307 | 0 | Set<File> dirsToAdd = new TreeSet<File>(); |
308 | |
|
309 | 0 | createdDirs.add( relativize( checkoutDirectory, checkoutDirectory ) ); |
310 | |
|
311 | 0 | for ( File f : added ) |
312 | |
{ |
313 | 0 | for ( File dir = f.getParentFile(); !dir.equals( checkoutDirectory ); dir = dir.getParentFile() ) |
314 | |
{ |
315 | 0 | File relativized = relativize( checkoutDirectory, dir ); |
316 | |
|
317 | 0 | if ( createdDirs.add( relativized ) ) |
318 | |
{ |
319 | 0 | dirsToAdd.add( relativized ); |
320 | |
} |
321 | |
else |
322 | |
{ |
323 | |
break; |
324 | |
} |
325 | |
} |
326 | 0 | addedList.add( relativize( checkoutDirectory, f ) ); |
327 | |
} |
328 | |
|
329 | 0 | for ( File relativized : dirsToAdd ) |
330 | |
{ |
331 | |
try |
332 | |
{ |
333 | 0 | ScmFileSet fileSet = new ScmFileSet( checkoutDirectory, relativized ); |
334 | 0 | getLog().debug( "scm add directory: " + relativized ); |
335 | 0 | AddScmResult addDirResult = scmProvider.add( scmRepository, fileSet, "Adding directory" ); |
336 | 0 | if ( !addDirResult.isSuccess() ) |
337 | |
{ |
338 | 0 | getLog().debug( " Error adding directory " + relativized + ": " + addDirResult.getCommandOutput() ); |
339 | |
} |
340 | |
} |
341 | 0 | catch ( ScmException e ) |
342 | |
{ |
343 | |
|
344 | 0 | } |
345 | |
} |
346 | |
|
347 | |
|
348 | 0 | addedList.removeAll( dirsToAdd ); |
349 | |
|
350 | 0 | ScmFileSet addedFileSet = new ScmFileSet( checkoutDirectory, addedList ); |
351 | 0 | getLog().debug( "scm add files: " + addedList ); |
352 | |
try |
353 | |
{ |
354 | 0 | CommandParameters commandParameters = new CommandParameters(); |
355 | 0 | commandParameters.setString( CommandParameter.MESSAGE, "Adding new site files." ); |
356 | 0 | commandParameters.setString( CommandParameter.FORCE_ADD, Boolean.TRUE.toString() ); |
357 | 0 | AddScmResult addResult = scmProvider.add( scmRepository, addedFileSet, commandParameters ); |
358 | 0 | if ( !addResult.isSuccess() ) |
359 | |
{ |
360 | 0 | logError( "add operation failed: %s", |
361 | |
addResult.getProviderMessage() + " " + addResult.getCommandOutput() ); |
362 | 0 | throw new MojoExecutionException( |
363 | |
"Failed to add new files: " + addResult.getProviderMessage() + " " + addResult.getCommandOutput() ); |
364 | |
} |
365 | |
} |
366 | 0 | catch ( ScmException e ) |
367 | |
{ |
368 | 0 | throw new MojoExecutionException( "Failed to add new files to SCM", e ); |
369 | 0 | } |
370 | 0 | } |
371 | |
|
372 | |
public boolean isDryRun() |
373 | |
{ |
374 | 0 | return dryRun; |
375 | |
} |
376 | |
} |