1 package org.codehaus.mojo.appassembler;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.execution.MavenSession;
35 import org.apache.maven.model.Resource;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.plugin.MojoFailureException;
38 import org.apache.maven.plugins.annotations.Component;
39 import org.apache.maven.plugins.annotations.Parameter;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.shared.filtering.MavenFilteringException;
42 import org.apache.maven.shared.filtering.MavenResourcesExecution;
43 import org.apache.maven.shared.filtering.MavenResourcesFiltering;
44 import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorService;
45 import org.codehaus.plexus.util.FileUtils;
46
47
48
49
50
51
52
53
54 public abstract class AbstractScriptGeneratorMojo
55 extends AbstractAppAssemblerMojo
56 {
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @Parameter
72 protected String environmentSetupFileName;
73
74
75
76
77
78
79 @Parameter
80 protected File licenseHeaderFile;
81
82
83
84
85
86
87
88 @Parameter( property = "unixScriptTemplate" )
89 protected String unixScriptTemplate;
90
91
92
93
94
95
96
97
98
99
100
101 @Parameter( defaultValue = "false" )
102 protected boolean useWildcardClassPath;
103
104
105
106
107
108
109
110 @Parameter( property = "windowsScriptTemplate" )
111 protected String windowsScriptTemplate;
112
113
114
115
116
117 @Parameter( defaultValue = "${project.runtimeArtifacts}", readonly = true )
118 protected List<Artifact> artifacts;
119
120 @Parameter( defaultValue = "${project}", readonly = true, required = true )
121 protected MavenProject mavenProject;
122
123
124
125
126 @Parameter( defaultValue = "true" )
127 protected boolean generateRepository;
128
129
130
131
132
133 @Parameter( defaultValue = "etc" )
134 protected String configurationDirectory;
135
136
137
138
139
140
141 @Parameter( defaultValue = "src/main/config" )
142 protected File configurationSourceDirectory;
143
144
145
146
147
148
149
150
151 @Parameter( defaultValue = "false" )
152 protected boolean filterConfigurationDirectory;
153
154
155
156
157
158
159 @Parameter( defaultValue = "false" )
160 protected boolean copyConfigurationDirectory;
161
162
163
164
165
166
167
168 @Parameter
169 protected File preAssembleDirectory;
170
171
172
173
174
175
176
177 @Parameter( defaultValue = "false" )
178 protected boolean filterPreAssembleDirectory;
179
180
181
182
183
184
185
186
187 @Parameter
188 protected String endorsedDir;
189
190
191
192
193
194
195 @Parameter( defaultValue = "${project.build.filters}", readonly = true )
196 protected List<String> buildFilters;
197
198
199
200
201
202
203 @Parameter( defaultValue = "${project.build.sourceEncoding}" )
204 protected String encoding;
205
206
207
208
209
210
211
212 @Parameter
213 protected String escapeString;
214
215
216
217
218 @Parameter( defaultValue = "${session}", readonly = true, required = true )
219 private MavenSession session;
220
221
222
223
224
225
226
227 @Parameter( property = "logsDirectory" )
228 protected String logsDirectory;
229
230
231
232
233
234
235
236 @Parameter( property = "tempDirectory" )
237 protected String tempDirectory;
238
239
240
241
242
243 @Component
244 protected DaemonGeneratorService daemonGeneratorService;
245
246
247
248
249
250
251 @Component( role = MavenResourcesFiltering.class, hint = "default" )
252 protected MavenResourcesFiltering mavenResourcesFiltering;
253
254 protected void doCopyConfigurationDirectory( final String targetDirectory )
255 throws MojoFailureException
256 {
257 getLog().debug( "copying configuration directory." );
258
259
260 Resource resource = new Resource();
261 resource.setDirectory( configurationSourceDirectory.getAbsolutePath() );
262 resource.setFiltering( filterConfigurationDirectory );
263 resource.setTargetPath( configurationDirectory );
264 List<Resource> resources = new ArrayList<Resource>();
265 resources.add( resource );
266
267 MavenResourcesExecution mavenResourcesExecution =
268 new MavenResourcesExecution( resources, new File( targetDirectory ), mavenProject, encoding, buildFilters,
269 Collections.<String>emptyList(), session );
270
271 mavenResourcesExecution.setEscapeString( escapeString );
272
273 mavenResourcesExecution.setIncludeEmptyDirs( true );
274 mavenResourcesExecution.setUseDefaultFilterWrappers( true );
275
276
277
278
279
280
281 try
282 {
283 getLog().debug( "Will try to copy configuration files from "
284 + configurationSourceDirectory.getAbsolutePath() + " to " + targetDirectory
285 + FileUtils.FS + configurationDirectory );
286
287
288 mavenResourcesFiltering.filterResources( mavenResourcesExecution );
289 }
290 catch ( MavenFilteringException mfe )
291 {
292 throw new MojoFailureException( "Failed to copy/filter the configuration files." );
293 }
294 }
295
296 protected void doCopyPreAssembleDirectory( final String targetDirectory )
297 throws MojoFailureException
298 {
299
300 Resource resource = new Resource();
301 resource.setDirectory( preAssembleDirectory.getAbsolutePath() );
302 resource.setFiltering( filterPreAssembleDirectory );
303 List<Resource> resources = new ArrayList<Resource>();
304 resources.add( resource );
305
306 MavenResourcesExecution mavenResourcesExecution =
307 new MavenResourcesExecution( resources, new File( targetDirectory ), mavenProject, encoding, buildFilters,
308 Collections.<String>emptyList(), session );
309
310 mavenResourcesExecution.setEscapeString( escapeString );
311
312 mavenResourcesExecution.setIncludeEmptyDirs( true );
313 mavenResourcesExecution.setUseDefaultFilterWrappers( true );
314
315 try
316 {
317 getLog().info( "Copy pre-assemble files from " + this.preAssembleDirectory.getAbsolutePath() + " to "
318 + targetDirectory );
319
320
321 mavenResourcesFiltering.filterResources( mavenResourcesExecution );
322 }
323 catch ( MavenFilteringException mfe )
324 {
325 throw new MojoFailureException( "Failed to copy/filter the configuration files." );
326 }
327 }
328
329
330
331
332
333 protected void doCreateExtraDirectories( File targetDirectory )
334 throws MojoFailureException
335 {
336 if ( this.logsDirectory != null )
337 {
338 File logsDirectory = new File( targetDirectory, this.logsDirectory );
339 if ( logsDirectory.exists() )
340 {
341 if ( !logsDirectory.delete() )
342 {
343 throw new MojoFailureException( "Failed to delete existing directory for log files." );
344 }
345 }
346 boolean success = logsDirectory.mkdirs();
347 if ( !success )
348 {
349 throw new MojoFailureException( "Failed to create directory for log files." );
350 }
351 }
352 if ( this.tempDirectory != null )
353 {
354 File tempDirectory = new File( targetDirectory, this.tempDirectory );
355 if ( tempDirectory.exists() )
356 {
357 if ( !tempDirectory.delete() )
358 {
359 throw new MojoFailureException( "Failed to delete existing directory for temp files." );
360 }
361 }
362 boolean success = tempDirectory.mkdirs();
363 if ( !success )
364 {
365 throw new MojoFailureException( "Failed to create directory for temp files." );
366 }
367 }
368 }
369
370 protected void installDependencies( final String outputDirectory, final String repositoryName )
371 throws MojoExecutionException, MojoFailureException
372 {
373 if ( generateRepository )
374 {
375
376 ArtifactRepository artifactRepository =
377 artifactRepositoryFactory.createDeploymentArtifactRepository( "appassembler", "file://"
378 + outputDirectory + "/" + repositoryName, getArtifactRepositoryLayout(), false );
379
380 for ( Artifact artifact : artifacts )
381 {
382 installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName );
383 }
384
385
386 installArtifact( projectArtifact, artifactRepository );
387 }
388 }
389 }