View Javadoc
1   package org.andromda.maven.plugin.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 or directory
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              if (sourceFile.isDirectory())
47              {
48                  FileUtils.copyDirectory(
49                          sourceFile,
50                          destFile);
51              }
52              else
53              {
54                  FileUtils.copyFile(
55                          sourceFile,
56                          destFile);
57              }
58          }
59          catch (Exception e)
60          {
61              throw new MojoExecutionException("Error copying file from " + sourceFile + " to " + destFile, e);
62          }
63      }
64  
65      /**
66       * Unpacks the archive file
67       *
68       * @param file File to be unpacked.
69       * @param location Location where to put the unpacked files.
70       * @throws MojoExecutionException
71       */
72      protected void unpack(File file, File location)
73          throws MojoExecutionException
74      {
75          final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase();
76          try
77          {
78              location.mkdirs();
79              UnArchiver unArchiver;
80              unArchiver = archiverManager.getUnArchiver(archiveExt);
81              unArchiver.setSourceFile(file);
82              unArchiver.setDestDirectory(location);
83              unArchiver.extract();
84          }
85          catch (NoSuchArchiverException e)
86          {
87              throw new MojoExecutionException("Unknown archiver type", e);
88          }
89          catch (ArchiverException e)
90          {
91              e.printStackTrace();
92              throw new MojoExecutionException("Error unpacking file: " +
93                      file + " to: " + location + "\r\n" + e.toString(), e);
94          }
95          catch (Exception e)
96          {
97              e.printStackTrace();
98              throw new MojoExecutionException("Error unpacking file: " +
99                      file.getAbsolutePath() + " to: " + location + "\r\n" + e.toString(), e);
100         }
101     }
102 }