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
15
16
17
18
19
20 public abstract class AbstractSiteMojo
21 extends AbstractMojo
22 {
23
24
25
26
27
28
29
30 protected ArchiverManager archiverManager;
31
32
33
34
35
36
37
38
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
67
68
69
70
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 }