1 | |
package org.apache.maven.shared.incremental; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
import org.apache.maven.execution.MavenSession; |
24 | |
import org.apache.maven.plugin.MojoExecution; |
25 | |
import org.apache.maven.plugin.MojoExecutionException; |
26 | |
import org.apache.maven.project.MavenProject; |
27 | |
import org.apache.maven.shared.utils.io.DirectoryScanResult; |
28 | |
import org.apache.maven.shared.utils.io.DirectoryScanner; |
29 | |
import org.apache.maven.shared.utils.io.FileUtils; |
30 | |
|
31 | |
import java.io.File; |
32 | |
import java.io.IOException; |
33 | |
import java.util.Set; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class IncrementalBuildHelper |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
private static final String MAVEN_STATUS_ROOT = "maven-status"; |
45 | |
public static final String CREATED_FILES_LST_FILENAME = "createdFiles.lst"; |
46 | |
private static final String INPUT_FILES_LST_FILENAME = "inputFiles.lst"; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private MojoExecution mojoExecution; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private MavenProject mavenProject; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private DirectoryScanner directoryScanner; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | private String[] filesBeforeAction = new String[0]; |
69 | |
|
70 | |
public IncrementalBuildHelper( MojoExecution mojoExecution, MavenSession mavenSession ) |
71 | |
{ |
72 | 0 | this( mojoExecution, getMavenProject( mavenSession ) ); |
73 | 0 | } |
74 | |
|
75 | |
public IncrementalBuildHelper( MojoExecution mojoExecution, MavenProject mavenProject ) |
76 | 0 | { |
77 | 0 | if ( mavenProject == null ) |
78 | |
{ |
79 | 0 | throw new IllegalArgumentException( "MavenProject must not be null!" ); |
80 | |
} |
81 | 0 | if ( mojoExecution == null ) |
82 | |
{ |
83 | 0 | throw new IllegalArgumentException( "MojoExecution must not be null!" ); |
84 | |
} |
85 | |
|
86 | 0 | this.mavenProject = mavenProject; |
87 | 0 | this.mojoExecution = mojoExecution; |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private static MavenProject getMavenProject( MavenSession mavenSession ) |
94 | |
{ |
95 | 0 | if ( mavenSession == null ) |
96 | |
{ |
97 | 0 | throw new IllegalArgumentException( "MavenSession must not be null!" ); |
98 | |
} |
99 | |
|
100 | 0 | return mavenSession.getCurrentProject(); |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public DirectoryScanner getDirectoryScanner() |
109 | |
{ |
110 | 0 | if ( directoryScanner == null ) |
111 | |
{ |
112 | 0 | directoryScanner = new DirectoryScanner(); |
113 | |
} |
114 | |
|
115 | 0 | return directoryScanner; |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void setDirectoryScanner( DirectoryScanner directoryScanner ) |
123 | |
{ |
124 | 0 | this.directoryScanner = directoryScanner; |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public File getMojoStatusDirectory() throws MojoExecutionException |
133 | |
{ |
134 | 0 | if ( mojoExecution == null ) |
135 | |
{ |
136 | 0 | throw new MojoExecutionException( "MojoExecution could not get resolved" ); |
137 | |
} |
138 | |
|
139 | 0 | File buildOutputDirectory = new File( mavenProject.getBuild().getDirectory() ); |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | 0 | String mojoStatusPath = MAVEN_STATUS_ROOT + File.separator |
145 | |
+ mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifactId() + File.separator |
146 | |
+ mojoExecution.getMojoDescriptor().getGoal() + File.separator |
147 | |
+ mojoExecution.getExecutionId(); |
148 | |
|
149 | 0 | File mojoStatusDir = new File( buildOutputDirectory, mojoStatusPath ); |
150 | |
|
151 | 0 | if ( !mojoStatusDir.exists() ) |
152 | |
{ |
153 | 0 | mojoStatusDir.mkdirs(); |
154 | |
} |
155 | |
|
156 | 0 | return mojoStatusDir; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public boolean inputFileTreeChanged( Set<File> inputFiles ) throws MojoExecutionException |
169 | |
{ |
170 | 0 | File mojoConfigBase = getMojoStatusDirectory(); |
171 | 0 | File mojoConfigFile = new File( mojoConfigBase, INPUT_FILES_LST_FILENAME ); |
172 | |
|
173 | 0 | String[] oldInputFiles = new String[0]; |
174 | |
|
175 | 0 | if ( mojoConfigFile.exists() ) |
176 | |
{ |
177 | |
try |
178 | |
{ |
179 | 0 | oldInputFiles = FileUtils.fileReadArray( mojoConfigFile ); |
180 | |
} |
181 | 0 | catch( IOException e ) |
182 | |
{ |
183 | 0 | throw new MojoExecutionException( "Error reading old mojo status " + mojoConfigFile, e ); |
184 | 0 | } |
185 | |
} |
186 | |
|
187 | 0 | String[] inputFileNames = new String[ inputFiles.size() ]; |
188 | 0 | int i = 0; |
189 | 0 | for ( File inputFile : inputFiles ) |
190 | |
{ |
191 | 0 | inputFileNames[ i++ ] = inputFile.getAbsolutePath(); |
192 | |
} |
193 | |
|
194 | 0 | DirectoryScanResult dsr = DirectoryScanner.diffFiles( oldInputFiles, inputFileNames ); |
195 | |
|
196 | |
|
197 | |
try |
198 | |
{ |
199 | 0 | FileUtils.fileWriteArray( mojoConfigFile, inputFileNames ); |
200 | |
} |
201 | 0 | catch( IOException e ) |
202 | |
{ |
203 | 0 | throw new MojoExecutionException( "Error while storing the mojo status", e ); |
204 | 0 | } |
205 | |
|
206 | 0 | return ( dsr.getFilesAdded().length > 0 || dsr.getFilesRemoved().length > 0 ); |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public boolean inputFileTreeChanged( DirectoryScanner dirScanner ) throws MojoExecutionException |
221 | |
{ |
222 | 0 | File mojoConfigBase = getMojoStatusDirectory(); |
223 | 0 | File mojoConfigFile = new File( mojoConfigBase, INPUT_FILES_LST_FILENAME ); |
224 | |
|
225 | 0 | String[] oldInputFiles = new String[0]; |
226 | |
|
227 | 0 | if ( mojoConfigFile.exists() ) |
228 | |
{ |
229 | |
try |
230 | |
{ |
231 | 0 | oldInputFiles = FileUtils.fileReadArray( mojoConfigFile ); |
232 | |
} |
233 | 0 | catch( IOException e ) |
234 | |
{ |
235 | 0 | throw new MojoExecutionException( "Error reading old mojo status " + mojoConfigFile, e ); |
236 | 0 | } |
237 | |
} |
238 | |
|
239 | 0 | dirScanner.scan(); |
240 | |
|
241 | |
try |
242 | |
{ |
243 | |
|
244 | 0 | FileUtils.fileWriteArray( mojoConfigFile, dirScanner.getIncludedFiles() ); |
245 | |
} |
246 | 0 | catch( IOException e ) |
247 | |
{ |
248 | 0 | throw new MojoExecutionException( "Error while storing new mojo status" + mojoConfigFile, e ); |
249 | 0 | } |
250 | |
|
251 | 0 | DirectoryScanResult dsr = dirScanner.diffIncludedFiles( oldInputFiles ); |
252 | |
|
253 | 0 | return ( dsr.getFilesAdded().length > 0 || dsr.getFilesRemoved().length > 0 ); |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
public String[] beforeRebuildExecution( File outputDirectory ) throws MojoExecutionException |
276 | |
{ |
277 | 0 | File mojoConfigBase = getMojoStatusDirectory(); |
278 | 0 | File mojoConfigFile = new File( mojoConfigBase, CREATED_FILES_LST_FILENAME ); |
279 | |
|
280 | |
String[] oldFiles; |
281 | |
|
282 | |
try |
283 | |
{ |
284 | 0 | oldFiles = FileUtils.fileReadArray( mojoConfigFile ); |
285 | 0 | for ( String oldFileName : oldFiles ) |
286 | |
{ |
287 | 0 | File oldFile = new File( outputDirectory, oldFileName ); |
288 | 0 | oldFile.delete(); |
289 | |
} |
290 | |
} |
291 | 0 | catch( IOException e ) |
292 | |
{ |
293 | 0 | throw new MojoExecutionException( "Error reading old mojo status", e ); |
294 | 0 | } |
295 | |
|
296 | |
|
297 | 0 | DirectoryScanner diffScanner = getDirectoryScanner(); |
298 | 0 | diffScanner.setBasedir( outputDirectory ); |
299 | 0 | if ( outputDirectory.exists() ) |
300 | |
{ |
301 | 0 | diffScanner.scan(); |
302 | 0 | filesBeforeAction = diffScanner.getIncludedFiles(); |
303 | |
} |
304 | |
|
305 | 0 | return oldFiles; |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public void afterRebuildExecution() throws MojoExecutionException |
317 | |
{ |
318 | 0 | DirectoryScanner diffScanner = getDirectoryScanner(); |
319 | |
|
320 | 0 | diffScanner.scan(); |
321 | 0 | DirectoryScanResult scanResult = diffScanner.diffIncludedFiles( filesBeforeAction ); |
322 | |
|
323 | 0 | File mojoConfigBase = getMojoStatusDirectory(); |
324 | 0 | File mojoConfigFile = new File( mojoConfigBase, CREATED_FILES_LST_FILENAME ); |
325 | |
|
326 | |
try |
327 | |
{ |
328 | 0 | FileUtils.fileWriteArray( mojoConfigFile, scanResult.getFilesAdded() ); |
329 | |
} |
330 | 0 | catch( IOException e ) |
331 | |
{ |
332 | 0 | throw new MojoExecutionException( "Error while storing the mojo status", e ); |
333 | 0 | } |
334 | |
|
335 | 0 | } |
336 | |
} |