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.

TempFile.java 4.6 kB

11 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.util.FileUtils;
  23. /**
  24. * This task sets a property to the name of a temporary file.
  25. * Unlike {@link File#createTempFile}, this task does not (by default) 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>&lt;tempfile property="temp.file" /&gt;</pre>
  31. * create a temporary file
  32. * <pre>&lt;tempfile property="temp.file" suffix=".xml" /&gt;</pre>
  33. * create a temporary file with the .xml suffix.
  34. * <pre>&lt;tempfile property="temp.file" destDir="build"/&gt;</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. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  41. /**
  42. * Name of property to set.
  43. */
  44. private String property;
  45. /**
  46. * Directory to create the file in. Can be null.
  47. */
  48. private File destDir = null;
  49. /**
  50. * Prefix for the file.
  51. */
  52. private String prefix;
  53. /**
  54. * Suffix for the file.
  55. */
  56. private String suffix = "";
  57. /** deleteOnExit flag */
  58. private boolean deleteOnExit;
  59. /** createFile flag */
  60. private boolean createFile;
  61. /**
  62. * Sets the property you wish to assign the temporary file to.
  63. *
  64. * @param property The property to set
  65. * @ant.attribute group="required"
  66. */
  67. public void setProperty(String property) {
  68. this.property = property;
  69. }
  70. /**
  71. * Sets the destination directory. If not set,
  72. * the basedir directory is used instead.
  73. *
  74. * @param destDir The new destDir value
  75. */
  76. public void setDestDir(File destDir) {
  77. this.destDir = destDir;
  78. }
  79. /**
  80. * Sets the optional prefix string for the temp file.
  81. *
  82. * @param prefix string to prepend to generated string
  83. */
  84. public void setPrefix(String prefix) {
  85. this.prefix = prefix;
  86. }
  87. /**
  88. * Sets the optional suffix string for the temp file.
  89. *
  90. * @param suffix suffix including any ".", e.g ".xml"
  91. */
  92. public void setSuffix(String suffix) {
  93. this.suffix = suffix;
  94. }
  95. /**
  96. * Set whether the tempfile created by this task should be set
  97. * for deletion on normal VM exit.
  98. * @param deleteOnExit boolean flag.
  99. */
  100. public void setDeleteOnExit(boolean deleteOnExit) {
  101. this.deleteOnExit = deleteOnExit;
  102. }
  103. /**
  104. * Learn whether deleteOnExit is set for this tempfile task.
  105. * @return boolean deleteOnExit flag.
  106. */
  107. public boolean isDeleteOnExit() {
  108. return deleteOnExit;
  109. }
  110. /**
  111. * If set the file is actually created, if not just a name is created.
  112. * @param createFile boolean flag.
  113. */
  114. public void setCreateFile(boolean createFile) {
  115. this.createFile = createFile;
  116. }
  117. /**
  118. * Learn whether createFile flag is set for this tempfile task.
  119. * @return the createFile flag.
  120. */
  121. public boolean isCreateFile() {
  122. return createFile;
  123. }
  124. /**
  125. * Creates the temporary file.
  126. *
  127. * @exception BuildException if something goes wrong with the build
  128. */
  129. @Override
  130. public void execute() throws BuildException {
  131. if (property == null || property.isEmpty()) {
  132. throw new BuildException("no property specified");
  133. }
  134. if (destDir == null) {
  135. destDir = getProject().resolveFile(".");
  136. }
  137. File tfile = FILE_UTILS.createTempFile(prefix, suffix, destDir,
  138. deleteOnExit, createFile);
  139. getProject().setNewProperty(property, tfile.toString());
  140. }
  141. }