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.IOUtils; |
24 | |
import org.apache.commons.io.filefilter.NameFileFilter; |
25 | |
import org.apache.commons.io.filefilter.NotFileFilter; |
26 | |
import org.apache.maven.plugin.MojoExecutionException; |
27 | |
import org.apache.maven.plugin.MojoFailureException; |
28 | |
import org.apache.maven.plugins.annotations.Component; |
29 | |
import org.apache.maven.plugins.annotations.Mojo; |
30 | |
import org.apache.maven.plugins.annotations.Parameter; |
31 | |
import org.apache.maven.project.MavenProject; |
32 | |
import org.codehaus.plexus.util.MatchPatterns; |
33 | |
|
34 | |
import java.io.BufferedReader; |
35 | |
import java.io.File; |
36 | |
import java.io.FileInputStream; |
37 | |
import java.io.FileOutputStream; |
38 | |
import java.io.IOException; |
39 | |
import java.io.InputStreamReader; |
40 | |
import java.io.OutputStreamWriter; |
41 | |
import java.io.PrintWriter; |
42 | |
import java.util.ArrayList; |
43 | |
import java.util.Arrays; |
44 | |
import java.util.Collections; |
45 | |
import java.util.HashSet; |
46 | |
import java.util.List; |
47 | |
import java.util.Set; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
@Mojo( name = "publish-scm", aggregator = true, requiresProject = false ) |
55 | 0 | public class ScmPublishPublishScmMojo |
56 | |
extends ScmPublishPublishMojo |
57 | |
{ |
58 | |
|
59 | |
|
60 | |
|
61 | |
@Parameter( property = "scmpublish.content", defaultValue = "${project.build.directory}/staging" ) |
62 | |
private File content; |
63 | |
|
64 | |
|
65 | |
|
66 | |
@Component |
67 | |
protected MavenProject project; |
68 | |
|
69 | 0 | private List<File> deleted = new ArrayList<File>(); |
70 | |
|
71 | 0 | private List<File> added = new ArrayList<File>(); |
72 | |
|
73 | 0 | private List<File> updated = new ArrayList<File>(); |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private void update( File checkout, File dir, List<String> doNotDeleteDirs ) |
85 | |
throws IOException |
86 | |
{ |
87 | 0 | String[] files = |
88 | |
checkout.list( new NotFileFilter( new NameFileFilter( scmProvider.getScmSpecificFilename() ) ) ); |
89 | |
|
90 | 0 | Set<String> checkoutContent = new HashSet<String>( Arrays.asList( files ) ); |
91 | 0 | List<String> dirContent = ( dir != null ) ? Arrays.asList( dir.list() ) : Collections.<String>emptyList(); |
92 | |
|
93 | 0 | Set<String> deleted = new HashSet<String>( checkoutContent ); |
94 | 0 | deleted.removeAll( dirContent ); |
95 | |
|
96 | 0 | MatchPatterns ignoreDeleteMatchPatterns = null; |
97 | 0 | List<String> pathsAsList = new ArrayList<String>( 0 ); |
98 | 0 | if ( ignorePathsToDelete != null && ignorePathsToDelete.length > 0 ) |
99 | |
{ |
100 | 0 | ignoreDeleteMatchPatterns = MatchPatterns.from( ignorePathsToDelete ); |
101 | 0 | pathsAsList = Arrays.asList( ignorePathsToDelete ); |
102 | |
} |
103 | |
|
104 | 0 | for ( String name : deleted ) |
105 | |
{ |
106 | 0 | if ( ignoreDeleteMatchPatterns != null && ignoreDeleteMatchPatterns.matches( name, true ) ) |
107 | |
{ |
108 | 0 | getLog().debug( name + " match one of the patterns '" + pathsAsList + "': do not add to deleted files" ); |
109 | 0 | continue; |
110 | |
} |
111 | 0 | File file = new File( checkout, name ); |
112 | |
|
113 | 0 | if ( ( doNotDeleteDirs != null ) && file.isDirectory() && ( doNotDeleteDirs.contains( name ) ) ) |
114 | |
{ |
115 | |
|
116 | 0 | continue; |
117 | |
} |
118 | |
|
119 | 0 | if ( file.isDirectory() ) |
120 | |
{ |
121 | 0 | update( file, null, null ); |
122 | |
} |
123 | 0 | this.deleted.add( file ); |
124 | 0 | } |
125 | |
|
126 | 0 | for ( String name : dirContent ) |
127 | |
{ |
128 | 0 | File file = new File( checkout, name ); |
129 | 0 | File source = new File( dir, name ); |
130 | |
|
131 | 0 | if ( source.isDirectory() ) |
132 | |
{ |
133 | 0 | if ( !checkoutContent.contains( name ) ) |
134 | |
{ |
135 | 0 | this.added.add( file ); |
136 | 0 | file.mkdir(); |
137 | |
} |
138 | |
|
139 | 0 | update( file, source, null ); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | if ( checkoutContent.contains( name ) ) |
144 | |
{ |
145 | 0 | this.updated.add( file ); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | this.added.add( file ); |
150 | |
} |
151 | |
|
152 | 0 | copyFile( source, file ); |
153 | |
} |
154 | 0 | } |
155 | 0 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private void copyFile( File srcFile, File destFile ) |
166 | |
throws IOException |
167 | |
{ |
168 | 0 | if ( requireNormalizeNewlines( srcFile ) ) |
169 | |
{ |
170 | 0 | copyAndNormalizeNewlines( srcFile, destFile ); |
171 | |
} |
172 | |
else |
173 | |
{ |
174 | 0 | FileUtils.copyFile( srcFile, destFile ); |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
private void copyAndNormalizeNewlines( File srcFile, File destFile ) |
186 | |
throws IOException |
187 | |
{ |
188 | 0 | BufferedReader in = null; |
189 | 0 | PrintWriter out = null; |
190 | |
try |
191 | |
{ |
192 | 0 | in = new BufferedReader( new InputStreamReader( new FileInputStream( srcFile ), siteOutputEncoding ) ); |
193 | 0 | out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( destFile ), siteOutputEncoding ) ); |
194 | |
String line; |
195 | 0 | while ( ( line = in.readLine() ) != null ) |
196 | |
{ |
197 | 0 | if ( in.ready() ) |
198 | |
{ |
199 | 0 | out.println( line ); |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 0 | out.print( line ); |
204 | |
} |
205 | |
} |
206 | |
} |
207 | |
finally |
208 | |
{ |
209 | 0 | IOUtils.closeQuietly( out ); |
210 | 0 | IOUtils.closeQuietly( in ); |
211 | 0 | } |
212 | 0 | } |
213 | |
|
214 | |
public void scmPublishExecute() |
215 | |
throws MojoExecutionException, MojoFailureException |
216 | |
{ |
217 | 0 | if ( siteOutputEncoding == null ) |
218 | |
{ |
219 | 0 | getLog().warn( "No output encoding, defaulting to UTF-8." ); |
220 | 0 | siteOutputEncoding = "utf-8"; |
221 | |
} |
222 | |
|
223 | 0 | if ( !content.exists() ) |
224 | |
{ |
225 | 0 | throw new MojoExecutionException( "Configured content directory does not exist: " + content ); |
226 | |
} |
227 | |
|
228 | 0 | if ( !content.canRead() ) |
229 | |
{ |
230 | 0 | throw new MojoExecutionException( "Can't read content directory: " + content ); |
231 | |
} |
232 | |
|
233 | 0 | checkoutExisting(); |
234 | |
|
235 | |
try |
236 | |
{ |
237 | 0 | logInfo( "Updating content..." ); |
238 | 0 | update( checkoutDirectory, content, ( project == null ) ? null : project.getModel().getModules() ); |
239 | |
} |
240 | 0 | catch ( IOException ioe ) |
241 | |
{ |
242 | 0 | throw new MojoExecutionException( "Could not copy content to scm checkout", ioe ); |
243 | 0 | } |
244 | |
|
245 | 0 | logInfo( "Publish files: %d addition(s), %d update(s), %d delete(s)", added.size(), updated.size(), |
246 | |
deleted.size() ); |
247 | |
|
248 | 0 | if ( isDryRun() ) |
249 | |
{ |
250 | 0 | for ( File addedFile : added ) |
251 | |
{ |
252 | 0 | logInfo( "Added %s", addedFile.getAbsolutePath() ); |
253 | |
} |
254 | 0 | for ( File deletedFile : deleted ) |
255 | |
{ |
256 | 0 | logInfo( "Deleted %s", deletedFile.getAbsolutePath() ); |
257 | |
} |
258 | 0 | for ( File updatedFile : updated ) |
259 | |
{ |
260 | 0 | logInfo( "Updated %s", updatedFile.getAbsolutePath() ); |
261 | |
} |
262 | 0 | return; |
263 | |
} |
264 | |
|
265 | 0 | if ( !added.isEmpty() ) |
266 | |
{ |
267 | 0 | addFiles( added ); |
268 | |
} |
269 | |
|
270 | 0 | if ( !deleted.isEmpty() ) |
271 | |
{ |
272 | 0 | deleteFiles( deleted ); |
273 | |
} |
274 | |
|
275 | 0 | logInfo( "Checking in SCM..." ); |
276 | 0 | checkinFiles(); |
277 | 0 | } |
278 | |
} |