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