KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Copyfile


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
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26
27 /**
28  * Copies a file.
29  *
30  * @since Ant 1.1
31  *
32  * @deprecated The copyfile task is deprecated since Ant 1.2. Use
33  * copy instead.
34  */

35
36 public class Copyfile extends Task {
37
38     private File JavaDoc srcFile;
39     private File JavaDoc destFile;
40     private boolean filtering = false;
41     private boolean forceOverwrite = false;
42
43     /**
44      * Set the source file.
45      * @param src the source file.
46      */

47     public void setSrc(File JavaDoc src) {
48         srcFile = src;
49     }
50
51     /**
52      * The forceoverwrite attribute.
53      * Default is false.
54      * @param force if true overwrite even if the destination file
55      * is newer that the source file
56      */

57     public void setForceoverwrite(boolean force) {
58         forceOverwrite = force;
59     }
60
61     /**
62      * Set the destination file.
63      * @param dest the destination file.
64      */

65     public void setDest(File JavaDoc dest) {
66         destFile = dest;
67     }
68
69     /**
70      * The filtering attribute.
71      * Default is false.
72      * @param filter if true use filtering
73      */

74     public void setFiltering(String JavaDoc filter) {
75         filtering = Project.toBoolean(filter);
76     }
77
78     /**
79      * Execute the task.
80      * @throws BuildException on error
81      */

82     public void execute() throws BuildException {
83         log("DEPRECATED - The copyfile task is deprecated. Use copy instead.");
84
85         if (srcFile == null) {
86             throw new BuildException("The src attribute must be present.",
87                                      getLocation());
88         }
89
90         if (!srcFile.exists()) {
91             throw new BuildException("src " + srcFile.toString()
92                                      + " does not exist.", getLocation());
93         }
94
95         if (destFile == null) {
96             throw new BuildException("The dest attribute must be present.",
97                                      getLocation());
98         }
99
100         if (srcFile.equals(destFile)) {
101             log("Warning: src == dest", Project.MSG_WARN);
102         }
103
104         if (forceOverwrite
105             || srcFile.lastModified() > destFile.lastModified()) {
106             try {
107                 getProject().copyFile(srcFile, destFile, filtering, forceOverwrite);
108             } catch (IOException JavaDoc ioe) {
109                 String JavaDoc msg = "Error copying file: " + srcFile.getAbsolutePath()
110                     + " due to " + ioe.getMessage();
111                 throw new BuildException(msg);
112             }
113         }
114     }
115 }
116
Popular Tags