001package org.andromda.maven.plugin.cartridge.site; 002 003import java.io.File; 004import org.apache.commons.io.FileUtils; 005import org.apache.commons.io.FilenameUtils; 006import org.apache.maven.plugin.AbstractMojo; 007import org.apache.maven.plugin.MojoExecutionException; 008import org.codehaus.plexus.archiver.ArchiverException; 009import org.codehaus.plexus.archiver.UnArchiver; 010import org.codehaus.plexus.archiver.manager.ArchiverManager; 011import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; 012 013/** 014 * Abstract parent class used by site mojos to perform helper functions like copy 015 * and unpack. 016 * 017 * @author vancek 018 * 019 */ 020public abstract class AbstractSiteMojo 021 extends AbstractMojo 022{ 023 /** 024 * To look up Archiver/UnArchiver implementations 025 * 026 * @component role="org.codehaus.plexus.archiver.manager.ArchiverManager" 027 * @required 028 * @readonly 029 */ 030 protected ArchiverManager archiverManager; 031 032 /** 033 * Does the actual copy of the file and logging. 034 * 035 * @param sourceFile represents the file to copy. 036 * @param destFile file name of destination file. 037 * 038 * @throws MojoExecutionException with a message if an error occurs. 039 */ 040 public void copyFile(File sourceFile, File destFile) 041 throws MojoExecutionException 042 { 043 try 044 { 045 this.getLog().info("Copying " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); 046 FileUtils.copyFile(sourceFile, destFile); 047 } 048 catch (Exception e) 049 { 050 throw new MojoExecutionException("Error copying file from " + sourceFile + " to " + destFile, e); 051 } 052 } 053 054 /** 055 * Unpacks the archive file. 056 * 057 * @param file File to be unpacked. 058 * @param location Location where to put the unpacked files. 059 * @throws MojoExecutionException 060 */ 061 protected void unpack(File file, File location) 062 throws MojoExecutionException 063 { 064 final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase(); 065 try 066 { 067 location.mkdirs(); 068 UnArchiver unArchiver; 069 unArchiver = archiverManager.getUnArchiver(archiveExt); 070 unArchiver.setSourceFile(file); 071 unArchiver.setDestDirectory(location); 072 unArchiver.extract(); 073 } 074 catch (NoSuchArchiverException e) 075 { 076 throw new MojoExecutionException("Unknown archiver type", e); 077 } 078 catch (ArchiverException e) 079 { 080 e.printStackTrace(); 081 throw new MojoExecutionException("Error unpacking file: " + 082 file + " to: " + location + "\r\n" + e.toString(), e); 083 } 084 catch (Exception e) 085 { 086 e.printStackTrace(); 087 throw new MojoExecutionException("Error unpacking file: " + 088 file.getAbsolutePath() + " to: " + location + "\r\n" + e.toString(), e); 089 } 090 } 091}