001package org.andromda.maven.plugin.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 or directory 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 if (sourceFile.isDirectory()) 047 { 048 FileUtils.copyDirectory( 049 sourceFile, 050 destFile); 051 } 052 else 053 { 054 FileUtils.copyFile( 055 sourceFile, 056 destFile); 057 } 058 } 059 catch (Exception e) 060 { 061 throw new MojoExecutionException("Error copying file from " + sourceFile + " to " + destFile, e); 062 } 063 } 064 065 /** 066 * Unpacks the archive file 067 * 068 * @param file File to be unpacked. 069 * @param location Location where to put the unpacked files. 070 * @throws MojoExecutionException 071 */ 072 protected void unpack(File file, File location) 073 throws MojoExecutionException 074 { 075 final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase(); 076 try 077 { 078 location.mkdirs(); 079 UnArchiver unArchiver; 080 unArchiver = archiverManager.getUnArchiver(archiveExt); 081 unArchiver.setSourceFile(file); 082 unArchiver.setDestDirectory(location); 083 unArchiver.extract(); 084 } 085 catch (NoSuchArchiverException e) 086 { 087 throw new MojoExecutionException("Unknown archiver type", e); 088 } 089 catch (ArchiverException e) 090 { 091 e.printStackTrace(); 092 throw new MojoExecutionException("Error unpacking file: " + 093 file + " to: " + location + "\r\n" + e.toString(), e); 094 } 095 catch (Exception e) 096 { 097 e.printStackTrace(); 098 throw new MojoExecutionException("Error unpacking file: " + 099 file.getAbsolutePath() + " to: " + location + "\r\n" + e.toString(), e); 100 } 101 } 102}