1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.tool.xar; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileOutputStream; |
24 |
|
import java.io.FilenameFilter; |
25 |
|
import java.io.OutputStream; |
26 |
|
import java.util.Collection; |
27 |
|
import java.util.LinkedList; |
28 |
|
import java.util.Queue; |
29 |
|
import java.util.Set; |
30 |
|
|
31 |
|
import org.apache.maven.artifact.Artifact; |
32 |
|
import org.apache.maven.plugin.MojoExecutionException; |
33 |
|
import org.codehaus.plexus.archiver.ArchiveEntry; |
34 |
|
import org.codehaus.plexus.archiver.zip.ZipArchiver; |
35 |
|
import org.dom4j.Document; |
36 |
|
import org.dom4j.Element; |
37 |
|
import org.dom4j.dom.DOMDocument; |
38 |
|
import org.dom4j.dom.DOMElement; |
39 |
|
import org.dom4j.io.OutputFormat; |
40 |
|
import org.dom4j.io.SAXReader; |
41 |
|
import org.dom4j.io.XMLWriter; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
|
|
| 82.6% |
Uncovered Elements: 31 (178) |
Complexity: 34 |
Complexity Density: 0.26 |
|
54 |
|
public class XarMojo extends AbstractXarMojo |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private boolean includeDependencies; |
62 |
|
|
|
|
| 62.5% |
Uncovered Elements: 3 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
63 |
3
|
@Override... |
64 |
|
public void execute() throws MojoExecutionException |
65 |
|
{ |
66 |
3
|
if (this.project.getResources().size() < 1) { |
67 |
0
|
getLog().warn("No XAR created as no resources were found"); |
68 |
0
|
return; |
69 |
|
} |
70 |
|
|
71 |
3
|
try { |
72 |
3
|
performArchive(); |
73 |
|
} catch (Exception e) { |
74 |
1
|
throw new MojoExecutionException("Error while creating XAR file", e); |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@throws |
82 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 4 |
Complexity Density: 0.22 |
|
83 |
3
|
private void performArchive() throws Exception... |
84 |
|
{ |
85 |
3
|
File xarFile = new File(this.project.getBuild().getDirectory(), this.project.getArtifactId() + ".xar"); |
86 |
|
|
87 |
3
|
String resourcesLocation = |
88 |
|
(this.project.getBasedir().getAbsolutePath() + "/src/main/resources").replace("/", File.separator); |
89 |
3
|
File resourcesDir = new File(resourcesLocation); |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
3
|
File sourceDir = new File(this.project.getBuild().getOutputDirectory()); |
95 |
|
|
96 |
3
|
ZipArchiver archiver = new ZipArchiver(); |
97 |
3
|
archiver.setEncoding(this.encoding); |
98 |
3
|
archiver.setDestFile(xarFile); |
99 |
3
|
archiver.setIncludeEmptyDirs(false); |
100 |
3
|
archiver.setCompress(true); |
101 |
|
|
102 |
3
|
if (this.includeDependencies) { |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
0
|
unpackDependentXars(); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
3
|
FilenameFilter packageXmlFiler = new FilenameFilter() |
112 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
113 |
5
|
@Override... |
114 |
|
public boolean accept(File dir, String name) |
115 |
|
{ |
116 |
5
|
return (name.equals(PACKAGE_XML)); |
117 |
|
} |
118 |
|
}; |
119 |
3
|
if (!resourcesDir.exists() || resourcesDir.list(packageXmlFiler).length == 0) { |
120 |
1
|
addFilesToArchive(archiver, sourceDir); |
121 |
|
} else { |
122 |
2
|
File packageXml = new File(resourcesDir, PACKAGE_XML); |
123 |
2
|
addFilesToArchive(archiver, sourceDir, packageXml); |
124 |
|
} |
125 |
|
|
126 |
2
|
archiver.createArchive(); |
127 |
|
|
128 |
2
|
this.project.getArtifact().setFile(xarFile); |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
@throws |
135 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
136 |
0
|
private void unpackDependentXars() throws MojoExecutionException... |
137 |
|
{ |
138 |
0
|
Set<Artifact> artifacts = this.project.getArtifacts(); |
139 |
0
|
if (artifacts != null) { |
140 |
0
|
for (Artifact artifact : artifacts) { |
141 |
0
|
if (!artifact.isOptional()) { |
142 |
0
|
if ("xar".equals(artifact.getType())) { |
143 |
0
|
unpackXarToOutputDirectory(artifact); |
144 |
|
} |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
@param |
154 |
|
@param |
155 |
|
@throws |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
157 |
1
|
private void generatePackageXml(File packageFile, Collection<ArchiveEntry> files) throws Exception... |
158 |
|
{ |
159 |
1
|
getLog().info("Generating package.xml descriptor at [" + packageFile.getPath() + "]"); |
160 |
|
|
161 |
1
|
OutputFormat outputFormat = new OutputFormat("", true); |
162 |
1
|
outputFormat.setEncoding(this.encoding); |
163 |
1
|
OutputStream out = new FileOutputStream(packageFile); |
164 |
1
|
XMLWriter writer = new XMLWriter(out, outputFormat); |
165 |
1
|
writer.write(toXML(files)); |
166 |
1
|
writer.close(); |
167 |
1
|
out.close(); |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
@param |
174 |
|
@return |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
176 |
1
|
private Document toXML(Collection<ArchiveEntry> files)... |
177 |
|
{ |
178 |
1
|
Document doc = new DOMDocument(); |
179 |
|
|
180 |
1
|
Element packageElement = new DOMElement("package"); |
181 |
1
|
doc.setRootElement(packageElement); |
182 |
|
|
183 |
1
|
Element infoElement = new DOMElement("infos"); |
184 |
1
|
packageElement.add(infoElement); |
185 |
1
|
addInfoElements(infoElement); |
186 |
|
|
187 |
1
|
Element filesElement = new DOMElement(FILES_TAG); |
188 |
1
|
packageElement.add(filesElement); |
189 |
1
|
addFileElements(files, filesElement); |
190 |
|
|
191 |
1
|
return doc; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
@param |
199 |
|
|
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 2 |
Complexity Density: 0.1 |
|
200 |
1
|
private void addInfoElements(Element infoElement)... |
201 |
|
{ |
202 |
1
|
Element el = new DOMElement("name"); |
203 |
1
|
el.addText(this.project.getName()); |
204 |
1
|
infoElement.add(el); |
205 |
|
|
206 |
1
|
el = new DOMElement("description"); |
207 |
1
|
String description = this.project.getDescription(); |
208 |
1
|
if (description == null) { |
209 |
0
|
el.addText(""); |
210 |
|
} else { |
211 |
1
|
el.addText(description); |
212 |
|
} |
213 |
1
|
infoElement.add(el); |
214 |
|
|
215 |
1
|
el = new DOMElement("licence"); |
216 |
1
|
el.addText(""); |
217 |
1
|
infoElement.add(el); |
218 |
|
|
219 |
1
|
el = new DOMElement("author"); |
220 |
1
|
el.addText("XWiki.Admin"); |
221 |
1
|
infoElement.add(el); |
222 |
|
|
223 |
1
|
el = new DOMElement("version"); |
224 |
1
|
el.addText(this.project.getVersion()); |
225 |
1
|
infoElement.add(el); |
226 |
|
|
227 |
1
|
el = new DOMElement("backupPack"); |
228 |
1
|
el.addText("true"); |
229 |
1
|
infoElement.add(el); |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
@param |
236 |
|
@param |
237 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
238 |
1
|
private void addFileElements(Collection<ArchiveEntry> files, Element filesElement)... |
239 |
|
{ |
240 |
1
|
for (ArchiveEntry entry : files) { |
241 |
|
|
242 |
8
|
if (entry.getName().indexOf("META-INF") == -1) { |
243 |
8
|
XWikiDocument xdoc = getDocFromXML(entry.getFile()); |
244 |
8
|
if (xdoc != null) { |
245 |
8
|
String fullName = xdoc.getFullName(); |
246 |
8
|
Element element = new DOMElement(FILE_TAG); |
247 |
8
|
element.setText(fullName); |
248 |
8
|
element.addAttribute("language", xdoc.getLanguage()); |
249 |
8
|
element.addAttribute("defaultAction", "0"); |
250 |
8
|
filesElement.add(element); |
251 |
|
} |
252 |
|
} |
253 |
|
} |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
@param |
260 |
|
@return |
261 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
262 |
8
|
private XWikiDocument getDocFromXML(File file)... |
263 |
|
{ |
264 |
8
|
XWikiDocument doc = null; |
265 |
|
|
266 |
8
|
try { |
267 |
8
|
doc = new XWikiDocument(); |
268 |
8
|
doc.fromXML(file); |
269 |
|
} catch (Exception e) { |
270 |
0
|
getLog().warn( |
271 |
|
"Failed to parse [" + file.getAbsolutePath() + "], skipping it. " + "The error was [" + e.getMessage() |
272 |
|
+ "]", e); |
273 |
|
} |
274 |
|
|
275 |
8
|
return doc; |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
@param |
282 |
|
@return |
283 |
|
@throws |
284 |
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
|
285 |
4
|
protected static Collection<String> getDocumentNamesFromXML(File file) throws Exception... |
286 |
|
{ |
287 |
4
|
Collection<String> result = new LinkedList<String>(); |
288 |
4
|
SAXReader reader = new SAXReader(); |
289 |
4
|
Document domdoc; |
290 |
4
|
domdoc = reader.read(file); |
291 |
|
|
292 |
4
|
Element filesElement = domdoc.getRootElement().element(FILES_TAG); |
293 |
|
|
294 |
4
|
if (filesElement == null) { |
295 |
1
|
throw new Exception("The supplied document contains no document list "); |
296 |
|
} |
297 |
|
|
298 |
3
|
Collection elements = filesElement.elements(FILE_TAG); |
299 |
3
|
for (Object item : elements) { |
300 |
18
|
if (item instanceof Element) { |
301 |
18
|
Element currentElement = (Element) item; |
302 |
18
|
String documentName = currentElement.getText(); |
303 |
18
|
result.add(documentName); |
304 |
|
} |
305 |
|
} |
306 |
|
|
307 |
3
|
return result; |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
@param |
315 |
|
@param |
316 |
|
@throws |
317 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
318 |
1
|
private void addFilesToArchive(ZipArchiver archiver, File sourceDir) throws Exception... |
319 |
|
{ |
320 |
1
|
File generatedPackageFile = new File(sourceDir, PACKAGE_XML); |
321 |
1
|
if (generatedPackageFile.exists()) { |
322 |
1
|
generatedPackageFile.delete(); |
323 |
|
} |
324 |
|
|
325 |
1
|
archiver.addDirectory(sourceDir, getIncludes(), getExcludes()); |
326 |
1
|
generatePackageXml(generatedPackageFile, archiver.getFiles().values()); |
327 |
1
|
archiver.addFile(generatedPackageFile, PACKAGE_XML); |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
@param |
335 |
|
@param |
336 |
|
@param |
337 |
|
@throws |
338 |
|
|
|
|
| 81.8% |
Uncovered Elements: 6 (33) |
Complexity: 7 |
Complexity Density: 0.28 |
|
339 |
2
|
private void addFilesToArchive(ZipArchiver archiver, File sourceDir, File packageXml) throws Exception... |
340 |
|
{ |
341 |
2
|
Collection<String> documentNames; |
342 |
2
|
getLog().info("Using the existing package.xml descriptor at [" + packageXml.getPath() + "]"); |
343 |
2
|
try { |
344 |
2
|
documentNames = getDocumentNamesFromXML(packageXml); |
345 |
|
} catch (Exception e) { |
346 |
1
|
getLog().error("The existing [" + PACKAGE_XML + "] is invalid."); |
347 |
1
|
throw e; |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
|
|
352 |
1
|
Queue<File> fileQueue = new LinkedList<File>(); |
353 |
1
|
addContentsToQueue(fileQueue, sourceDir); |
354 |
12
|
while (!fileQueue.isEmpty() && !documentNames.isEmpty()) { |
355 |
11
|
File currentFile = fileQueue.poll(); |
356 |
11
|
if (currentFile.isDirectory()) { |
357 |
3
|
addContentsToQueue(fileQueue, currentFile); |
358 |
|
} else { |
359 |
8
|
String documentName = XWikiDocument.getFullName(currentFile); |
360 |
8
|
if (documentNames.contains(documentName)) { |
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
368 |
5
|
String archivedFilePath = |
369 |
|
currentFile.getAbsolutePath() |
370 |
|
.substring((sourceDir.getAbsolutePath() + File.separator).length()); |
371 |
5
|
archivedFilePath.replace(File.separatorChar, '/'); |
372 |
|
|
373 |
5
|
archiver.addFile(currentFile, archivedFilePath); |
374 |
5
|
documentNames.remove(documentName); |
375 |
|
} |
376 |
|
} |
377 |
|
} |
378 |
|
|
379 |
1
|
if (!documentNames.isEmpty()) { |
380 |
0
|
StringBuilder errorMessage = new StringBuilder("The following documents could not be found: "); |
381 |
0
|
for (String name : documentNames) { |
382 |
0
|
errorMessage.append(name); |
383 |
0
|
errorMessage.append(" "); |
384 |
|
} |
385 |
0
|
throw new Exception(errorMessage.toString()); |
386 |
|
} |
387 |
|
|
388 |
1
|
archiver.addFile(packageXml, PACKAGE_XML); |
389 |
|
} |
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
|
394 |
|
@param |
395 |
|
@param |
396 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
397 |
4
|
private static void addContentsToQueue(Queue<File> fileQueue, File sourceDir)... |
398 |
|
{ |
399 |
4
|
for (File currentFile : sourceDir.listFiles()) { |
400 |
11
|
fileQueue.add(currentFile); |
401 |
|
} |
402 |
|
} |
403 |
|
} |