1. /*
  2. * Copyright 2001-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. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.types.ZipFileSet;
  23. import org.apache.tools.ant.util.FileUtils;
  24. import org.apache.tools.zip.ZipOutputStream;
  25. /**
  26. * Creates a EAR archive. Based on WAR task
  27. *
  28. *
  29. * @since Ant 1.4
  30. *
  31. * @ant.task category="packaging"
  32. */
  33. public class Ear extends Jar {
  34. private File deploymentDescriptor;
  35. private boolean descriptorAdded;
  36. private static final FileUtils fu = FileUtils.newFileUtils();
  37. /**
  38. * Create an Ear task.
  39. */
  40. public Ear() {
  41. super();
  42. archiveType = "ear";
  43. emptyBehavior = "create";
  44. }
  45. /**
  46. * @deprecated Use setDestFile(destfile) instead
  47. */
  48. public void setEarfile(File earFile) {
  49. setDestFile(earFile);
  50. }
  51. /**
  52. * File to incorporate as application.xml.
  53. */
  54. public void setAppxml(File descr) {
  55. deploymentDescriptor = descr;
  56. if (!deploymentDescriptor.exists()) {
  57. throw new BuildException("Deployment descriptor: "
  58. + deploymentDescriptor
  59. + " does not exist.");
  60. }
  61. // Create a ZipFileSet for this file, and pass it up.
  62. ZipFileSet fs = new ZipFileSet();
  63. fs.setFile(deploymentDescriptor);
  64. fs.setFullpath("META-INF/application.xml");
  65. super.addFileset(fs);
  66. }
  67. /**
  68. * Adds zipfileset.
  69. *
  70. * @param fs zipfileset to add
  71. */
  72. public void addArchives(ZipFileSet fs) {
  73. // We just set the prefix for this fileset, and pass it up.
  74. // Do we need to do this? LH
  75. fs.setPrefix("/");
  76. super.addFileset(fs);
  77. }
  78. protected void initZipOutputStream(ZipOutputStream zOut)
  79. throws IOException, BuildException {
  80. // If no webxml file is specified, it's an error.
  81. if (deploymentDescriptor == null && !isInUpdateMode()) {
  82. throw new BuildException("appxml attribute is required", getLocation());
  83. }
  84. super.initZipOutputStream(zOut);
  85. }
  86. /**
  87. * Overridden from Zip class to deal with application.xml
  88. */
  89. protected void zipFile(File file, ZipOutputStream zOut, String vPath,
  90. int mode)
  91. throws IOException {
  92. // If the file being added is META-INF/application.xml, we
  93. // warn if it's not the one specified in the "appxml"
  94. // attribute - or if it's being added twice, meaning the same
  95. // file is specified by the "appxml" attribute and in a
  96. // <fileset> element.
  97. if (vPath.equalsIgnoreCase("META-INF/application.xml")) {
  98. if (deploymentDescriptor == null
  99. || !fu.fileNameEquals(deploymentDescriptor, file)
  100. || descriptorAdded) {
  101. log("Warning: selected " + archiveType
  102. + " files include a META-INF/application.xml which will"
  103. + " be ignored (please use appxml attribute to "
  104. + archiveType + " task)", Project.MSG_WARN);
  105. } else {
  106. super.zipFile(file, zOut, vPath, mode);
  107. descriptorAdded = true;
  108. }
  109. } else {
  110. super.zipFile(file, zOut, vPath, mode);
  111. }
  112. }
  113. /**
  114. * Make sure we don't think we already have a application.xml next
  115. * time this task gets executed.
  116. */
  117. protected void cleanUp() {
  118. descriptorAdded = false;
  119. super.cleanUp();
  120. }
  121. }