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