1 package org.apache.maven.plugins.site;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.project.MavenProjectHelper;
28
29 import org.codehaus.plexus.archiver.ArchiverException;
30 import org.codehaus.plexus.archiver.jar.JarArchiver;
31
32
33
34
35
36
37
38
39
40
41 public class SiteJarMojo
42 extends SiteMojo
43 {
44
45
46
47
48
49
50 private String jarOutputDirectory;
51
52
53
54
55
56
57
58
59 private String finalName;
60
61
62
63
64
65
66 private MavenProjectHelper projectHelper;
67
68
69
70
71
72
73 private boolean attach;
74
75
76
77
78 public void execute()
79 throws MojoExecutionException, MojoFailureException
80 {
81 if ( !outputDirectory.exists() )
82 {
83 super.execute();
84 }
85
86 try
87 {
88 File outputFile = createArchive( outputDirectory,
89 finalName + "-" + getClassifier() + "." + getArtifactType() );
90
91 if ( attach )
92 {
93 projectHelper.attachArtifact( project, getArtifactType(), getClassifier(), outputFile );
94 }
95 else
96 {
97 getLog().info( "NOT adding site jar to the list of attached artifacts." );
98 }
99 }
100 catch ( ArchiverException e )
101 {
102 throw new MojoExecutionException( "Error while creating archive.", e );
103 }
104 catch ( IOException e )
105 {
106 throw new MojoExecutionException( "Error while creating archive.", e );
107 }
108 }
109
110 protected String getArtifactType()
111 {
112 return "jar";
113 }
114
115 protected String getClassifier()
116 {
117 return "site";
118 }
119
120
121
122
123
124
125
126
127
128
129 private File createArchive( File siteDirectory, String jarFilename )
130 throws ArchiverException, IOException
131 {
132 File siteJar = new File( jarOutputDirectory, jarFilename );
133
134 if ( siteJar.exists() )
135 {
136 siteJar.delete();
137 }
138
139 JarArchiver archiver = new JarArchiver();
140 archiver.addDirectory( siteDirectory );
141 archiver.setDestFile( siteJar );
142 archiver.createArchive();
143
144 return siteJar;
145 }
146 }