View Javadoc
1   package org.andromda.maven.plugin.cartridge.site;
2   
3   import java.io.File;
4   import org.apache.commons.io.FileUtils;
5   import org.apache.commons.io.FilenameUtils;
6   import org.apache.maven.plugin.AbstractMojo;
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.codehaus.plexus.archiver.ArchiverException;
9   import org.codehaus.plexus.archiver.UnArchiver;
10  import org.codehaus.plexus.archiver.manager.ArchiverManager;
11  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
12  
13  /**
14   * Abstract parent class used by site mojos to perform helper functions like copy
15   * and unpack.
16   *
17   * @author vancek
18   *
19   */
20  public abstract class AbstractSiteMojo
21      extends AbstractMojo
22  {
23      /**
24       * To look up Archiver/UnArchiver implementations
25       *
26       * @component role="org.codehaus.plexus.archiver.manager.ArchiverManager"
27       * @required
28       * @readonly
29       */
30      protected ArchiverManager archiverManager;
31  
32      /**
33       * Does the actual copy of the file and logging.
34       *
35       * @param sourceFile represents the file to copy.
36       * @param destFile file name of destination file.
37       *
38       * @throws MojoExecutionException with a message if an error occurs.
39       */
40      public void copyFile(File sourceFile, File destFile)
41          throws MojoExecutionException
42      {
43          try
44          {
45              this.getLog().info("Copying " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath());
46              FileUtils.copyFile(sourceFile, destFile);
47          }
48          catch (Exception e)
49          {
50              throw new MojoExecutionException("Error copying file from " + sourceFile + " to " + destFile, e);
51          }
52      }
53  
54      /**
55       * Unpacks the archive file.
56       *
57       * @param file File to be unpacked.
58       * @param location Location where to put the unpacked files.
59       * @throws MojoExecutionException
60       */
61      protected void unpack(File file, File location)
62          throws MojoExecutionException
63      {
64          final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase();
65          try
66          {
67              location.mkdirs();
68              UnArchiver unArchiver;
69              unArchiver = archiverManager.getUnArchiver(archiveExt);
70              unArchiver.setSourceFile(file);
71              unArchiver.setDestDirectory(location);
72              unArchiver.extract();
73          }
74          catch (NoSuchArchiverException e)
75          {
76              throw new MojoExecutionException("Unknown archiver type", e);
77          }
78          catch (ArchiverException e)
79          {
80              e.printStackTrace();
81              throw new MojoExecutionException("Error unpacking file: " +
82                      file + " to: " + location + "\r\n" + e.toString(), e);
83          }
84          catch (Exception e)
85          {
86              e.printStackTrace();
87              throw new MojoExecutionException("Error unpacking file: " +
88                      file.getAbsolutePath() + " to: " + location + "\r\n" + e.toString(), e);
89          }
90      }
91  }