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 private String[] filesBeforeAction = new String[0];
69
70 public IncrementalBuildHelper( MojoExecution mojoExecution, MavenSession mavenSession )
71 {
72 this( mojoExecution, getMavenProject( mavenSession ) );
73 }
74
75 public IncrementalBuildHelper( MojoExecution mojoExecution, MavenProject mavenProject )
76 {
77 if ( mavenProject == null )
78 {
79 throw new IllegalArgumentException( "MavenProject must not be null!" );
80 }
81 if ( mojoExecution == null )
82 {
83 throw new IllegalArgumentException( "MojoExecution must not be null!" );
84 }
85
86 this.mavenProject = mavenProject;
87 this.mojoExecution = mojoExecution;
88 }
89
90
91
92
93 private static MavenProject getMavenProject( MavenSession mavenSession )
94 {
95 if ( mavenSession == null )
96 {
97 throw new IllegalArgumentException( "MavenSession must not be null!" );
98 }
99
100 return mavenSession.getCurrentProject();
101 }
102
103
104
105
106
107
108 public DirectoryScanner getDirectoryScanner()
109 {
110 if ( directoryScanner == null )
111 {
112 directoryScanner = new DirectoryScanner();
113 }
114
115 return directoryScanner;
116 }
117
118
119
120
121
122 public void setDirectoryScanner( DirectoryScanner directoryScanner )
123 {
124 this.directoryScanner = directoryScanner;
125 }
126
127
128
129
130
131
132 public File getMojoStatusDirectory() throws MojoExecutionException
133 {
134 if ( mojoExecution == null )
135 {
136 throw new MojoExecutionException( "MojoExecution could not get resolved" );
137 }
138
139 File buildOutputDirectory = new File( mavenProject.getBuild().getDirectory() );
140
141
142
143
144 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 File mojoStatusDir = new File( buildOutputDirectory, mojoStatusPath );
150
151 if ( !mojoStatusDir.exists() )
152 {
153 mojoStatusDir.mkdirs();
154 }
155
156 return mojoStatusDir;
157 }
158
159
160
161
162
163
164
165
166
167
168 public boolean inputFileTreeChanged( Set<File> inputFiles ) throws MojoExecutionException
169 {
170 File mojoConfigBase = getMojoStatusDirectory();
171 File mojoConfigFile = new File( mojoConfigBase, INPUT_FILES_LST_FILENAME );
172
173 String[] oldInputFiles = new String[0];
174
175 if ( mojoConfigFile.exists() )
176 {
177 try
178 {
179 oldInputFiles = FileUtils.fileReadArray( mojoConfigFile );
180 }
181 catch( IOException e )
182 {
183 throw new MojoExecutionException( "Error reading old mojo status " + mojoConfigFile, e );
184 }
185 }
186
187 String[] inputFileNames = new String[ inputFiles.size() ];
188 int i = 0;
189 for ( File inputFile : inputFiles )
190 {
191 inputFileNames[ i++ ] = inputFile.getAbsolutePath();
192 }
193
194 DirectoryScanResult dsr = DirectoryScanner.diffFiles( oldInputFiles, inputFileNames );
195
196
197 try
198 {
199 FileUtils.fileWriteArray( mojoConfigFile, inputFileNames );
200 }
201 catch( IOException e )
202 {
203 throw new MojoExecutionException( "Error while storing the mojo status", e );
204 }
205
206 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 File mojoConfigBase = getMojoStatusDirectory();
223 File mojoConfigFile = new File( mojoConfigBase, INPUT_FILES_LST_FILENAME );
224
225 String[] oldInputFiles = new String[0];
226
227 if ( mojoConfigFile.exists() )
228 {
229 try
230 {
231 oldInputFiles = FileUtils.fileReadArray( mojoConfigFile );
232 }
233 catch( IOException e )
234 {
235 throw new MojoExecutionException( "Error reading old mojo status " + mojoConfigFile, e );
236 }
237 }
238
239 dirScanner.scan();
240
241 try
242 {
243
244 FileUtils.fileWriteArray( mojoConfigFile, dirScanner.getIncludedFiles() );
245 }
246 catch( IOException e )
247 {
248 throw new MojoExecutionException( "Error while storing new mojo status" + mojoConfigFile, e );
249 }
250
251 DirectoryScanResult dsr = dirScanner.diffIncludedFiles( oldInputFiles );
252
253 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 File mojoConfigBase = getMojoStatusDirectory();
278 File mojoConfigFile = new File( mojoConfigBase, CREATED_FILES_LST_FILENAME );
279
280 String[] oldFiles;
281
282 try
283 {
284 oldFiles = FileUtils.fileReadArray( mojoConfigFile );
285 for ( String oldFileName : oldFiles )
286 {
287 File oldFile = new File( outputDirectory, oldFileName );
288 oldFile.delete();
289 }
290 }
291 catch( IOException e )
292 {
293 throw new MojoExecutionException( "Error reading old mojo status", e );
294 }
295
296
297 DirectoryScanner diffScanner = getDirectoryScanner();
298 diffScanner.setBasedir( outputDirectory );
299 if ( outputDirectory.exists() )
300 {
301 diffScanner.scan();
302 filesBeforeAction = diffScanner.getIncludedFiles();
303 }
304
305 return oldFiles;
306 }
307
308
309
310
311
312
313
314
315
316 public void afterRebuildExecution() throws MojoExecutionException
317 {
318 DirectoryScanner diffScanner = getDirectoryScanner();
319
320 diffScanner.scan();
321 DirectoryScanResult scanResult = diffScanner.diffIncludedFiles( filesBeforeAction );
322
323 File mojoConfigBase = getMojoStatusDirectory();
324 File mojoConfigFile = new File( mojoConfigBase, CREATED_FILES_LST_FILENAME );
325
326 try
327 {
328 FileUtils.fileWriteArray( mojoConfigFile, scanResult.getFilesAdded() );
329 }
330 catch( IOException e )
331 {
332 throw new MojoExecutionException( "Error while storing the mojo status", e );
333 }
334
335 }
336 }