View Javadoc

1   /**
2    * Pyx4me framework
3    * Copyright (C) 2006-2008 pyx4j.com.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing,
12   * software distributed under the License is distributed on an
13   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   * KIND, either express or implied.  See the License for the
15   * specific language governing permissions and limitations
16   * under the License.
17   * 
18   * @author vlads
19   * @version $Id: GammuDeployMojo.java 2066 2008-06-21 23:16:18Z vlads $
20   */
21  package com.pyx4me.maven.gammu;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  
33  /**
34   * The gammu:deploy task is an wrapper for gammu.exe to install application.
35   * 
36   * @author vlads
37   * 
38   * @goal deploy
39   * @description Install the *.jar/*.jad file pair of a midlet in the application
40   *              or game menu of the nokia phone
41   */
42  
43  public class GammuDeployMojo extends AbstractMojo {
44  
45  	/**
46  	 * Specifies specifies which config section to use.
47  	 * 
48  	 * @parameter expression="${gammu.c}" default-value="1"
49  	 */
50  	protected String confign = "1";
51  
52  	/**
53  	 * Specifies name of folder Application|Game.
54  	 * 
55  	 * @parameter expression="${gammu.section}" default-value="Application"
56  	 */
57  	private String appType = "Application";
58  
59  	/**
60  	 * Specifies the loaction of gammu executable. Default is taken from maven
61  	 * repository.
62  	 * 
63  	 * @parameter
64  	 */
65  	private File gammuexe;
66  
67  	/**
68  	 * Gammu setting configuration file. Copied to outputDirectory (Current
69  	 * directory for execution).
70  	 * 
71  	 * @parameter
72  	 */
73  	private File gammurc;
74  
75  	/**
76  	 * Directory containing the generated JAR.
77  	 * 
78  	 * @parameter expression="${project.build.directory}"
79  	 * @required
80  	 */
81  	protected File outputDirectory;
82  
83  	/**
84  	 * The base name of the JAR/JAD files. Classifier would be appended.
85  	 * 
86  	 * @parameter expression="${project.build.finalName}"
87  	 * @required
88  	 */
89  	protected String jarfile;
90  
91  	/**
92  	 * Classifier to added to the artifact generated.
93  	 * 
94  	 * @parameter expression="${gammu.classifier}" default-value="me"
95  	 * @required
96  	 */
97  	protected String classifier;
98  
99  	/**
100 	 * Set to false to exclude the classifier from the Artifact final name.
101 	 * Default value is true.
102 	 * 
103 	 * @parameter default-value="true"
104 	 */
105 	protected boolean appendClassifier;
106 
107 	/**
108 	 * @parameter expression="${plugin.artifacts}"
109 	 * @required
110 	 * @readonly
111 	 */
112 	private List pluginArtifacts;
113 
114 	public void execute() throws MojoExecutionException, MojoFailureException {
115 
116 		ExecuteGammu exe = new ExecuteGammu(gammuexe, gammurc, pluginArtifacts);
117 		ArrayList args = new ArrayList();
118 
119 		if (gammurc != null) {
120 			try {
121 				File tmpGammurc;
122 				// tmpGammurc = new File(exe.exe.getParentFile(), "gammurc");
123 				// tmpGammurc = new File(exe.exe.getParentFile(),
124 				// FilenameUtils.getBaseName(exe.exe.getName()) + "rc");
125 				tmpGammurc = new File(outputDirectory, "gammurc");
126 
127 				getLog().info("copy configuration file " + tmpGammurc);
128 				FileUtils.copyFile(gammurc, tmpGammurc);
129 			} catch (IOException e) {
130 				getLog().error("Unable to copy gammurc " + e.getMessage(), e);
131 				throw new MojoExecutionException("Unable to copy gammurc " + e.getMessage(), e);
132 			}
133 		}
134 
135 		args.add(confign);
136 		args.add("--nokiaaddfile");
137 		args.add(appType);
138 		args.add(new File(outputDirectory, getArtifactName(jarfile, classifier)).getAbsolutePath());
139 		args.add("-overwrite");
140 
141 		getLog().info("execute " + exe.exe + " " + args.toString());
142 		if (!exe.run((String[]) args.toArray(new String[args.size()]), outputDirectory)) {
143 			throw new MojoExecutionException("Gammu execution error");
144 		}
145 	}
146 
147 	private boolean useArtifactClassifier(String classifier) {
148 		return appendClassifier && ((classifier != null) && (classifier.length() > 0) && (!"none".equals(classifier)));
149 	}
150 
151 	protected String getArtifactName(String finalName, String classifier) {
152 		if (!useArtifactClassifier(classifier)) {
153 			return finalName;
154 		}
155 		return finalName + "-" + classifier;
156 	}
157 }