1 package org.apache.maven.plugins.site.render;
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.archiver.MavenArchiveConfiguration;
26 import org.apache.maven.archiver.MavenArchiver;
27 import org.apache.maven.artifact.DependencyResolutionRequiredException;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.Component;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.plugins.annotations.ResolutionScope;
35 import org.apache.maven.project.MavenProjectHelper;
36 import org.codehaus.plexus.archiver.Archiver;
37 import org.codehaus.plexus.archiver.ArchiverException;
38 import org.codehaus.plexus.archiver.jar.JarArchiver;
39 import org.codehaus.plexus.archiver.jar.ManifestException;
40
41
42
43
44
45
46
47
48
49 @Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST,
50 requiresReports = true )
51 public class SiteJarMojo
52 extends SiteMojo
53 {
54 private static final String[] DEFAULT_ARCHIVE_EXCLUDES = new String[]{ };
55
56 private static final String[] DEFAULT_ARCHIVE_INCLUDES = new String[]{ "**/**" };
57
58
59
60
61 @Parameter( property = "project.build.directory", required = true )
62 private String jarOutputDirectory;
63
64
65
66
67
68 @Parameter( property = "project.build.finalName", required = true )
69 private String finalName;
70
71
72
73
74 @Component
75 private MavenProjectHelper projectHelper;
76
77
78
79
80 @Parameter( property = "site.attach", defaultValue = "true" )
81 private boolean attach;
82
83
84
85
86
87
88 @Component( role = Archiver.class, hint = "jar" )
89 private JarArchiver jarArchiver;
90
91
92
93
94
95
96
97 @Parameter
98 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
99
100
101
102
103
104
105
106 @Parameter
107 private String[] archiveIncludes;
108
109
110
111
112
113
114
115 @Parameter
116 private String[] archiveExcludes;
117
118
119
120
121 public void execute()
122 throws MojoExecutionException, MojoFailureException
123 {
124 if ( skip )
125 {
126 getLog().info( "maven.site.skip = true: Skipping jar generation" );
127 return;
128 }
129
130 if ( !outputDirectory.exists() )
131 {
132 super.execute();
133 }
134
135 try
136 {
137 File outputFile = createArchive( outputDirectory,
138 finalName + "-" + getClassifier() + "." + getArtifactType() );
139
140 if ( attach )
141 {
142 projectHelper.attachArtifact( project, getArtifactType(), getClassifier(), outputFile );
143 }
144 else
145 {
146 getLog().info( "NOT adding site jar to the list of attached artifacts." );
147 }
148 }
149 catch ( ArchiverException | IOException | ManifestException | DependencyResolutionRequiredException e )
150 {
151 throw new MojoExecutionException( "Error while creating archive.", e );
152 }
153 }
154
155 protected String getArtifactType()
156 {
157 return "jar";
158 }
159
160 protected String getClassifier()
161 {
162 return "site";
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176 private File createArchive( File siteDirectory, String jarFilename )
177 throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException
178 {
179 File siteJar = new File( jarOutputDirectory, jarFilename );
180
181 MavenArchiver archiver = new MavenArchiver();
182
183 archiver.setArchiver( this.jarArchiver );
184
185 archiver.setOutputFile( siteJar );
186
187 if ( !siteDirectory.isDirectory() )
188 {
189 getLog().warn( "JAR will be empty - no content was marked for inclusion !" );
190 }
191 else
192 {
193 archiver.getArchiver().addDirectory( siteDirectory, getArchiveIncludes(), getArchiveExcludes() );
194 }
195
196 archiver.createArchive( getSession(), getProject(), archive );
197
198 return siteJar;
199 }
200
201 private String[] getArchiveIncludes()
202 {
203 if ( this.archiveIncludes != null && this.archiveIncludes.length > 0 )
204 {
205 return this.archiveIncludes;
206 }
207
208 return DEFAULT_ARCHIVE_INCLUDES;
209 }
210
211 private String[] getArchiveExcludes()
212 {
213 if ( this.archiveExcludes != null && this.archiveExcludes.length > 0 )
214 {
215 return this.archiveExcludes;
216 }
217 return DEFAULT_ARCHIVE_EXCLUDES;
218 }
219 }