View Javadoc

1   /*
2    * Copyright 2011 SOFTEC sa. All rights reserved.
3    *
4    * This source code is licensed under the Creative Commons
5    * Attribution-NonCommercial-NoDerivs 3.0 Luxembourg
6    * License.
7    *
8    * To view a copy of this license, visit
9    * http://creativecommons.org/licenses/by-nc-nd/3.0/lu/
10   * or send a letter to Creative Commons, 171 Second Street,
11   * Suite 300, San Francisco, California, 94105, USA.
12   */
13  
14  package org.codehaus.mojo.javascript;
15  
16  import com.sun.servicetag.Installer;
17  import org.apache.maven.plugin.AbstractMojo;
18  import org.apache.maven.plugin.MojoExecutionException;
19  import org.apache.maven.plugin.MojoFailureException;
20  import org.apache.maven.plugin.logging.Log;
21  import org.apache.maven.project.MavenProject;
22  import org.codehaus.mojo.javascript.titanium.TitaniumBuilder;
23  import org.codehaus.mojo.javascript.titanium.TitaniumUtils;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStreamReader;
29  import java.text.MessageFormat;
30  import java.util.List;
31  import java.util.UUID;
32  import java.util.regex.Matcher;
33  import java.util.regex.Pattern;
34  
35  /**
36   * Compile the titanium application based on the selected platform.
37   *
38   * @author <a href="mailto:olivier.desaive@softec.lu">Olivier Desaive</a>
39   * @goal titanium-package
40   * @phase package
41   * @plexus.component role-hint="titanium"
42   */
43  public class TitaniumPackageMojo extends AbstractTitaniumPackageMojo {
44  
45      public void execute() throws MojoExecutionException, MojoFailureException {
46          TitaniumUtils.checkVirtualDevice(platform, getTitaniumSettings() , getVirtualDevice());
47  
48          if (!checkPomSettings()) {
49              return;
50          }
51  
52          File builder = null;
53  
54          if (platform.compareToIgnoreCase("android") == 0) {
55              builder = resolveAndroidBuilder();
56              packageAndroid(builder);
57          } else if (platform.compareToIgnoreCase("iphone") == 0
58                  || platform.compareToIgnoreCase("ipad") == 0
59                  || platform.compareToIgnoreCase("universal") == 0) {
60              builder = resolveIOSBuilder();
61              packageIphone(builder);
62          } else {
63              throw new MojoExecutionException("Unsupported platform: " + platform);
64          }
65      }
66  
67      protected void packageAndroid(File builder) throws MojoFailureException, MojoExecutionException {
68          checkAndroidAppId();
69  
70          TitaniumBuilder tiBuilder = new TitaniumBuilder(builder,
71                  null,
72                  titaniumSettings.getAndroidSdk());
73  
74          try {
75              if (executeMode.compareToIgnoreCase("virtual") == 0) {
76                  launchOnEmulator(tiBuilder);
77              } else if (executeMode.compareToIgnoreCase("device") == 0) {
78                  launchOnDevice(tiBuilder);
79              } else if (executeMode.compareToIgnoreCase("none") == 0) {
80                  launchDistribute(tiBuilder);
81              }
82  
83          } catch (MojoFailureException e) {
84              throw e;
85          } catch (Throwable t) {
86              throw new MojoExecutionException("Error while executing android builder", t);
87          }
88      }
89  
90      private boolean checkAndroidAppId() {
91          String appId = project.getGroupId() + "." + project.getArtifactId();
92          if ( !Pattern.matches("^([a-zA-Z0-9]+\\.[a-zA-Z0-9]+)+$", appId) ) {
93              getLog().warn("The appId " + appId + " may not be supported under android. " +
94                      "You should ensure that the appid follow com.company.name " +
95                      "and doesn't contain the '-' character.");
96              return false;
97          } else {
98              return true;
99          }
100     }
101 
102     private void launchOnEmulator(TitaniumBuilder tiBuilder)
103             throws MojoExecutionException, MojoFailureException,
104             IOException, InterruptedException {
105         tiBuilder.launchOnAndroidEmulator(project.getName(),
106                                         getTiProjectDirectory(),
107                                         project.getGroupId() + "." + project.getArtifactId(),
108                                         getAndroidAPI(),
109                                         getVirtualDevice().getAndroidAPI(),
110                                         getVirtualDevice().getSkin(),
111                                         getVirtualDevice().getWait(),
112                                         getLog());
113     }
114 
115     private void launchOnDevice(TitaniumBuilder tiBuilder) throws MojoFailureException, MojoExecutionException, IOException, InterruptedException {
116         ProcessBuilder pb = tiBuilder.createAndroidBuilderProcess("install",
117                 project.getName(),
118                 getTiProjectDirectory().getAbsolutePath(),
119                 project.getGroupId() + "." + project.getArtifactId(),
120                 getAndroidAPI(),
121                 virtualDevice.getSkin());
122         Process deviceProcess = pb.start();
123         getLog().info("Deploying on device ");
124         TitaniumBuilder.logProcess(deviceProcess, getLog());
125         getLog().info("done");
126         if (deviceProcess.exitValue() != 0) {
127             throw new MojoFailureException("Titanium builder failed");
128         }
129     }
130 
131     private void launchDistribute(TitaniumBuilder tiBuilder) throws MojoFailureException, MojoExecutionException, IOException, InterruptedException {
132         ProcessBuilder pb = tiBuilder.createAndroidDistributeBuilderProcess(outputDirectory,
133                 project.getName(),
134                 getTiProjectDirectory().getAbsolutePath(),
135                 project.getGroupId() + "." + project.getArtifactId(),
136                 getTitaniumSettings().getKeystore(titaniumVersion, new File(outputDirectory, "titanium_mobile")),
137                 getTitaniumSettings().getKeystorePassword(),
138                 getTitaniumSettings().getKeystoreAlias(),
139                 virtualDevice.getAndroidAPI(),
140                 virtualDevice.getSkin());
141         Process distProcess = pb.start();
142         getLog().info("Creating distribution ");
143         TitaniumBuilder.logProcess(distProcess, getLog());
144         getLog().info("done");
145         if (distProcess.exitValue() != 0) {
146             throw new MojoFailureException("Titanium builder failed");
147         }
148     }
149 
150     protected void packageIphone(File iosBuilder) throws MojoFailureException, MojoExecutionException {
151         getLog().info("Packaging for iPhone");
152 
153         TitaniumBuilder tiBuilder = new TitaniumBuilder(null,
154                 iosBuilder,
155                 null);
156 
157 
158         if (project.getName().toLowerCase().contains("titanium")) {
159             getLog().warn("Project may not build for iPhone as it's name contains the word 'titanium'. ");
160             getLog().warn("See http://jira.appcelerator.org/browse/TIMOB-1082");
161         }
162 
163         if (executeMode.equals("none")) {
164             launchIphoneDistribute(tiBuilder);
165         } else if (executeMode.equals("device")) {
166             String appId = project.getGroupId() + "." + project.getArtifactId();
167             File tiAppFile = new File(getTiProjectDirectory(), "tiapp.xml");
168             try {
169                 if (tiAppFile.exists()) {
170                     Tiapp tiApp = getTitaniumSettings().getTiappFromXML(tiAppFile);
171                     if (tiApp != null && tiApp.getProjectId() != null && tiApp.getProjectId().trim().length() > 0) {
172                         appId = tiApp.getProjectId();
173                     }
174                 }  else {
175                     getLog().warn("Unable to find " + tiAppFile.getAbsolutePath()
176                     + ". uuid will be random.");
177                 }
178 
179             } catch (Throwable t) {
180                 getLog().error("Error while parsing " + tiAppFile.getAbsolutePath()
181                 + ". The application uuid will be random ", t);
182             }
183             tiBuilder.launchIphoneDevice(getIosVersion(),
184                     getTiProjectDirectory(),
185                     appId,
186                     project.getName(),
187                     getTitaniumSettings().getIosDevelopmentProvisioningProfile(),
188                     getTitaniumSettings().getIosDevelopmentCertificate(),
189                     getVirtualDevice().getFamily(),
190                     getLog());
191         } else if (executeMode.equals("virtual")) {
192             launchIphoneEmulator(tiBuilder);
193         }
194     }
195 
196     private void launchIphoneEmulator(TitaniumBuilder tiBuilder) throws MojoFailureException, MojoExecutionException {
197         tiBuilder.launchIphoneEmulator(getIosVersion(),
198                 getTiProjectDirectory().getAbsolutePath(),
199                 project.getName(),
200                 platform,
201                 virtualDevice.getFamily(),
202                 getLog());
203     }
204 
205 
206     private void launchIphoneDistribute(TitaniumBuilder tiBuilder) throws MojoFailureException, MojoExecutionException {
207         String appId = project.getGroupId() + "." + project.getArtifactId();
208         File targetDir = new File(outputDirectory, platform + "-bin");
209 
210         File tiAppFile = new File(getTiProjectDirectory(), "tiapp.xml");
211         try {
212             if (tiAppFile.exists()) {
213                 Tiapp tiApp = getTitaniumSettings().getTiappFromXML(tiAppFile);
214                 if (tiApp != null && tiApp.getProjectId() != null && tiApp.getProjectId().trim().length() > 0) {
215                     appId = tiApp.getProjectId();
216                 }
217             } else {
218                 getLog().warn("Unable to find " + tiAppFile.getAbsolutePath()
219                 + ". uuid will be random.");
220             }
221 
222         } catch (Throwable t) {
223             getLog().error("Error while parsing " + tiAppFile.getAbsolutePath()
224             + ". The application uuid will be random ");
225         }
226 
227         tiBuilder.launchIphoneDistribute(getIosVersion(),
228                 getTiProjectDirectory(),
229                 appId,
230                 project.getName(),
231                 getTitaniumSettings().getIosDistributionProvisioningProfile(),
232                 getTitaniumSettings().getIosDistributionCertificate(),
233                 targetDir,
234                 virtualDevice.getFamily(),
235                 getLog());
236     }
237 
238 
239 }