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