1
2
3
4
5
6
7
8
9
10
11
12
13 package org.sonatype.install4j.maven;
14
15 import java.io.BufferedOutputStream;
16 import java.io.BufferedReader;
17 import java.io.File;
18 import java.io.FileOutputStream;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.io.OutputStreamWriter;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.Properties;
30
31 import org.apache.maven.plugins.annotations.Component;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.project.MavenProjectHelper;
35 import org.apache.tools.ant.taskdefs.ExecTask;
36
37
38
39
40
41
42
43
44
45 @Mojo(name = "compile")
46 public class CompileMojo
47 extends Install4jcMojoSupport
48 {
49
50
51
52 private static final byte[] BOM = new byte[] {(byte)0xEF, (byte)0xBB, (byte)0xBF};
53
54
55
56
57 @Parameter(property = "install4j.projectFile", required = true)
58 private File projectFile;
59
60
61
62
63 @Parameter(property = "install4j.verbose", defaultValue = "false")
64 private boolean verbose;
65
66
67
68
69 @Parameter(property = "install4j.quiet", defaultValue = "false")
70 private boolean quiet;
71
72
73
74
75 @Parameter(property = "install4j.test", defaultValue = "false")
76 private boolean test;
77
78
79
80
81 @Parameter(property = "install4j.incremental", defaultValue = "false")
82 private boolean incremental;
83
84
85
86
87 @Parameter(property = "install4j.debug", defaultValue = "false")
88 private boolean debug;
89
90
91
92
93 @Parameter(property = "install4j.faster", defaultValue = "false")
94 private boolean faster;
95
96
97
98
99 @Parameter(property = "install4j.disableSigning", defaultValue = "false")
100 private boolean disableSigning;
101
102
103
104
105 @Parameter(property = "install4j.disableBundling", defaultValue = "false")
106 private boolean disableBundling;
107
108
109
110
111 @Parameter(property = "install4j.preserve", defaultValue = "false")
112 private boolean preserve;
113
114
115
116
117 @Parameter(property = "install4j.winKeystorePassword")
118 private String winKeystorePassword;
119
120
121
122
123 @Parameter(property = "install4j.macKeystorePassword")
124 private String macKeystorePassword;
125
126
127
128
129 @Parameter(property = "install4j.appleIdPassword")
130 private String appleIdPassword;
131
132
133
134
135 @Parameter(property = "install4j.release", defaultValue = "${project.version}")
136 private String release;
137
138
139
140
141 @Parameter(property = "install4j.destination", defaultValue = "${project.build.directory}/media")
142 private File destination;
143
144
145
146
147 @Parameter(property = "install4j.buildSelected", defaultValue = "false")
148 private boolean buildSelected;
149
150
151
152
153 @Parameter(property = "install4j.buildIds")
154 private String buildIds;
155
156
157
158
159 @Parameter(property = "install4j.mediaTypes")
160 private String mediaTypes;
161
162
163
164
165 @Parameter(property = "install4j.variableFile")
166 private File variableFile;
167
168
169
170
171 @Parameter
172 private Properties variables;
173
174
175
176
177 @Parameter(defaultValue = "${project.build.directory}/install4j-variables.txt")
178 private File variablesTempFile;
179
180
181
182
183 @Parameter
184 private List<String> jvmArguments;
185
186
187
188
189
190
191 @Parameter(property = "install4j.attach", defaultValue = "false")
192 private boolean attach;
193
194 @Component
195 private MavenProjectHelper projectHelper;
196
197 @Override
198 protected void execute(final AntHelper ant, final ExecTask task) throws Exception {
199 task.createArg().setFile(projectFile);
200
201
202 task.setFailonerror(true);
203 task.setFailIfExecutionFails(true);
204
205 if (jvmArguments != null) {
206 Iterator<String> iter = jvmArguments.iterator();
207 while (iter.hasNext()) {
208 String arg = String.valueOf(iter.next());
209 task.createArg().setValue("-J" + arg);
210 }
211 }
212
213 if (verbose) {
214 task.createArg().setValue("--verbose");
215 }
216
217 if (quiet) {
218 task.createArg().setValue("--quiet");
219 }
220
221 if (test) {
222 task.createArg().setValue("--test");
223 }
224
225 if (incremental) {
226 task.createArg().setValue("--incremental");
227 }
228
229 if (debug) {
230 task.createArg().setValue("--debug");
231 }
232
233 if (faster) {
234 task.createArg().setValue("--faster");
235 }
236
237 if (disableSigning) {
238 task.createArg().setValue("--disable-signing");
239 }
240
241 if (disableBundling) {
242 task.createArg().setValue("--disable-bundling");
243 }
244
245 if (preserve) {
246 task.createArg().setValue("--preserve");
247 }
248
249 if (winKeystorePassword != null) {
250 task.createArg().setValue("--win-keystore-password");
251 task.createArg().setValue(winKeystorePassword);
252 }
253
254 if (macKeystorePassword != null) {
255 task.createArg().setValue("--mac-keystore-password");
256 task.createArg().setValue(macKeystorePassword);
257 }
258
259 if (appleIdPassword != null) {
260 task.createArg().setValue("--apple-id-password");
261 task.createArg().setValue(appleIdPassword);
262 }
263
264 if (release != null) {
265 task.createArg().setValue("--release");
266 task.createArg().setValue(release);
267 }
268
269 if (destination != null) {
270 task.createArg().setValue("--destination");
271 task.createArg().setFile(destination);
272 }
273
274 if (buildSelected) {
275 task.createArg().setValue("--build-selected");
276 }
277
278 if (buildIds != null) {
279 task.createArg().setValue("--build-ids");
280 task.createArg().setValue(buildIds);
281 }
282
283 if (mediaTypes != null) {
284 task.createArg().setValue("--media-types");
285 task.createArg().setValue(mediaTypes);
286 }
287
288 if (variableFile != null || (variables != null && !variables.isEmpty())) {
289 StringBuilder buff = new StringBuilder();
290 if (variableFile != null) {
291 buff.append(variableFile.getPath());
292 }
293
294
295 if (!variables.isEmpty()) {
296 if (buff.length() > 0) {
297 buff.append(";");
298 }
299 writeVariablesToFile(variables, variablesTempFile);
300 buff.append(variablesTempFile.getPath());
301 }
302
303 task.createArg().setValue("--var-file");
304 task.createArg().setValue(buff.toString());
305 }
306
307 task.execute();
308
309 if (attach) {
310 for (AttachedFile attachedFile : parseAttachedFiles()) {
311 projectHelper.attachArtifact(
312 project,
313 attachedFile.type,
314 attachedFile.classifier,
315 attachedFile.file
316 );
317 }
318
319
320 maybeAttachFile("txt", "output", new File(destination, "output.txt"));
321 maybeAttachFile("xml", "updates", new File(destination, "updates.xml"));
322 maybeAttachFile("txt", "md5sums", new File(destination, "md5sums"));
323 }
324 }
325
326 private void writeVariablesToFile(final Properties variables, final File file) throws IOException {
327 log.info("Writing variables to: " + file.getPath());
328 OutputStream output = new BufferedOutputStream(new FileOutputStream(file));
329 try {
330
331 output.write(BOM);
332
333 PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"));
334 for (Entry<Object, Object> entry : variables.entrySet()) {
335 writer.print(entry.getKey());
336 writer.print('=');
337 writer.println(entry.getValue());
338 }
339
340 writer.flush();
341 }
342 finally {
343 output.close();
344 }
345 }
346
347 private void maybeAttachFile(final String type, final String classifier, final File file) {
348 if (!file.exists()) {
349 log.warn("File missing; unable to attach file: " + file);
350 return;
351 }
352 projectHelper.attachArtifact(project, type, classifier, file);
353 }
354
355 private static class AttachedFile
356 {
357 public final File file;
358
359 public final String type;
360
361 public final String classifier;
362
363 private AttachedFile(final File file, final String classifier) {
364 this.file = file;
365 this.type = getType(file);
366
367 this.classifier = classifier;
368 }
369
370 private static String getType(final File file) {
371 String path = file.getAbsolutePath();
372
373
374 if (path.endsWith(".tar.gz")) {
375 return "tar.gz";
376 }
377 else if (path.endsWith(".tar.bz2")) {
378 return "tar.bz2";
379 }
380
381 int i = path.lastIndexOf(".");
382 return path.substring(i + 1, path.length());
383 }
384 }
385
386 private List<AttachedFile> parseAttachedFiles() throws Exception {
387 File file = new File(destination, "output.txt");
388 if (!file.exists()) {
389 log.warn("Missing output.txt file: " + file);
390 return Collections.emptyList();
391 }
392
393 log.debug("Parsing: " + file);
394
395 BufferedReader reader = new BufferedReader(new FileReader(file));
396 List<AttachedFile> files = new ArrayList<AttachedFile>();
397 String line;
398 while ((line = reader.readLine()) != null) {
399 if (line.startsWith("#")) {
400
401 continue;
402 }
403
404 log.debug("Read: " + line);
405
406
407
408 String[] parts = line.split("\t");
409 AttachedFile attachedFile = new AttachedFile(
410 normalize(destination, parts[3]),
411 parts[0]
412 );
413 files.add(attachedFile);
414 }
415
416 return files;
417 }
418
419
420
421
422
423
424
425
426
427
428 private File normalize(final File base, final String path) throws Exception {
429 String basePath = base.getCanonicalPath();
430 String filePath = new File(path).getCanonicalPath();
431 if (filePath.startsWith(basePath)) {
432 String relPath = filePath.substring(basePath.length() + 1, filePath.length());
433 return new File(base, relPath);
434
435 }
436 else {
437 return new File(path);
438 }
439 }
440 }