1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Locale ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.types.ZipFileSet; 27 import org.apache.tools.ant.util.FileUtils; 28 import org.apache.tools.zip.ZipOutputStream; 29 30 31 48 public class War extends Jar { 49 50 53 private File deploymentDescriptor; 54 55 58 private boolean needxmlfile = true; 59 private File addedWebXmlFile; 60 61 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 62 63 private static final String XML_DESCRIPTOR_PATH = "WEB-INF/web.xml"; 64 65 private static final String XML_DESCRIPTOR_PATH_LC = 66 XML_DESCRIPTOR_PATH.toLowerCase(Locale.ENGLISH); 67 68 69 public War() { 70 super(); 71 archiveType = "war"; 72 emptyBehavior = "create"; 73 } 74 75 83 public void setWarfile(File warFile) { 84 setDestFile(warFile); 85 } 86 87 92 public void setWebxml(File descr) { 93 deploymentDescriptor = descr; 94 if (!deploymentDescriptor.exists()) { 95 throw new BuildException("Deployment descriptor: " 96 + deploymentDescriptor 97 + " does not exist."); 98 } 99 100 ZipFileSet fs = new ZipFileSet(); 102 fs.setFile(deploymentDescriptor); 103 fs.setFullpath(XML_DESCRIPTOR_PATH); 104 super.addFileset(fs); 105 } 106 107 108 112 public void setNeedxmlfile(boolean needxmlfile) { 113 this.needxmlfile = needxmlfile; 114 } 115 116 120 121 public void addLib(ZipFileSet fs) { 122 fs.setPrefix("WEB-INF/lib/"); 124 super.addFileset(fs); 125 } 126 127 131 public void addClasses(ZipFileSet fs) { 132 fs.setPrefix("WEB-INF/classes/"); 134 super.addFileset(fs); 135 } 136 137 141 public void addWebinf(ZipFileSet fs) { 142 fs.setPrefix("WEB-INF/"); 144 super.addFileset(fs); 145 } 146 147 154 protected void initZipOutputStream(ZipOutputStream zOut) 155 throws IOException , BuildException { 156 super.initZipOutputStream(zOut); 157 } 158 159 174 protected void zipFile(File file, ZipOutputStream zOut, String vPath, 175 int mode) 176 throws IOException { 177 String vPathLowerCase = vPath.toLowerCase(Locale.ENGLISH); 182 boolean addFile = true; 184 if (XML_DESCRIPTOR_PATH_LC.equals(vPathLowerCase)) { 185 if (addedWebXmlFile != null) { 187 addFile = false; 189 if (!FILE_UTILS.fileNameEquals(addedWebXmlFile, file)) { 191 log("Warning: selected " + archiveType 192 + " files include a second " + XML_DESCRIPTOR_PATH 193 + " which will be ignored.\n" 194 + "The duplicate entry is at " + file + '\n' 195 + "The file that will be used is " 196 + addedWebXmlFile, 197 Project.MSG_WARN); 198 } 199 } else { 200 addedWebXmlFile = file; 202 addFile = true; 204 deploymentDescriptor = file; 206 } 207 } 208 if (addFile) { 209 super.zipFile(file, zOut, vPath, mode); 210 } 211 } 212 213 214 218 protected void cleanUp() { 219 if (addedWebXmlFile == null 220 && deploymentDescriptor == null 221 && needxmlfile 222 && !isInUpdateMode()) { 223 throw new BuildException("No WEB-INF/web.xml file was added.\n" 224 + "If this is your intent, set needxml='false' "); 225 } 226 addedWebXmlFile = null; 227 super.cleanUp(); 228 } 229 } 230 | Popular Tags |