| 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 | 0 | public abstract class AbstractSonicMojo extends AbstractMojo { |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 0 | protected final File deployGenSrcDir = null; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
protected File getTargetTopologyDir() { |
| 37 | |
|
| 38 | 0 | return new File(getOutputDirectory(), "sonicesb/topology"); |
| 39 | |
} |
| 40 | |
|
| 41 | 0 | 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 | 0 | private String systemTempDir = null; |
| 96 | 0 | 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 | 0 | 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 | 0 | return project.getGroupId(); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
protected String getArtifactId() { |
| 155 | |
|
| 156 | 0 | return project.getArtifactId(); |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
protected String getVersion() { |
| 163 | |
|
| 164 | 0 | return project.getVersion(); |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
protected String getFinalAssemblyBaseName() { |
| 171 | |
|
| 172 | 0 | return getFinalAssemblyBaseName(includeVersionInXarName); |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
protected String getFinalAssemblyBaseName(final boolean includeVersion) { |
| 181 | |
|
| 182 | 0 | String classifierSuffix = ""; |
| 183 | |
|
| 184 | 0 | if (getClassifier() != null) { |
| 185 | 0 | if (getClassifier().trim().length() > 0 && !getClassifier().startsWith("-")) { |
| 186 | 0 | classifierSuffix = "-" + getClassifier(); |
| 187 | |
} else { |
| 188 | 0 | classifierSuffix = getClassifier(); |
| 189 | |
} |
| 190 | |
} |
| 191 | |
|
| 192 | 0 | String result = project.getBuild().getFinalName(); |
| 193 | 0 | if (!includeVersion) { |
| 194 | 0 | result = project.getArtifact().getArtifactId(); |
| 195 | |
} |
| 196 | |
|
| 197 | 0 | return result + classifierSuffix; |
| 198 | |
} |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
protected String getFinalAssemblyFileName() { |
| 205 | |
|
| 206 | 0 | String extension = project.getArtifact().getArtifactHandler().getExtension(); |
| 207 | |
|
| 208 | 0 | if (extension == null || "".equals(extension)) { |
| 209 | 0 | extension = project.getPackaging(); |
| 210 | |
} |
| 211 | 0 | return getFinalAssemblyBaseName(true) + "." + extension; |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
protected String getOutputDirectory() { |
| 218 | |
|
| 219 | 0 | return project.getBuild().getDirectory(); |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
protected String getDependencyDirectory() { |
| 226 | |
|
| 227 | 0 | return getOutputDirectory() + "/sonicesb/dependency"; |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
protected String getTestDependencyDirectory() { |
| 234 | |
|
| 235 | 0 | return getOutputDirectory() + "/sonicesb/testDependency"; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
protected File getSonicEsbSourceDirectory() { |
| 242 | |
|
| 243 | 0 | return sonicesbSourceDirectory; |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
protected File getSonicEsbTestSourceDirectory() { |
| 250 | |
|
| 251 | 0 | return sonicesbTestSourceDirectory; |
| 252 | |
} |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
protected String getProjectName() { |
| 259 | |
|
| 260 | 0 | File projectFile = null; |
| 261 | |
|
| 262 | 0 | if (projectName == null) { |
| 263 | |
try { |
| 264 | 0 | projectFile = new File(project.getBasedir(), ".project"); |
| 265 | 0 | if (projectFile.exists() && projectFile.isFile() && projectFile.canRead()) { |
| 266 | 0 | XmlStreamReader reader = new XmlStreamReader(projectFile); |
| 267 | 0 | Xpp3Dom projectDom = Xpp3DomBuilder.build(reader); |
| 268 | 0 | projectName = projectDom.getChild("name").getValue(); |
| 269 | |
} |
| 270 | 0 | } catch (Exception e) { |
| 271 | 0 | getLog().warn("Cannot read project file: " + projectFile.getAbsolutePath(), e); |
| 272 | |
} finally { |
| 273 | 0 | if (projectName == null) { |
| 274 | 0 | projectName = project.getArtifactId() + "-" + getVersion(); |
| 275 | |
} |
| 276 | |
} |
| 277 | 0 | getLog().debug("Resolved project name to: " + projectName); |
| 278 | |
} |
| 279 | 0 | return projectName; |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
public final void execute() throws MojoExecutionException, MojoFailureException { |
| 289 | |
|
| 290 | 0 | getLog().info("========================================================================="); |
| 291 | 0 | getLog().info("Executing Mojo : " + this.getClass().getName()); |
| 292 | 0 | getLog().info("Project : " + getArtifactId()); |
| 293 | 0 | getLog().info("Group : " + getGroupId()); |
| 294 | 0 | getLog().info("Version : " + getVersion()); |
| 295 | 0 | getLog().info("Project Name : " + getProjectName()); |
| 296 | 0 | getLog().info("Source : " + getSonicEsbSourceDirectory().getAbsolutePath()); |
| 297 | 0 | getLog().info("Build : " + getOutputDirectory()); |
| 298 | 0 | getLog().info("BaseDirectory : " + project.getBasedir()); |
| 299 | 0 | getLog().info("SonicFS dir : " + getSonicFSProjectDir()); |
| 300 | |
|
| 301 | 0 | setPluginTempDir(); |
| 302 | 0 | doExecute(); |
| 303 | 0 | resetTempDir(); |
| 304 | |
|
| 305 | 0 | getLog().info("======================= Mojo finished ==================================="); |
| 306 | 0 | } |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
protected String getESBVersion() { |
| 313 | |
|
| 314 | 0 | return project.getProperties().getProperty("esb.version", "2015"); |
| 315 | |
} |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
protected String getSonicFSProjectRoot() { |
| 321 | |
|
| 322 | 0 | return sonicFSProjectRoot; |
| 323 | |
} |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
protected String getSonicFSProjectDir() { |
| 329 | |
|
| 330 | 0 | if (sonicFSProjectRoot == null) { |
| 331 | 0 | sonicFSProjectRoot = "ESBArtifacts"; |
| 332 | |
} |
| 333 | |
|
| 334 | 0 | if (sonicFSProjectDir == null) { |
| 335 | 0 | sonicFSProjectDir = "#{" + project.getGroupId() + "}/" + project.getArtifactId() + "/" + project.getVersion(); |
| 336 | |
} |
| 337 | |
|
| 338 | 0 | return sonicFSProjectRoot + (sonicFSProjectRoot.endsWith("/") ? "" : "/") |
| 339 | |
+ RegexUtil.applyReplaceFunction(sonicFSProjectDir, ".", "/"); |
| 340 | |
} |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
protected String getSonicFSResourcesDir() { |
| 346 | |
|
| 347 | 0 | return sonicFSResourcesRoot; |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
protected String getSDMDir() { |
| 354 | |
|
| 355 | 0 | 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 | 0 | systemTempDir = System.getProperty("java.io.tmpdir"); |
| 372 | 0 | String pluginDirName = getGroupId() + "--" + getArtifactId() + "--" + getVersion(); |
| 373 | 0 | pluginDirName = "mvnesbplugin" + pluginDirName.hashCode() + System.currentTimeMillis(); |
| 374 | 0 | File f = new File(systemTempDir, pluginDirName); |
| 375 | 0 | f.mkdirs(); |
| 376 | 0 | pluginTempDir = f.getAbsolutePath(); |
| 377 | 0 | System.setProperty("java.io.tmpdir", pluginTempDir); |
| 378 | 0 | } |
| 379 | |
|
| 380 | |
private void resetTempDir() { |
| 381 | |
|
| 382 | 0 | System.setProperty("java.io.tmpdir", systemTempDir); |
| 383 | |
|
| 384 | 0 | getLog().debug("Deleting directory : " + pluginTempDir); |
| 385 | |
try { |
| 386 | 0 | FileUtils.deleteDirectory(new File(pluginTempDir)); |
| 387 | 0 | } catch (IOException ioe) { |
| 388 | 0 | getLog().warn("Temporary directory could not be deleted: " + pluginTempDir); |
| 389 | 0 | } |
| 390 | 0 | } |
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
protected String getSonicEsbSourceDirectoryFromBase() { |
| 396 | |
|
| 397 | 0 | return getSonicEsbSourceDirectory().getAbsolutePath().replace(project.getBasedir().getAbsolutePath(), "") |
| 398 | |
.replaceAll("\\\\", "/"); |
| 399 | |
} |
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
protected String getClassifier() { |
| 406 | |
|
| 407 | 0 | return classifier; |
| 408 | |
} |
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
protected String getPackageXarDir() { |
| 416 | |
|
| 417 | 0 | return getOutputDirectory() + "/sonicesb/packageXar/fileSystem"; |
| 418 | |
} |
| 419 | |
|
| 420 | |
protected String getPackageXarFileDir() { |
| 421 | 0 | return getOutputDirectory() + File.separator + "sonicesb" + File.separator + "packageXar"; |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
protected String getInlineESBDir() { |
| 430 | |
|
| 431 | 0 | return getOutputDirectory() + "/sonicesb/packageXar/inlineSystem"; |
| 432 | |
} |
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
protected void copyJarFiles() throws Exception { |
| 439 | |
|
| 440 | 0 | getLog().info("Copying jar files into xar archive ..."); |
| 441 | |
|
| 442 | 0 | DependencyAnalyzer da = new DependencyAnalyzer(getDependencyDirectory()); |
| 443 | 0 | for (String jarFile : da.getUnpackagedJarFiles()) { |
| 444 | 0 | getLog().info("Copying: " + getDependencyDirectory() + "/" + jarFile); |
| 445 | 0 | File src = new File(da.getJarDependencyDir(), jarFile); |
| 446 | 0 | File dest = new File(getPackageXarDir(), "SonicFS/" + getSonicFSResourcesDir() + "/" + jarFile); |
| 447 | 0 | FileUtils.copyFile(src, dest); |
| 448 | 0 | } |
| 449 | 0 | getLog().debug("Leaving copyJarFile"); |
| 450 | 0 | } |
| 451 | |
|
| 452 | |
} |