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. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.util.FileUtils;
  22. /**
  23. * This task sets a property to the name of a temporary file.
  24. * Unlike the Java1.2 method to create a temporary file, this task
  25. * does work on Java1.1. Also, it does not actually create the
  26. * temporary file, but it does guarantee that the file did not
  27. * exist when the task was executed.
  28. * <p>
  29. * Examples
  30. * <pre><tempfile property="temp.file" /></pre>
  31. * create a temporary file
  32. * <pre><tempfile property="temp.file" suffix=".xml" /></pre>
  33. * create a temporary file with the .xml suffix.
  34. * <pre><tempfile property="temp.file" destDir="build"/></pre>
  35. * create a temp file in the build subdir
  36. *@since Ant 1.5
  37. *@ant.task
  38. */
  39. public class TempFile extends Task {
  40. /**
  41. * Name of property to set.
  42. */
  43. private String property;
  44. /**
  45. * Directory to create the file in. Can be null.
  46. */
  47. private File destDir = null;
  48. /**
  49. * Prefix for the file.
  50. */
  51. private String prefix;
  52. /**
  53. * Suffix for the file.
  54. */
  55. private String suffix = "";
  56. /**
  57. * Sets the property you wish to assign the temporary file to.
  58. *
  59. * @param property The property to set
  60. * @ant.attribute group="required"
  61. */
  62. public void setProperty(String property) {
  63. this.property = property;
  64. }
  65. /**
  66. * Sets the destination directory. If not set,
  67. * the basedir directory is used instead.
  68. *
  69. * @param destDir The new destDir value
  70. */
  71. public void setDestDir(File destDir) {
  72. this.destDir = destDir;
  73. }
  74. /**
  75. * Sets the optional prefix string for the temp file.
  76. *
  77. * @param prefix string to prepend to generated string
  78. */
  79. public void setPrefix(String prefix) {
  80. this.prefix = prefix;
  81. }
  82. /**
  83. * Sets the optional suffix string for the temp file.
  84. *
  85. * @param suffix suffix including any "." , e.g ".xml"
  86. */
  87. public void setSuffix(String suffix) {
  88. this.suffix = suffix;
  89. }
  90. /**
  91. * Creates the temporary file.
  92. *
  93. *@exception BuildException if something goes wrong with the build
  94. */
  95. public void execute() throws BuildException {
  96. if (property == null || property.length() == 0) {
  97. throw new BuildException("no property specified");
  98. }
  99. if (destDir == null) {
  100. destDir = getProject().resolveFile(".");
  101. }
  102. FileUtils utils = FileUtils.newFileUtils();
  103. File tfile = utils.createTempFile(prefix, suffix, destDir);
  104. getProject().setNewProperty(property, tfile.toString());
  105. }
  106. }