1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.plexus.archiver.war;
18
19 import javax.inject.Named;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.codehaus.plexus.archiver.ArchiveEntry;
25 import org.codehaus.plexus.archiver.ArchiverException;
26 import org.codehaus.plexus.archiver.jar.JarArchiver;
27 import org.codehaus.plexus.archiver.util.ResourceUtils;
28 import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 @Named("war")
47 public class WarArchiver extends JarArchiver {
48
49
50
51
52 private File deploymentDescriptor;
53
54
55
56
57 private boolean expectWebXml = true;
58
59
60
61
62 private boolean descriptorAdded;
63
64
65
66
67
68 @Deprecated
69 public void setIgnoreWebxml(boolean excpectWebXml) {
70 expectWebXml = excpectWebXml;
71 }
72
73
74
75
76
77
78 public void setExpectWebXml(boolean expectWebXml) {
79 this.expectWebXml = expectWebXml;
80 }
81
82 public WarArchiver() {
83 super();
84 archiveType = "war";
85 }
86
87
88
89
90
91 public void setWebxml(File descr) throws ArchiverException {
92 deploymentDescriptor = descr;
93 if (!deploymentDescriptor.exists()) {
94 throw new ArchiverException("Deployment descriptor: " + deploymentDescriptor + " does not exist.");
95 }
96
97 addFile(descr, "WEB-INF" + File.separatorChar + "web.xml");
98 }
99
100
101
102
103 public void addLib(File fileName) throws ArchiverException {
104 addDirectory(fileName.getParentFile(), "WEB-INF/lib/", new String[] {fileName.getName()}, null);
105 }
106
107
108
109
110 public void addLibs(File directoryName, String[] includes, String[] excludes) throws ArchiverException {
111 addDirectory(directoryName, "WEB-INF/lib/", includes, excludes);
112 }
113
114
115
116
117 public void addClass(File fileName) throws ArchiverException {
118 addDirectory(fileName.getParentFile(), "WEB-INF/classes/", new String[] {fileName.getName()}, null);
119 }
120
121
122
123
124 public void addClasses(File directoryName, String[] includes, String[] excludes) throws ArchiverException {
125 addDirectory(directoryName, "WEB-INF/classes/", includes, excludes);
126 }
127
128
129
130
131 public void addWebinf(File directoryName, String[] includes, String[] excludes) throws ArchiverException {
132 addDirectory(directoryName, "WEB-INF/", includes, excludes);
133 }
134
135
136
137
138
139
140
141 @Override
142 protected void initZipOutputStream(ConcurrentJarCreator zOut) throws ArchiverException, IOException {
143
144 if (expectWebXml && deploymentDescriptor == null && !isInUpdateMode()) {
145 throw new ArchiverException(
146 "webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)");
147 }
148
149 super.initZipOutputStream(zOut);
150 }
151
152
153
154
155 @Override
156 protected void zipFile(ArchiveEntry entry, ConcurrentJarCreator zOut, String vPath)
157 throws IOException, ArchiverException {
158
159
160
161
162 if (vPath.equalsIgnoreCase("WEB-INF/web.xml")) {
163 if (descriptorAdded
164 || (expectWebXml
165 && (deploymentDescriptor == null
166 || !ResourceUtils.isCanonicalizedSame(
167 entry.getResource(), deploymentDescriptor)))) {
168 getLogger()
169 .warn("Warning: selected " + archiveType
170 + " files include a WEB-INF/web.xml which will be ignored "
171 + "\n(webxml attribute is missing from "
172 + archiveType + " task, or ignoreWebxml attribute is specified as 'true')");
173 } else {
174 super.zipFile(entry, zOut, vPath);
175 descriptorAdded = true;
176 }
177 } else {
178 super.zipFile(entry, zOut, vPath);
179 }
180 }
181
182
183
184
185
186 @Override
187 protected void cleanUp() throws IOException {
188 descriptorAdded = false;
189 expectWebXml = true;
190 super.cleanUp();
191 }
192 }