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