You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ear.java 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.types.ZipFileSet;
  24. import org.apache.tools.ant.util.FileUtils;
  25. import org.apache.tools.zip.ZipOutputStream;
  26. /**
  27. * Creates a EAR archive. Based on WAR task
  28. *
  29. * @since Ant 1.4
  30. *
  31. * @ant.task category="packaging"
  32. */
  33. public class Ear extends Jar {
  34. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  35. private File deploymentDescriptor;
  36. private boolean descriptorAdded;
  37. private static final String XML_DESCRIPTOR_PATH = "META-INF/application.xml";
  38. /**
  39. * Create an Ear task.
  40. */
  41. public Ear() {
  42. super();
  43. archiveType = "ear";
  44. emptyBehavior = "create";
  45. }
  46. /**
  47. * Set the destination file.
  48. * @param earFile the destination file
  49. * @deprecated since 1.5.x.
  50. * Use setDestFile(destfile) instead.
  51. */
  52. public void setEarfile(File earFile) {
  53. setDestFile(earFile);
  54. }
  55. /**
  56. * File to incorporate as application.xml.
  57. * @param descr the descriptor file
  58. */
  59. public void setAppxml(File descr) {
  60. deploymentDescriptor = descr;
  61. if (!deploymentDescriptor.exists()) {
  62. throw new BuildException("Deployment descriptor: "
  63. + deploymentDescriptor
  64. + " does not exist.");
  65. }
  66. // Create a ZipFileSet for this file, and pass it up.
  67. ZipFileSet fs = new ZipFileSet();
  68. fs.setFile(deploymentDescriptor);
  69. fs.setFullpath(XML_DESCRIPTOR_PATH);
  70. super.addFileset(fs);
  71. }
  72. /**
  73. * Adds zipfileset.
  74. *
  75. * @param fs zipfileset to add
  76. */
  77. public void addArchives(ZipFileSet fs) {
  78. // We just set the prefix for this fileset, and pass it up.
  79. // Do we need to do this? LH
  80. fs.setPrefix("/");
  81. super.addFileset(fs);
  82. }
  83. /**
  84. * Initialize the output stream.
  85. * @param zOut the zip output stream.
  86. * @throws IOException on I/O errors
  87. * @throws BuildException on other errors
  88. */
  89. protected void initZipOutputStream(ZipOutputStream zOut)
  90. throws IOException, BuildException {
  91. // If no webxml file is specified, it's an error.
  92. if (deploymentDescriptor == null && !isInUpdateMode()) {
  93. throw new BuildException("appxml attribute is required", getLocation());
  94. }
  95. super.initZipOutputStream(zOut);
  96. }
  97. /**
  98. * Overridden from Zip class to deal with application.xml
  99. * @param file the file to add to the archive
  100. * @param zOut the stream to write to
  101. * @param vPath the name this entry shall have in the archive
  102. * @param mode the Unix permissions to set.
  103. * @throws IOException on error
  104. */
  105. protected void zipFile(File file, ZipOutputStream zOut, String vPath,
  106. int mode)
  107. throws IOException {
  108. // If the file being added is META-INF/application.xml, we
  109. // warn if it's not the one specified in the "appxml"
  110. // attribute - or if it's being added twice, meaning the same
  111. // file is specified by the "appxml" attribute and in a
  112. // <fileset> element.
  113. if (XML_DESCRIPTOR_PATH.equalsIgnoreCase(vPath)) {
  114. if (deploymentDescriptor == null
  115. || !FILE_UTILS.fileNameEquals(deploymentDescriptor, file)
  116. || descriptorAdded) {
  117. logWhenWriting("Warning: selected " + archiveType
  118. + " files include a " + XML_DESCRIPTOR_PATH
  119. + " which will"
  120. + " be ignored (please use appxml attribute to "
  121. + archiveType + " task)",
  122. Project.MSG_WARN);
  123. } else {
  124. super.zipFile(file, zOut, vPath, mode);
  125. descriptorAdded = true;
  126. }
  127. } else {
  128. super.zipFile(file, zOut, vPath, mode);
  129. }
  130. }
  131. /**
  132. * Make sure we don't think we already have a application.xml next
  133. * time this task gets executed.
  134. */
  135. protected void cleanUp() {
  136. descriptorAdded = false;
  137. super.cleanUp();
  138. }
  139. }