This repository has been archived by the owner. It is now read-only.
/
PackageAdtMojo.java
439 lines (346 loc) · 12.5 KB
/
PackageAdtMojo.java
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* Copyright (C) 2012 https://github.com/yelbota/adt-maven-plugin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yelbota.plugins.adt;
import com.yelbota.plugins.adt.exceptions.AdtConfigurationException;
import com.yelbota.plugins.adt.model.AneModel;
import com.yelbota.plugins.adt.model.ApplicationDescriptorModel;
import com.yelbota.plugins.adt.model.ApplicationDescriptorModel;
import com.yelbota.plugins.adt.utils.CleanStream;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* @goal package
* @phase package
* @threadSafe
*/
public class PackageAdtMojo extends CommandAdtMojo {
/**
* @parameter default-value="${project}
*/
public MavenProject project;
/**
* ADT package target. available:
* Desktop AIR runtime:
* air,
* airi
* Android:
* apk,
* apk-debug,
* apk-captive-runtime
* IOS:
* ipa-ad-hoc,
* ipa-app-store,
* ipa-debug,
* ipa-test,
* ipa-debug-interpreter,
* ipa-test-interpreter
* Native desktop:
* native
* For more information visit Adobe website
* http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac1e63e3d128cdca935b-8000.html
*
* @parameter
*/
public String target;
/**
* http://help.adobe.com/en_US/air/build/WS5b3ccc516d4fbf351e63e3d118666ade46-7f72.html
*
* @parameter default-value="pkcs12"
*/
public String storetype;
/**
* Certificate file
*
* @parameter expression="${build.adt.keystore}"
*/
public File keystore;
/**
* Password for certificate file.
*
* @parameter expression="${build.adt.storepass}"
*/
public String storepass;
/**
* Required for iOS packages (ipa*).
* Example: /Users/yelbota/myapp.mobileprovision
*
* @parameter expression="${build.adt.mobileprovision}"
*/
public File provisioningProfile;
/**
* Output filename without extension.
*
* @parameter expression="${project.artifactId}-${project.version}"
*/
public String fileNamePattern;
/**
* AIR Application descriptor. For example src/main/flex/MyProject-app.xml
*
* @parameter default-value="src/main/resources/application-descriptor.xml"
*/
public File descriptor;
/**
* File to include in application descriptor
*
* @required
* @parameter expression="${project.build.directory}/${project.build.finalName}.${project.packaging}"
*/
public File applicationContent;
/**
* Root directory for includes.
*
* @parameter expression="${project.build.directory}/classes"
*/
public File includesRoot;
/**
* List of files and directories to include into the package
* relative to includesRoot directory
*
* @parameter
*/
public List<String> includes;
/**
* A string value (such as "v1", "2.5", or "Alpha 1") that represents the version of the application, as it should
* be shown to users.
*
* @parameter expression="${project.version}"
*/
public String versionLabel;
/**
* A string value of the format <0-999>.<0-999>.<0-999> that represents application version which can be used
* to check for application upgrade.Values can also be 1-part or 2-part. It is not necessary to have a 3-part value.
* An updated version of application must have a versionNumber value higher than the previous version.
*
* @parameter default-value="0.0.0"
*/
public String versionNumber;
@Override
protected void prepareArguments() throws MojoFailureException
{
super.prepareArguments();
validateConfiguration();
File aneDir = prepareAneDir();
File finalAppDescriptor = getFinalApplicationDescriptor(getExtensionsIds(aneDir));
List<String> args = new ArrayList<String>();
args.addAll(getPackageArguments());
if (!isNativeTarget())
args.addAll(getCertificateArguments());
args.addAll(getMainArgs(finalAppDescriptor));
args.addAll(getIncludesArgs());
if (aneDir.list().length > 0)
args.addAll(getExtDirArguments(aneDir));
String[] argsArray = args.toArray(new String[]{});
getLog().info("Building package " + getFinalName());
arguments = StringUtils.join(argsArray, " ");
}
private List<String> getExtensionsIds(File aneDir) throws MojoFailureException {
List<String> result = new ArrayList<String>();
for (String name: aneDir.list()) {
AneModel model = new AneModel(new File(aneDir, name));
result.add(model.getId());
}
return result;
}
private File getFinalApplicationDescriptor(List<String> extensionsIds) throws MojoFailureException {
ApplicationDescriptorModel configurator = new ApplicationDescriptorModel(descriptor);
File mutatedDescriptor = FileUtils.resolveFile(outputDirectory, "application-descriptor.xml");
configurator.setContent(applicationContent.getName());
configurator.setVersionLabel(versionLabel);
configurator.setVersionNumber(versionNumber);
configurator.setExtensionIds(extensionsIds);
configurator.printToFile(mutatedDescriptor);
return mutatedDescriptor;
}
public File prepareAneDir() throws MojoFailureException {
try {
File dir = new File(outputDirectory, "extDir");
if (dir.exists())
FileUtils.cleanDirectory(dir);
else dir.mkdir();
for (Artifact artifact : project.getDependencyArtifacts()) {
if (artifact.getType().equals("ane")) {
FileUtils.copyFileToDirectory(artifact.getFile(), dir);
}
}
return dir;
} catch (IOException e) {
throw failWith("Can't create ANE directory");
}
}
public List<String> getPackageArguments() {
List<String> args = new ArrayList<String>();
if (isUnsignedAirTarget()) {
args.add("-prepare");
}
else {
args.add("-package");
if (isNativeTarget())
args.addAll(getCertificateArguments());
if (!isAirTarget()) {
args.add("-target");
args.add(target);
}
}
return args;
}
public List<String> getCertificateArguments() {
List<String> args = new ArrayList<String>();
if (!isUnsignedAirTarget()) {
if (getProvisioningProfileRequired()) {
args.add("-provisioning-profile");
args.add(provisioningProfile.getAbsolutePath());
}
args.add("-storetype");
args.add(storetype);
args.add("-keystore");
args.add(keystore.getAbsolutePath());
args.add("-storepass");
args.add(storepass);
}
return args;
}
public List<String> getExtDirArguments(File extDir) {
List<String> result = new ArrayList<String>();
result.add("-extdir");
result.add(extDir.getAbsolutePath());
return result;
}
/**
* @param finalAppDescriptorFile mutated application descriptor
* @return list of args
*/
public List<String> getMainArgs(File finalAppDescriptorFile) throws MojoFailureException
{
List<String> args = new ArrayList<String>();
File outputFile = new File(outputDirectory, getFinalName());
args.add(outputFile.getAbsolutePath());
args.add(finalAppDescriptorFile.getAbsolutePath());
return args;
}
private String getFinalName() throws MojoFailureException
{
String ext;
if (isiOSTarget()) {
ext = "ipa";
} else if (isAndroidTarget()) {
ext = "apk";
} else if (isUnsignedAirTarget()) {
ext = "airi";
} else if (isNativeTarget()) {
ext = getNativeDesktopFileExt();
} else {
ext = "air";
}
return fileNamePattern + "." + ext;
}
public List<String> getIncludesArgs() throws MojoFailureException {
List<String> args = new ArrayList<String>();
// Get SWF file to add in includes
if (!applicationContent.exists()) {
throw failWith("Application content (" + applicationContent.getPath() + ") not found");
} else {
addIncludeArg(args, applicationContent.getParentFile(), applicationContent.getName());
if (includes == null)
return args;
for (String name : includes) {
File include = new File(includesRoot, name);
if (include.exists()) {
addIncludeArg(args, includesRoot, name);
} else {
File includeWithRoot = include;
include = new File(name);
if (include.exists()) {
addIncludeArg(args, include.getParentFile(), include.getName());
} else {
String path = include.getPath() + " (or " + includeWithRoot.getPath() + ")";
throw failWith("Include " + path + " doesn't exists");
}
}
}
}
return args;
}
public void validateConfiguration() throws AdtConfigurationException {
if (target == null)
throw new AdtConfigurationException("target (for example: air, apk, ipa-debug) must be specified");
validateCertificate();
}
public void validateCertificate() throws AdtConfigurationException {
if (getCertificateRequired()) {
// Certificate required
if (storetype == null || storepass == null || keystore == null)
throw new AdtConfigurationException("Signing options (storetype, keystore, storepass) must be defined");
else if (!keystore.exists())
throw new AdtConfigurationException("Keystore file doesn't exists");
}
if (getProvisioningProfileRequired()) {
// Check mobileprovision
if (provisioningProfile == null) {
throw new AdtConfigurationException("provisioningProfile must be defined for ipa* targets");
} else if (!provisioningProfile.exists()) {
throw new AdtConfigurationException("provisioningProfile not found");
}
}
}
private void addIncludeArg(List<String> args, File dir, String name) {
args.add("-C");
args.add(dir.getAbsolutePath());
args.add(name);
}
private boolean getCertificateRequired() {
return !isUnsignedAirTarget();
}
private boolean getProvisioningProfileRequired() {
return isiOSTarget();
}
private boolean isNativeTarget() {
return target.equals("native");
}
private boolean isUnsignedAirTarget() {
return target.indexOf("airi") > -1;
}
private boolean isAirTarget() {
return target.indexOf("airi") < 0 && target.indexOf("air") > -1;
}
private boolean isiOSTarget() {
return target.indexOf("ipa") > -1;
}
private boolean isAndroidTarget() {
return target.indexOf("apk") > -1;
}
private String getNativeDesktopFileExt() throws MojoFailureException
{
String fullName = System.getProperty("os.name");
String osName = fullName.toLowerCase();
if (osName.indexOf("win") > -1)
return "exe";
else if (osName.indexOf("mac") > -1)
return "dmg";
else {
throw failWith(fullName + " is not supported");
}
}
}