1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.IOException;
|
---|
22 | import org.apache.tools.ant.BuildException;
|
---|
23 | import org.apache.tools.ant.Project;
|
---|
24 | import org.apache.tools.ant.types.ZipFileSet;
|
---|
25 | import org.apache.tools.ant.util.FileUtils;
|
---|
26 | import org.apache.tools.zip.ZipOutputStream;
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * An extension of <jar> to create a WAR archive.
|
---|
31 | * Contains special treatment for files that should end up in the
|
---|
32 | * <code>WEB-INF/lib</code>, <code>WEB-INF/classes</code> or
|
---|
33 | * <code>WEB-INF</code> directories of the Web Application Archive.</p>
|
---|
34 | * <p>(The War task is a shortcut for specifying the particular layout of a WAR file.
|
---|
35 | * The same thing can be accomplished by using the <i>prefix</i> and <i>fullpath</i>
|
---|
36 | * attributes of zipfilesets in a Zip or Jar task.)</p>
|
---|
37 | * <p>The extended zipfileset element from the zip task
|
---|
38 | * (with attributes <i>prefix</i>, <i>fullpath</i>, and <i>src</i>)
|
---|
39 | * is available in the War task.</p>
|
---|
40 | *
|
---|
41 | *
|
---|
42 | * @since Ant 1.2
|
---|
43 | *
|
---|
44 | * @ant.task category="packaging"
|
---|
45 | * @see Jar
|
---|
46 | */
|
---|
47 | public class War extends Jar {
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * our web.xml deployment descriptor
|
---|
51 | */
|
---|
52 | private File deploymentDescriptor;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * flag set if the descriptor is added
|
---|
56 | */
|
---|
57 | private boolean descriptorAdded;
|
---|
58 |
|
---|
59 | private static final FileUtils fu = FileUtils.newFileUtils();
|
---|
60 |
|
---|
61 | public War() {
|
---|
62 | super();
|
---|
63 | archiveType = "war";
|
---|
64 | emptyBehavior = "create";
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * <i>Deprecated<i> name of the file to create
|
---|
69 | * -use <tt>destfile</tt> instead.
|
---|
70 | * @deprecated Use setDestFile(File) instead
|
---|
71 | * @ant.attribute ignore="true"
|
---|
72 | */
|
---|
73 | public void setWarfile(File warFile) {
|
---|
74 | setDestFile(warFile);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * set the deployment descriptor to use (WEB-INF/web.xml);
|
---|
79 | * required unless <tt>update=true</tt>
|
---|
80 | */
|
---|
81 | public void setWebxml(File descr) {
|
---|
82 | deploymentDescriptor = descr;
|
---|
83 | if (!deploymentDescriptor.exists()) {
|
---|
84 | throw new BuildException("Deployment descriptor: "
|
---|
85 | + deploymentDescriptor
|
---|
86 | + " does not exist.");
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Create a ZipFileSet for this file, and pass it up.
|
---|
90 | ZipFileSet fs = new ZipFileSet();
|
---|
91 | fs.setFile(deploymentDescriptor);
|
---|
92 | fs.setFullpath("WEB-INF/web.xml");
|
---|
93 | super.addFileset(fs);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * add files under WEB-INF/lib/
|
---|
98 | */
|
---|
99 |
|
---|
100 | public void addLib(ZipFileSet fs) {
|
---|
101 | // We just set the prefix for this fileset, and pass it up.
|
---|
102 | fs.setPrefix("WEB-INF/lib/");
|
---|
103 | super.addFileset(fs);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * add files under WEB-INF/classes
|
---|
108 | */
|
---|
109 | public void addClasses(ZipFileSet fs) {
|
---|
110 | // We just set the prefix for this fileset, and pass it up.
|
---|
111 | fs.setPrefix("WEB-INF/classes/");
|
---|
112 | super.addFileset(fs);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * files to add under WEB-INF;
|
---|
117 | */
|
---|
118 | public void addWebinf(ZipFileSet fs) {
|
---|
119 | // We just set the prefix for this fileset, and pass it up.
|
---|
120 | fs.setPrefix("WEB-INF/");
|
---|
121 | super.addFileset(fs);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * override of parent; validates configuration
|
---|
126 | * before initializing the output stream.
|
---|
127 | */
|
---|
128 | protected void initZipOutputStream(ZipOutputStream zOut)
|
---|
129 | throws IOException, BuildException {
|
---|
130 | // If no webxml file is specified, it's an error.
|
---|
131 | if (deploymentDescriptor == null && !isInUpdateMode()) {
|
---|
132 | throw new BuildException("webxml attribute is required", getLocation());
|
---|
133 | }
|
---|
134 |
|
---|
135 | super.initZipOutputStream(zOut);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Overridden from Zip class to deal with web.xml
|
---|
140 | */
|
---|
141 | protected void zipFile(File file, ZipOutputStream zOut, String vPath,
|
---|
142 | int mode)
|
---|
143 | throws IOException {
|
---|
144 | // If the file being added is WEB-INF/web.xml, we warn if it's
|
---|
145 | // not the one specified in the "webxml" attribute - or if
|
---|
146 | // it's being added twice, meaning the same file is specified
|
---|
147 | // by the "webxml" attribute and in a <fileset> element.
|
---|
148 | if (vPath.equalsIgnoreCase("WEB-INF/web.xml")) {
|
---|
149 | if (deploymentDescriptor == null
|
---|
150 | || !fu.fileNameEquals(deploymentDescriptor, file)
|
---|
151 | || descriptorAdded) {
|
---|
152 | log("Warning: selected " + archiveType
|
---|
153 | + " files include a WEB-INF/web.xml which will be ignored "
|
---|
154 | + "(please use webxml attribute to "
|
---|
155 | + archiveType + " task)", Project.MSG_WARN);
|
---|
156 | } else {
|
---|
157 | super.zipFile(file, zOut, vPath, mode);
|
---|
158 | descriptorAdded = true;
|
---|
159 | }
|
---|
160 | } else {
|
---|
161 | super.zipFile(file, zOut, vPath, mode);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Make sure we don't think we already have a web.xml next time this task
|
---|
167 | * gets executed.
|
---|
168 | */
|
---|
169 | protected void cleanUp() {
|
---|
170 | descriptorAdded = false;
|
---|
171 | super.cleanUp();
|
---|
172 | }
|
---|
173 | }
|
---|