1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.tools.ant.taskdefs;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.taskdefs.War;
22 import org.apache.tools.ant.types.ZipFileSet;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.NamedNodeMap;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27 import org.xml.sax.SAXException;
28
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
32 import java.io.File;
33 import java.io.IOException;
34
35 /*** Extension to the regular war task to include files specified in the libXmlFile. */
36 public class WarAutoLib extends War {
37
38 /*** Directory which contains the library files. Default is lib. */
39 public File directory;
40
41 /*** Holds value of property libXmlFile. */
42 private File libXmlFile;
43
44
45 /*** Setter for libDir property.
46 * @param directory Directory which contains the library files.
47 */
48 public void setLibdir(File directory) {
49
50 this.directory = directory;
51 }
52
53
54 /*** Method called by ant to execute this task.
55 * @throws BuildException Exception which is thrown to stop the build process and display an error message
56 */
57 public void execute() throws BuildException {
58 if (this.directory == null) {
59 directory = new File("lib");
60 }
61 if (!this.directory.exists()) {
62 this.directory.mkdirs();
63 }
64 if (!directory.isDirectory()) {
65 throw new BuildException("lib directory not properly set.");
66 }
67 try {
68 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
69 dbf.setValidating(true);
70 DocumentBuilder db = dbf.newDocumentBuilder();
71 Document doc = db.parse(libXmlFile);
72 doc.normalize();
73 NodeList warTags = doc.getElementsByTagName("war");
74 if (warTags.getLength() > 0) {
75 NodeList nl = warTags.item(0).getChildNodes();
76 for (int i = 0; i < nl.getLength(); i++) {
77 Node node = nl.item(i);
78 if (!node.getNodeName().equals("lib") && !node.getNodeName().equals("localLib")) continue;
79 NamedNodeMap atts = node.getAttributes();
80 Node att = atts.getNamedItem("file");
81 if (att == null) {
82 att = atts.getNamedItem("url");
83 }
84 String filename = att.getNodeValue();
85 if (filename.indexOf("/") > -1) {
86 filename = filename.substring(filename.lastIndexOf("/") + 1);
87 }
88 File file = new File(directory.getAbsolutePath(), filename);
89 log("adding lib " + filename);
90 ZipFileSet zfs = new ZipFileSet();
91 zfs.setFile(file);
92 addLib(zfs);
93 }
94 }
95 else {
96 log("Not adding any libs because war tag is not found in " + libXmlFile.getName());
97 }
98 super.execute();
99 }
100 catch (ParserConfigurationException pce) {
101 throw new BuildException(pce);
102 }
103 catch (SAXException se) {
104 throw new BuildException(se);
105 }
106 catch (IOException ie) {
107 throw new BuildException(ie);
108 }
109 }
110
111
112 /*** Setter for property libXmlFile.
113 * @param libXmlFile The lib file which specifies what files are to be checked out
114 */
115 public void setLibXmlFile(File libXmlFile) {
116 this.libXmlFile = libXmlFile;
117 }
118
119 }