]> git.argeo.org Git - gpl/argeo-slc.git/blob - EquinoxExecMojo.java
c483c5250a3fd412845d4a5c3881ee1d668925c4
[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 protected String[] defaultJvmArgs = { "-Xmx128m" };
59
60 /**
61 * Equinox args
62 *
63 * @parameter alias="${args}"
64 */
65 protected String[] args;
66
67 protected String[] defaultArgs = { "-console", "-configuration", "conf",
68 "-data", "data" };
69
70 /**
71 * JVM system properties
72 *
73 * @parameter alias="${systemProperties}"
74 */
75 protected Map systemProperties;
76
77 /**
78 * Execution directory
79 *
80 * @parameter expression="${execDir}"
81 * default-value="${project.build.directory}/exec"
82 * @required
83 */
84 protected File execDir;
85
86 public void execute() throws MojoExecutionException, MojoFailureException {
87 if ("bundles".equals(project.getArtifact().getType())) {
88 System.out.println("Skip artifact of type bundles "
89 + artifactToString(project.getArtifact()));
90 return;
91 }
92
93 try {
94 Artifact equinoxArtifact = null;
95 Artifact osgiBootArtifact = null;
96
97 Set dependencies = mavenDependencyManager
98 .getTransitiveProjectDependencies(project, remoteRepos,
99 local);
100
101 StringBuffer osgiLocations = new StringBuffer();
102 List bundleArtifacts = new ArrayList();
103 boolean first = true;
104 for (Iterator it = dependencies.iterator(); it.hasNext();) {
105 Artifact depArtifact = (Artifact) it.next();
106 printArtifact(depArtifact);
107
108 if (depArtifact.getArtifactId().equals(equinoxArtifactId)) {
109 equinoxArtifact = depArtifact;
110 } else if (depArtifact.getArtifactId().equals(
111 osgiBootArtifactId)) {
112 osgiBootArtifact = depArtifact;
113 } else {
114 bundleArtifacts.add(depArtifact);
115
116 if ("jar".equals(depArtifact.getType())) {
117 // Add to OSGi locations
118 if (first)
119 first = false;
120 else
121 osgiLocations.append(File.pathSeparatorChar);
122
123 osgiLocations.append(depArtifact.getFile()
124 .getCanonicalPath().replace(File.separatorChar,
125 '/'));
126 }
127 }
128 }
129
130 // Set defaults
131 if (jvmArgs == null)
132 jvmArgs = defaultJvmArgs;
133 if (args == null)
134 args = defaultArgs;
135 if (systemProperties == null)
136 systemProperties = new HashMap();
137
138 if (!execDir.exists())
139 execDir.mkdirs();
140
141 // Build command
142 List cmdList = new ArrayList();
143 cmdList.add(jvm);
144 cmdList.addAll(Arrays.asList(jvmArgs));
145 if (!systemProperties.containsKey("osgi.bundles"))
146 cmdList.add("-Dosgi.bundles="
147 + osgiBootArtifact.getFile().getCanonicalPath()
148 + "@start");
149 if (!systemProperties.containsKey("slc.osgi.locations"))
150 cmdList.add("-Dslc.osgi.locations=" + osgiLocations);
151 for (Iterator keys = systemProperties.keySet().iterator(); keys
152 .hasNext();) {
153 Object key = keys.next();
154 Object value = systemProperties.get(key);
155 String strValue = null;
156 if (value != null) {
157 strValue = value.toString().trim();
158 strValue = strValue.replaceAll("\n", "");
159 strValue = strValue.replaceAll("\t", "");
160 }
161 cmdList.add("-D" + key + "=" + strValue);
162 }
163 cmdList.add("-jar");
164 cmdList.add(equinoxArtifact.getFile().getCanonicalPath());
165 cmdList.addAll(Arrays.asList(args));
166
167 String[] cmd = (String[]) cmdList.toArray(new String[0]);
168
169 System.out.println("\nExecute command:\n");
170 for (int i = 0; i < cmd.length; i++) {
171 boolean containsSpace = (cmd[i].indexOf(' ') >= 0)
172 || (cmd[i].indexOf('\t') >= 0);
173 if (containsSpace)
174 System.out.print('\"');
175 System.out.print(cmd[i]);
176 if (containsSpace)
177 System.out.print('\"');
178 System.out.print(' ');
179 }
180 System.out.println("\n");
181
182 SystemCall systemCall = new SystemCall(execDir.getCanonicalPath(),
183 cmd, true);
184 systemCall.run();
185
186 } catch (Exception e) {
187 throw new MojoExecutionException("Cannot execute Equinox", e);
188 }
189
190 }
191
192 protected static void printArtifact(Artifact artifact) {
193 System.out.println(artifactToString(artifact));
194 }
195
196 protected static String artifactToString(Artifact artifact) {
197 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
198 + artifact.getType() + ":" + artifact.getClassifier() + ":"
199 + artifact.getVersion() + " (" + artifact.getFile() + ")";
200 }
201
202 }