1. /*
  2. * Copyright 2002-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 org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.ProjectHelper;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.ProjectHelper;
  22. import org.apache.tools.ant.Task;
  23. import org.apache.tools.ant.util.FileUtils;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.Vector;
  27. /**
  28. * <p>
  29. * Task to import another build file into the current project.
  30. * <p>
  31. * It must be 'top level'. On execution it will read another Ant file
  32. * into the same Project.
  33. * <p>
  34. * <b>Important</b>: we have not finalized how relative file references
  35. * will be resolved in deep/complex build hierarchies -such as what happens
  36. * when an imported file imports another file. Use absolute references for
  37. * enhanced build file stability, especially in the imported files.
  38. *
  39. * Examples
  40. * <pre>
  41. * <import file="../common-targets.xml" />
  42. * </pre>
  43. * Import targets from a file in a parent directory.
  44. *<p>
  45. * <pre>
  46. * <import file="${deploy-platform}.xml" />
  47. * </pre>
  48. * Import the project defined by the property deploy-platform
  49. *
  50. * @since Ant1.6
  51. * @ant.task category="control"
  52. */
  53. public class ImportTask extends Task {
  54. private String file;
  55. private boolean optional;
  56. private static final FileUtils FILE_UTILS = FileUtils.newFileUtils();
  57. /**
  58. * sets the optional attribute
  59. *
  60. * @param optional if true ignore files that are not present,
  61. * default is false
  62. */
  63. public void setOptional(boolean optional) {
  64. this.optional = optional;
  65. }
  66. /**
  67. * the name of the file to import. How relative paths are resolved is still
  68. * in flux: use absolute paths for safety.
  69. * @param file the name of the file
  70. */
  71. public void setFile(String file) {
  72. // I don't think we can use File - different rules
  73. // for relative paths.
  74. this.file = file;
  75. }
  76. /**
  77. * This relies on the task order model.
  78. *
  79. */
  80. public void execute() {
  81. if (file == null) {
  82. throw new BuildException("import requires file attribute");
  83. }
  84. if (getOwningTarget() == null
  85. || !"".equals(getOwningTarget().getName())) {
  86. throw new BuildException("import only allowed as a top-level task");
  87. }
  88. ProjectHelper helper =
  89. (ProjectHelper) getProject().getReference("ant.projectHelper");
  90. Vector importStack = helper.getImportStack();
  91. if (importStack.size() == 0) {
  92. // this happens if ant is used with a project
  93. // helper that doesn't set the import.
  94. throw new BuildException("import requires support in ProjectHelper");
  95. }
  96. if (getLocation() == null || getLocation().getFileName() == null) {
  97. throw new BuildException("Unable to get location of import task");
  98. }
  99. File buildFile = new File(getLocation().getFileName());
  100. buildFile = new File(buildFile.getAbsolutePath());
  101. getProject().log("Importing file " + file + " from "
  102. + buildFile.getAbsolutePath(), Project.MSG_VERBOSE);
  103. // Paths are relative to the build file they're imported from,
  104. // *not* the current directory (same as entity includes).
  105. File buildFileParent = new File(buildFile.getParent());
  106. File importedFile = FILE_UTILS.resolveFile(buildFileParent, file);
  107. if (!importedFile.exists()) {
  108. String message =
  109. "Cannot find " + file + " imported from "
  110. + buildFile.getAbsolutePath();
  111. if (optional) {
  112. getProject().log(message, Project.MSG_VERBOSE);
  113. return;
  114. } else {
  115. throw new BuildException(message);
  116. }
  117. }
  118. if (importStack.contains(importedFile)) {
  119. getProject().log(
  120. "Skipped already imported file:\n "
  121. + importedFile + "\n", Project.MSG_VERBOSE);
  122. return;
  123. }
  124. try {
  125. helper.parse(getProject(), importedFile);
  126. } catch (BuildException ex) {
  127. throw ProjectHelper.addLocationToBuildException(
  128. ex, getLocation());
  129. }
  130. }
  131. }