diff --git a/docs/manual/CoreTasks/war.html b/docs/manual/CoreTasks/war.html
index eeaf1dd5d..0ed78f46b 100644
--- a/docs/manual/CoreTasks/war.html
+++ b/docs/manual/CoreTasks/war.html
@@ -33,7 +33,20 @@ treatment for files that should end up in the
(The War task is a shortcut for specifying the particular layout of a WAR file.
The same thing can be accomplished by using the prefix and fullpath
attributes of zipfilesets in a Zip or Jar task.)
-The extended zipfileset element from the zip task (with attributes prefix, fullpath, and src) is available in the War task.
+The extended zipfileset element from the zip task
+ (with attributes prefix, fullpath, and src)
+ is available in the War task. The task is also resource-enabled
+ and will add nested resources and resource collections to the archive.
+
+
+ Before Servlet API 2.5/Java EE 5, a WEB-INF/web.xml file was mandatory in a
+ WAR file, so this task failed if the webxml
attribute was missing.
+ As the web.xml file is now optional, the webxml
attribute is now
+ downgraded to being optional. The task will warn if the file is not
+ included as an attribute or in a fileset, but still succeed. The task
+ will also complain if more than one web.xml file is added to the JAR.
+
+
Please note that the zip format allows multiple files of the same
fully-qualified name to exist within a single archive. This has been
@@ -55,13 +68,13 @@ to a value other than its default, "add"
.
warfile |
- Deprecated name of the file to create
+ | Deprecated name of the file to create
-use destfile instead. |
webxml |
The deployment descriptor to use (WEB-INF/web.xml). |
- Yes, unless update is set to true |
+ No (since Ant1.7) |
basedir |
@@ -231,7 +244,7 @@ using Ant's default manifest file. The content of
directory, and thus it is our fault your webapp doesn't work. The cause
of these complaints lies in WinZip, which turns an all upper-case
directory into an all lower case one in a fit of helpfulness. Please check that
-jar xvf yourwebapp.war shows the same behaviour before filing another
+jar xvf yourwebapp.war
shows the same behaviour before filing another
report.
Winzip has an option allowing all uppercase names (which is off by default!). It can be enabled by:
Menu "Options" -> "Configuration", "View" property/tab page, then "General" group box has an option called "Allow all uppercase file names".
diff --git a/src/main/org/apache/tools/ant/taskdefs/War.java b/src/main/org/apache/tools/ant/taskdefs/War.java
index f8a9ef49f..96d3fced6 100644
--- a/src/main/org/apache/tools/ant/taskdefs/War.java
+++ b/src/main/org/apache/tools/ant/taskdefs/War.java
@@ -20,6 +20,7 @@ package org.apache.tools.ant.taskdefs;
import java.io.File;
import java.io.IOException;
+import java.util.Locale;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.ZipFileSet;
@@ -55,8 +56,10 @@ public class War extends Jar {
* flag set if the descriptor is added
*/
private boolean descriptorAdded;
+ private File addedWebXmlFile;
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
+ private static final String XML_DESCRIPTOR_PATH = "web-inf/web.xml";
/** Constructor for the War Task. */
public War() {
@@ -93,7 +96,7 @@ public class War extends Jar {
// Create a ZipFileSet for this file, and pass it up.
ZipFileSet fs = new ZipFileSet();
fs.setFile(deploymentDescriptor);
- fs.setFullpath("WEB-INF/web.xml");
+ fs.setFullpath(XML_DESCRIPTOR_PATH);
super.addFileset(fs);
}
@@ -137,16 +140,18 @@ public class War extends Jar {
*/
protected void initZipOutputStream(ZipOutputStream zOut)
throws IOException, BuildException {
- // If no webxml file is specified, it's an error.
- if (deploymentDescriptor == null && !isInUpdateMode()) {
- throw new BuildException("webxml attribute is required", getLocation());
- }
-
super.initZipOutputStream(zOut);
}
/**
* Overridden from Zip class to deal with web.xml
+ *
+ * Here are cases that can arise
+ * -not a web.xml file : add
+ * -first web.xml : add, remember we added it
+ * -same web.xml again: skip
+ * -alternate web.xml : warn and skip
+ *
* @param file the file to add to the archive
* @param zOut the stream to write to
* @param vPath the name this entry shall have in the archive
@@ -160,29 +165,51 @@ public class War extends Jar {
// not the one specified in the "webxml" attribute - or if
// it's being added twice, meaning the same file is specified
// by the "webxml" attribute and in a element.
- if (vPath.equalsIgnoreCase("WEB-INF/web.xml")) {
- if (deploymentDescriptor == null
- || !FILE_UTILS.fileNameEquals(deploymentDescriptor, file)
- || descriptorAdded) {
- log("Warning: selected " + archiveType
- + " files include a WEB-INF/web.xml which will be ignored "
- + "(please use webxml attribute to "
- + archiveType + " task)", Project.MSG_WARN);
+ String vPathLowerCase = vPath.toLowerCase(Locale.ENGLISH);
+ //by default, we add the file.
+ boolean addFile = true;
+ if (XML_DESCRIPTOR_PATH.equals(vPathLowerCase)) {
+ //a web.xml file was found. See if it is a duplicate or not
+ if (addedWebXmlFile != null) {
+ //a second web.xml file, so skip it
+ addFile = false;
+ //check to see if we warn or not
+ if (!FILE_UTILS.fileNameEquals(addedWebXmlFile, file)) {
+ log("Warning: selected " + archiveType
+ +" files include a second " +XML_DESCRIPTOR_PATH
+ + " which will be ignored.\n"
+ + "The duplicate entry is at " +file +'\n'
+ + "The file that will be used is "
+ + addedWebXmlFile,
+ Project.MSG_WARN);
+ }
} else {
- super.zipFile(file, zOut, vPath, mode);
+ //no added file, yet
+ addedWebXmlFile=file;
+ //there is no web.xml file, so add it
+ addFile = true;
+ //and remember that we did
descriptorAdded = true;
+ deploymentDescriptor = file;
}
- } else {
+ }
+ if (addFile) {
super.zipFile(file, zOut, vPath, mode);
}
}
+
/**
* Make sure we don't think we already have a web.xml next time this task
* gets executed.
*/
protected void cleanUp() {
- descriptorAdded = false;
+ if(addedWebXmlFile==null) {
+ log("No WEB-INF/web.xml file was added.\n"
+ +"This WAR file is only valid on Java EE 5+ runtimes\n"
+ +"and web servers that support v2.5 Web Applications");
+ }
+ addedWebXmlFile = null;
super.cleanUp();
}
}
diff --git a/src/tests/antunit/taskdefs/war-test.xml b/src/tests/antunit/taskdefs/war-test.xml
new file mode 100644
index 000000000..2a4281e96
--- /dev/null
+++ b/src/tests/antunit/taskdefs/war-test.xml
@@ -0,0 +1,113 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/tests/antunit/taskdefs/web.xml b/src/tests/antunit/taskdefs/web.xml
new file mode 100644
index 000000000..6ed2715e8
--- /dev/null
+++ b/src/tests/antunit/taskdefs/web.xml
@@ -0,0 +1,5 @@
+
+
+
+