]> git.argeo.org Git - gpl/argeo-slc.git/blob - EquinoxExecMojo.java
ea75a22838a001497edb694eb11666a981d19647
[gpl/argeo-slc.git] / EquinoxExecMojo.java
1 package org.argeo.slc.maven.plugins.osgi;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import org.apache.maven.artifact.Artifact;
13 import org.apache.maven.plugin.MojoExecutionException;
14 import org.apache.maven.plugin.MojoFailureException;
15 import org.argeo.slc.maven.plugin.MavenDependencyManager;
16 import org.argeo.slc.maven.plugin.SystemCall;
17
18 /**
19 * @goal equinox
20 * */
21 public class EquinoxExecMojo extends AbstractOsgiMojo {
22 /** @component */
23 private MavenDependencyManager mavenDependencyManager;
24
25 /**
26 * Equinox artifact id
27 *
28 * @parameter expression="${equinoxArtifactId}"
29 * default-value="org.eclipse.osgi"
30 * @required
31 */
32 protected String equinoxArtifactId;
33
34 /**
35 * OSGIBoot artifact id
36 *
37 * @parameter expression="${osgiBootArtifactId}"
38 * default-value="org.argeo.slc.osgiboot"
39 * @required
40 */
41 protected String osgiBootArtifactId;
42
43 /**
44 * Java executable
45 *
46 * @parameter expression="${jvm}" default-value="${java.home}/bin/java"
47 * @required
48 */
49 protected String jvm;
50
51 /**
52 * JVM arguments
53 *
54 * @parameter alias="${jvmArgs}"
55 */
56 protected String[] jvmArgs;
57
58 /**
59 * JVM arguments to append
60 *
61 * @parameter alias="${jvmArgsToAppend}"
62 */
63 protected String[] jvmArgsToAppend;
64
65 protected String[] defaultJvmArgs = { "-Xmx128m" };
66
67 /**
68 * Debug port (0 deactivate)
69 *
70 * @parameter expression="${debug}" default-value="0"
71 * @required
72 */
73 protected String debug;
74
75 /**
76 * Equinox args
77 *
78 * @parameter alias="${args}"
79 */
80 protected String[] args;
81
82 /**
83 * Equinox args to append
84 *
85 * @parameter alias="${argsToAppend}"
86 */
87 protected String[] argsToAppend;
88
89 protected String[] defaultArgs = { "-console", "-configuration", "conf",
90 "-data", "data" };
91
92 /**
93 * JVM system properties
94 *
95 * @parameter alias="${systemProperties}"
96 */
97 protected Map systemProperties;
98
99 /**
100 * Execution directory
101 *
102 * @parameter expression="${execDir}"
103 * default-value="${project.build.directory}/exec"
104 * @required
105 */
106 protected File execDir;
107
108 public void execute() throws MojoExecutionException, MojoFailureException {
109 if ("bundles".equals(project.getArtifact().getType())) {
110 System.out.println("Skip artifact of type bundles "
111 + artifactToString(project.getArtifact()));
112 return;
113 }
114
115 try {
116 Artifact equinoxArtifact = null;
117 Artifact osgiBootArtifact = null;
118
119 Set dependencies = mavenDependencyManager
120 .getTransitiveProjectDependencies(project, remoteRepos,
121 local);
122
123 StringBuffer osgiLocations = new StringBuffer();
124 List bundleArtifacts = new ArrayList();
125 boolean first = true;
126 for (Iterator it = dependencies.iterator(); it.hasNext();) {
127 Artifact depArtifact = (Artifact) it.next();
128 printArtifact(depArtifact);
129
130 if (depArtifact.getArtifactId().equals(equinoxArtifactId)) {
131 equinoxArtifact = depArtifact;
132 } else if (depArtifact.getArtifactId().equals(
133 osgiBootArtifactId)) {
134 osgiBootArtifact = depArtifact;
135 } else {
136 bundleArtifacts.add(depArtifact);
137
138 if ("jar".equals(depArtifact.getType())) {
139 // Add to OSGi locations
140 if (first)
141 first = false;
142 else
143 osgiLocations.append(File.pathSeparatorChar);
144
145 osgiLocations.append(depArtifact.getFile()
146 .getCanonicalPath().replace(File.separatorChar,
147 '/'));
148 }
149 }
150 }
151
152 // Set defaults
153 if (jvmArgs == null)
154 jvmArgs = defaultJvmArgs;
155 if (args == null)
156 args = defaultArgs;
157 if (systemProperties == null)
158 systemProperties = new HashMap();
159
160 if (!execDir.exists())
161 execDir.mkdirs();
162
163 // Build command
164 List cmdList = new ArrayList();
165 // JVM
166 cmdList.add(jvm);
167 // JVM arguments
168 cmdList.addAll(Arrays.asList(jvmArgs));
169
170 if (!"0".equals(debug))
171 cmdList
172 .add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
173 + debug);
174
175 if (jvmArgsToAppend != null)
176 cmdList.addAll(Arrays.asList(jvmArgsToAppend));
177
178 // System properties
179 if (!systemProperties.containsKey("osgi.bundles"))
180 cmdList.add("-Dosgi.bundles="
181 + osgiBootArtifact.getFile().getCanonicalPath()
182 + "@start");
183 if (!systemProperties.containsKey("slc.osgi.locations"))
184 cmdList.add("-Dslc.osgi.locations=" + osgiLocations);
185 for (Iterator keys = systemProperties.keySet().iterator(); keys
186 .hasNext();) {
187 Object key = keys.next();
188 Object value = systemProperties.get(key);
189 String strValue = null;
190 if (value != null) {
191 strValue = value.toString().trim();
192 strValue = strValue.replaceAll("\n", "");
193 strValue = strValue.replaceAll("\t", "");
194 }
195 cmdList.add("-D" + key + "=" + strValue);
196 }
197
198 // Equinox jar
199 cmdList.add("-jar");
200 cmdList.add(equinoxArtifact.getFile().getCanonicalPath());
201
202 // Program arguments
203 cmdList.addAll(Arrays.asList(args));
204 if (argsToAppend != null)
205 cmdList.addAll(Arrays.asList(argsToAppend));
206
207 String[] cmd = (String[]) cmdList.toArray(new String[0]);
208
209 System.out.println("\nExecute command:\n");
210 for (int i = 0; i < cmd.length; i++) {
211 boolean containsSpace = (cmd[i].indexOf(' ') >= 0)
212 || (cmd[i].indexOf('\t') >= 0);
213 if (containsSpace)
214 System.out.print('\"');
215 System.out.print(cmd[i]);
216 if (containsSpace)
217 System.out.print('\"');
218 System.out.print(' ');
219 }
220 System.out.println("\n");
221
222 SystemCall systemCall = new SystemCall(execDir.getCanonicalPath(),
223 cmd, true);
224 systemCall.run();
225
226 } catch (Exception e) {
227 throw new MojoExecutionException("Cannot execute Equinox", e);
228 }
229
230 }
231
232 protected static void printArtifact(Artifact artifact) {
233 System.out.println(artifactToString(artifact));
234 }
235
236 protected static String artifactToString(Artifact artifact) {
237 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
238 + artifact.getType() + ":" + artifact.getClassifier() + ":"
239 + artifact.getVersion() + " (" + artifact.getFile() + ")";
240 }
241
242 }