1 package com.aurea.maven.plugins.sonic;
2
3 import java.io.File;
4
5 import java.io.IOException;
6
7 import org.apache.maven.plugin.AbstractMojo;
8 import org.apache.maven.plugin.MojoExecutionException;
9 import org.apache.maven.plugin.MojoFailureException;
10 import org.apache.maven.project.MavenProject;
11 import org.codehaus.plexus.util.FileUtils;
12 import org.codehaus.plexus.util.xml.XmlStreamReader;
13 import org.codehaus.plexus.util.xml.Xpp3Dom;
14 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
15
16 import com.aurea.maven.plugins.sonic.utils.DependencyAnalyzer;
17 import com.aurea.maven.plugins.sonic.utils.RegexUtil;
18
19
20
21
22
23
24
25 public abstract class AbstractSonicMojo extends AbstractMojo {
26
27
28
29
30 protected final File deployGenSrcDir = null;
31
32
33
34
35
36 protected File getTargetTopologyDir() {
37
38 return new File(getOutputDirectory(), "sonicesb/topology");
39 }
40
41 private String sdmDirName = "sdm";
42
43
44
45
46
47
48
49 private boolean includeVersionInXarName;
50
51
52
53
54
55
56
57 private String sonicFSProjectRoot;
58
59
60
61
62
63
64
65
66
67 private String sonicFSProjectDir;
68
69
70
71
72
73
74
75
76 protected String defaultTopologyPrefix;
77
78
79
80
81
82
83
84 private String sonicFSResourcesRoot;
85
86
87
88
89
90
91
92
93 protected MavenProject project;
94
95 private String systemTempDir = null;
96 private String pluginTempDir = null;
97
98
99
100
101
102
103 protected File sonicesbSourceDirectory;
104
105
106
107
108
109
110 protected File sonicesbTestSourceDirectory;
111
112
113
114
115
116
117 protected File sonicesbGeneratedSourceDirectory;
118
119 private String projectName = null;
120
121
122
123
124 protected static final String XARS_DIR_NAME = "xars";
125
126
127
128
129 protected static final String TAILOR_DIR_NAME = "tailor";
130
131
132
133
134 protected static final String SDM_SNIPPET_DIR_NAME = "sdm-snippets";
135
136
137
138
139
140
141 private String classifier;
142
143
144
145
146 protected String getGroupId() {
147
148 return project.getGroupId();
149 }
150
151
152
153
154 protected String getArtifactId() {
155
156 return project.getArtifactId();
157 }
158
159
160
161
162 protected String getVersion() {
163
164 return project.getVersion();
165 }
166
167
168
169
170 protected String getFinalAssemblyBaseName() {
171
172 return getFinalAssemblyBaseName(includeVersionInXarName);
173 }
174
175
176
177
178
179
180 protected String getFinalAssemblyBaseName(final boolean includeVersion) {
181
182 String classifierSuffix = "";
183
184 if (getClassifier() != null) {
185 if (getClassifier().trim().length() > 0 && !getClassifier().startsWith("-")) {
186 classifierSuffix = "-" + getClassifier();
187 } else {
188 classifierSuffix = getClassifier();
189 }
190 }
191
192 String result = project.getBuild().getFinalName();
193 if (!includeVersion) {
194 result = project.getArtifact().getArtifactId();
195 }
196
197 return result + classifierSuffix;
198 }
199
200
201
202
203
204 protected String getFinalAssemblyFileName() {
205
206 String extension = project.getArtifact().getArtifactHandler().getExtension();
207
208 if (extension == null || "".equals(extension)) {
209 extension = project.getPackaging();
210 }
211 return getFinalAssemblyBaseName(true) + "." + extension;
212 }
213
214
215
216
217 protected String getOutputDirectory() {
218
219 return project.getBuild().getDirectory();
220 }
221
222
223
224
225 protected String getDependencyDirectory() {
226
227 return getOutputDirectory() + "/sonicesb/dependency";
228 }
229
230
231
232
233 protected String getTestDependencyDirectory() {
234
235 return getOutputDirectory() + "/sonicesb/testDependency";
236 }
237
238
239
240
241 protected File getSonicEsbSourceDirectory() {
242
243 return sonicesbSourceDirectory;
244 }
245
246
247
248
249 protected File getSonicEsbTestSourceDirectory() {
250
251 return sonicesbTestSourceDirectory;
252 }
253
254
255
256
257
258 protected String getProjectName() {
259
260 File projectFile = null;
261
262 if (projectName == null) {
263 try {
264 projectFile = new File(project.getBasedir(), ".project");
265 if (projectFile.exists() && projectFile.isFile() && projectFile.canRead()) {
266 XmlStreamReader reader = new XmlStreamReader(projectFile);
267 Xpp3Dom projectDom = Xpp3DomBuilder.build(reader);
268 projectName = projectDom.getChild("name").getValue();
269 }
270 } catch (Exception e) {
271 getLog().warn("Cannot read project file: " + projectFile.getAbsolutePath(), e);
272 } finally {
273 if (projectName == null) {
274 projectName = project.getArtifactId() + "-" + getVersion();
275 }
276 }
277 getLog().debug("Resolved project name to: " + projectName);
278 }
279 return projectName;
280 }
281
282
283
284
285
286
287
288 public final void execute() throws MojoExecutionException, MojoFailureException {
289
290 getLog().info("=========================================================================");
291 getLog().info("Executing Mojo : " + this.getClass().getName());
292 getLog().info("Project : " + getArtifactId());
293 getLog().info("Group : " + getGroupId());
294 getLog().info("Version : " + getVersion());
295 getLog().info("Project Name : " + getProjectName());
296 getLog().info("Source : " + getSonicEsbSourceDirectory().getAbsolutePath());
297 getLog().info("Build : " + getOutputDirectory());
298 getLog().info("BaseDirectory : " + project.getBasedir());
299 getLog().info("SonicFS dir : " + getSonicFSProjectDir());
300
301 setPluginTempDir();
302 doExecute();
303 resetTempDir();
304
305 getLog().info("======================= Mojo finished ===================================");
306 }
307
308
309
310
311
312 protected String getESBVersion() {
313
314 return project.getProperties().getProperty("esb.version", "2015");
315 }
316
317
318
319
320 protected String getSonicFSProjectRoot() {
321
322 return sonicFSProjectRoot;
323 }
324
325
326
327
328 protected String getSonicFSProjectDir() {
329
330 if (sonicFSProjectRoot == null) {
331 sonicFSProjectRoot = "ESBArtifacts";
332 }
333
334 if (sonicFSProjectDir == null) {
335 sonicFSProjectDir = "#{" + project.getGroupId() + "}/" + project.getArtifactId() + "/" + project.getVersion();
336 }
337
338 return sonicFSProjectRoot + (sonicFSProjectRoot.endsWith("/") ? "" : "/")
339 + RegexUtil.applyReplaceFunction(sonicFSProjectDir, ".", "/");
340 }
341
342
343
344
345 protected String getSonicFSResourcesDir() {
346
347 return sonicFSResourcesRoot;
348 }
349
350
351
352
353 protected String getSDMDir() {
354
355 return getOutputDirectory() + "/" + sdmDirName;
356 }
357
358
359
360
361
362
363
364
365
366
367 protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
368
369 private void setPluginTempDir() {
370
371 systemTempDir = System.getProperty("java.io.tmpdir");
372 String pluginDirName = getGroupId() + "--" + getArtifactId() + "--" + getVersion();
373 pluginDirName = "mvnesbplugin" + pluginDirName.hashCode() + System.currentTimeMillis();
374 File f = new File(systemTempDir, pluginDirName);
375 f.mkdirs();
376 pluginTempDir = f.getAbsolutePath();
377 System.setProperty("java.io.tmpdir", pluginTempDir);
378 }
379
380 private void resetTempDir() {
381
382 System.setProperty("java.io.tmpdir", systemTempDir);
383
384 getLog().debug("Deleting directory : " + pluginTempDir);
385 try {
386 FileUtils.deleteDirectory(new File(pluginTempDir));
387 } catch (IOException ioe) {
388 getLog().warn("Temporary directory could not be deleted: " + pluginTempDir);
389 }
390 }
391
392
393
394
395 protected String getSonicEsbSourceDirectoryFromBase() {
396
397 return getSonicEsbSourceDirectory().getAbsolutePath().replace(project.getBasedir().getAbsolutePath(), "")
398 .replaceAll("\\\\", "/");
399 }
400
401
402
403
404
405 protected String getClassifier() {
406
407 return classifier;
408 }
409
410
411
412
413
414
415 protected String getPackageXarDir() {
416
417 return getOutputDirectory() + "/sonicesb/packageXar/fileSystem";
418 }
419
420 protected String getPackageXarFileDir() {
421 return getOutputDirectory() + File.separator + "sonicesb" + File.separator + "packageXar";
422 }
423
424
425
426
427
428
429 protected String getInlineESBDir() {
430
431 return getOutputDirectory() + "/sonicesb/packageXar/inlineSystem";
432 }
433
434
435
436
437
438 protected void copyJarFiles() throws Exception {
439
440 getLog().info("Copying jar files into xar archive ...");
441
442 DependencyAnalyzer da = new DependencyAnalyzer(getDependencyDirectory());
443 for (String jarFile : da.getUnpackagedJarFiles()) {
444 getLog().info("Copying: " + getDependencyDirectory() + "/" + jarFile);
445 File src = new File(da.getJarDependencyDir(), jarFile);
446 File dest = new File(getPackageXarDir(), "SonicFS/" + getSonicFSResourcesDir() + "/" + jarFile);
447 FileUtils.copyFile(src, dest);
448 }
449 getLog().debug("Leaving copyJarFile");
450 }
451
452 }