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.

Mkdir.java 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Project;
  23. /**
  24. * Creates a given directory.
  25. * Creates a directory and any non-existent parent directories, when
  26. * necessary
  27. *
  28. * @since Ant 1.1
  29. *
  30. * @ant.task category="filesystem"
  31. */
  32. public class Mkdir extends Task {
  33. private static final int MKDIR_RETRY_SLEEP_MILLIS = 10;
  34. /**
  35. * our little directory
  36. */
  37. private File dir;
  38. /**
  39. * create the directory and all parents
  40. * @throws BuildException if dir is somehow invalid, or creation failed.
  41. */
  42. public void execute() throws BuildException {
  43. if (dir == null) {
  44. throw new BuildException("dir attribute is required", getLocation());
  45. }
  46. if (dir.isFile()) {
  47. throw new BuildException("Unable to create directory as a file "
  48. + "already exists with that name: "
  49. + dir.getAbsolutePath());
  50. }
  51. if (!dir.exists()) {
  52. boolean result = mkdirs(dir);
  53. if (!result) {
  54. String msg = "Directory " + dir.getAbsolutePath()
  55. + " creation was not successful for an unknown reason";
  56. throw new BuildException(msg, getLocation());
  57. }
  58. log("Created dir: " + dir.getAbsolutePath());
  59. } else {
  60. log("Skipping " + dir.getAbsolutePath()
  61. + " because it already exists.", Project.MSG_VERBOSE);
  62. }
  63. }
  64. /**
  65. * the directory to create; required.
  66. *
  67. * @param dir the directory to be made.
  68. */
  69. public void setDir(File dir) {
  70. this.dir = dir;
  71. }
  72. /**
  73. * Attempt to fix possible race condition when creating
  74. * directories on WinXP. If the mkdirs does not work,
  75. * wait a little and try again.
  76. */
  77. private boolean mkdirs(File f) {
  78. if (!f.mkdirs()) {
  79. try {
  80. Thread.sleep(MKDIR_RETRY_SLEEP_MILLIS);
  81. return f.mkdirs();
  82. } catch (InterruptedException ex) {
  83. return f.mkdirs();
  84. }
  85. }
  86. return true;
  87. }
  88. }