View Javadoc

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  * Execute this mojo and fix the dependency copy
21  *
22  * @goal execute-mojo
23  * @requiresDependencyResolution compile
24  */
25  public abstract class AbstractSonicMojo extends AbstractMojo {
26  
27  	  /**
28  	   * @parameter default-value="deploy/generated-src/xar"
29  	   */
30  	  protected final File deployGenSrcDir     = null;
31  	  
32  
33  		/**
34  		 * @return location of the SDM directory
35  		 */
36  		protected File getTargetTopologyDir() {
37  
38  			return new File(getOutputDirectory(), "sonicesb/topology");
39  		}
40  	  
41    private String                sdmDirName           = "sdm";
42  
43    /**
44     * Defines whether the version classifier shoudl be included in XAR names.
45     * 
46     * @parameter default-value="true" property="sonicesb.versionedXar"
47     * @required
48     */
49    private boolean               includeVersionInXarName;
50  
51    /**
52     * The SonicFS directory where all maven generated sources go.
53     * 
54     * @parameter default-value="ESBArtifacts" property="sonicesb.projectRoot"
55     * @required
56     */
57    private String                sonicFSProjectRoot;
58  
59    /**
60     * The directory (in projectRoot) where this project's files will be stored. This expression alone allows for the #{}
61     * function, which replaces the '.' character by '/' in it's argument.
62     * 
63     * @parameter default-value="#{${project.groupId}}/${project.artifactId}/${project.version}"
64     *            property="sonicesb.projectDir"
65     * @required
66     */
67    private String                sonicFSProjectDir;
68    
69  	/**
70  	 * The defaultTopologyPrefix.
71  	 * 
72  	 * @parameter default-value="default"
73  	 *            property="sonicesb.defaultTopologyPrefix"
74  	 * @required
75  	 */
76  	protected String defaultTopologyPrefix;
77  
78    /**
79     * The SonicFS directory where all dependency jars go.
80     * 
81     * @parameter default-value="ESBResources" property="sonicesb.resourcesRoot"
82     * @required
83     */
84    private String                sonicFSResourcesRoot;
85  
86    /**
87     * The reference to the Maven project we are building.
88     * 
89     * @parameter property="project"
90     * @required
91     * @readonly
92     */
93    protected MavenProject        project;
94  
95    private String                systemTempDir        = null;
96    private String                pluginTempDir        = null;
97  
98    /**
99     * directory containing the sonicesb sources.
100    * 
101    * @parameter property="sonicesb.sourceDirectory" default-value="src/main/sonicesb"
102    */
103   protected File                sonicesbSourceDirectory;
104 
105   /**
106    * directory containing the sonicesb testing sources.
107    * 
108    * @parameter property="sonicesb.testSourceDirectory" default-value="src/test/sonicesb"
109    */
110   protected File                sonicesbTestSourceDirectory;
111   
112   /**
113    * directory containing the sonicesb generated sources.
114    * 
115    * @parameter property="sonicesb.generatedSourceDirectory" default-value="target/generated-sources/annotations/sonicesb"
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    * Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
138    * 
139    * @parameter
140    */
141   private String                classifier;
142 
143   /**
144    * @return String
145    */
146   protected String getGroupId() {
147 
148     return project.getGroupId();
149   }
150 
151   /**
152    * @return String
153    */
154   protected String getArtifactId() {
155 
156     return project.getArtifactId();
157   }
158 
159   /**
160    * @return String
161    */
162   protected String getVersion() {
163 
164     return project.getVersion();
165   }
166 
167   /**
168    * @return String
169    */
170   protected String getFinalAssemblyBaseName() {
171 
172     return getFinalAssemblyBaseName(includeVersionInXarName);
173   }
174 
175   /**
176    * @param includeVersion
177    *          set to true if the version number should be included in the returned value.
178    * @return String
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    * @return String
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    * @return the build directory (usually /target)
216    */
217   protected String getOutputDirectory() {
218 
219     return project.getBuild().getDirectory();
220   }
221 
222   /**
223    * @return the directory to contain any dependencies
224    */
225   protected String getDependencyDirectory() {
226 
227     return getOutputDirectory() + "/sonicesb/dependency";
228   }
229 
230   /**
231    * @return the directory to contain any dependencies
232    */
233   protected String getTestDependencyDirectory() {
234 
235     return getOutputDirectory() + "/sonicesb/testDependency";
236   }
237 
238   /**
239    * @return the path to the defined sonicesb source directory
240    */
241   protected File getSonicEsbSourceDirectory() {
242 
243     return sonicesbSourceDirectory;
244   }
245 
246   /**
247    * @return the path to the defined sonicesb test source directory
248    */
249   protected File getSonicEsbTestSourceDirectory() {
250 
251     return sonicesbTestSourceDirectory;
252   }
253 
254 
255   /**
256    * @return the name of the eclipse project
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    * @throws MojoExecutionException
284    *           MojoExecutionException
285    * @throws MojoFailureException
286    *           MojoFailureException
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    * @return esb version
311    */
312   protected String getESBVersion() {
313 
314     return project.getProperties().getProperty("esb.version", "2015");
315   }
316 
317   /**
318    * @return location for this project in the SonicFS
319    */
320   protected String getSonicFSProjectRoot() {
321 
322     return sonicFSProjectRoot;
323   }
324 
325   /**
326    * @return location for this project in the SonicFS
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    * @return location of the SonicFS resources directory
344    */
345   protected String getSonicFSResourcesDir() {
346 
347     return sonicFSResourcesRoot;
348   }
349 
350   /**
351    * @return location of the SDM directory
352    */
353   protected String getSDMDir() {
354 
355     return getOutputDirectory() + "/" + sdmDirName;
356   }
357 
358 
359   /**
360    * execute the mojo.
361    * 
362    * @throws MojoExecutionException
363    *           if something goes wrong
364    * @throws MojoFailureException
365    *           if something goes wrong
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    * @return the sonic source directory from the base directory (eg /src/main/sonicesb)
394    */
395   protected String getSonicEsbSourceDirectoryFromBase() {
396 
397     return getSonicEsbSourceDirectory().getAbsolutePath().replace(project.getBasedir().getAbsolutePath(), "")
398         .replaceAll("\\\\", "/");
399   }
400 
401 
402   /**
403    * {@inheritDoc}
404    */
405   protected String getClassifier() {
406 
407     return classifier;
408   }
409 
410   /**
411    * get the temporary location to use while assembling the Maven package.
412    * 
413    * @return a directory for the package xar
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    * get the temporary location to use while assembling the Maven package.
426    * 
427    * @return a directory for the Inline XML stuff
428    */
429   protected String getInlineESBDir() {
430 
431     return getOutputDirectory() + "/sonicesb/packageXar/inlineSystem";
432   }
433 
434   /**
435    * @throws Exception
436    *           Exception
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 }