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 java.io.IOException;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.Task;
  23. import org.apache.tools.ant.util.FileUtils;
  24. /**
  25. * Renames a file.
  26. *
  27. *
  28. * @deprecated The rename task is deprecated since Ant 1.2. Use move instead.
  29. * @since Ant 1.1
  30. */
  31. public class Rename extends Task {
  32. private File src;
  33. private File dest;
  34. private boolean replace = true;
  35. /**
  36. * Sets the file to be renamed.
  37. * @param src the file to rename
  38. */
  39. public void setSrc(File src) {
  40. this.src = src;
  41. }
  42. /**
  43. * Sets the new name of the file.
  44. * @param dest the new name of the file.
  45. */
  46. public void setDest(File dest) {
  47. this.dest = dest;
  48. }
  49. /**
  50. * Sets whether an existing file should be replaced.
  51. * @param replace <code>on</code>, if an existing file should be replaced.
  52. */
  53. public void setReplace(String replace) {
  54. this.replace = Project.toBoolean(replace);
  55. }
  56. /**
  57. * Renames the file <code>src</code> to <code>dest</code>
  58. * @exception org.apache.tools.ant.BuildException The exception is
  59. * thrown, if the rename operation fails.
  60. */
  61. public void execute() throws BuildException {
  62. log("DEPRECATED - The rename task is deprecated. Use move instead.");
  63. if (dest == null) {
  64. throw new BuildException("dest attribute is required", getLocation());
  65. }
  66. if (src == null) {
  67. throw new BuildException("src attribute is required", getLocation());
  68. }
  69. if (!replace && dest.exists()) {
  70. throw new BuildException(dest + " already exists.");
  71. }
  72. try {
  73. FileUtils.newFileUtils().rename(src, dest);
  74. } catch (IOException e) {
  75. throw new BuildException("Unable to rename " + src + " to "
  76. + dest, e, getLocation());
  77. }
  78. }
  79. }