1 | /*
|
---|
2 | * Copyright 2000,2002-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 java.util.Enumeration;
|
---|
23 | import java.util.Hashtable;
|
---|
24 | import org.apache.tools.ant.BuildException;
|
---|
25 | import org.apache.tools.ant.DirectoryScanner;
|
---|
26 | import org.apache.tools.ant.Project;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Copies a directory.
|
---|
30 | *
|
---|
31 | *
|
---|
32 | * @since Ant 1.1
|
---|
33 | *
|
---|
34 | * @deprecated The copydir task is deprecated since Ant 1.2. Use copy instead.
|
---|
35 | */
|
---|
36 |
|
---|
37 | public class Copydir extends MatchingTask {
|
---|
38 |
|
---|
39 | private File srcDir;
|
---|
40 | private File destDir;
|
---|
41 | private boolean filtering = false;
|
---|
42 | private boolean flatten = false;
|
---|
43 | private boolean forceOverwrite = false;
|
---|
44 | private Hashtable filecopyList = new Hashtable();
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * The src attribute
|
---|
48 | *
|
---|
49 | * @param src the source file
|
---|
50 | */
|
---|
51 | public void setSrc(File src) {
|
---|
52 | srcDir = src;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The dest attribute
|
---|
57 | *
|
---|
58 | * @param dest the destination file
|
---|
59 | */
|
---|
60 | public void setDest(File dest) {
|
---|
61 | destDir = dest;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void setFiltering(boolean filter) {
|
---|
65 | filtering = filter;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void setFlatten(boolean flatten) {
|
---|
69 | this.flatten = flatten;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void setForceoverwrite(boolean force) {
|
---|
73 | forceOverwrite = force;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void execute() throws BuildException {
|
---|
77 | log("DEPRECATED - The copydir task is deprecated. Use copy instead.");
|
---|
78 |
|
---|
79 | if (srcDir == null) {
|
---|
80 | throw new BuildException("src attribute must be set!",
|
---|
81 | getLocation());
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (!srcDir.exists()) {
|
---|
85 | throw new BuildException("srcdir " + srcDir.toString()
|
---|
86 | + " does not exist!", getLocation());
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (destDir == null) {
|
---|
90 | throw new BuildException("The dest attribute must be set.",
|
---|
91 | getLocation());
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (srcDir.equals(destDir)) {
|
---|
95 | log("Warning: src == dest", Project.MSG_WARN);
|
---|
96 | }
|
---|
97 |
|
---|
98 | DirectoryScanner ds = super.getDirectoryScanner(srcDir);
|
---|
99 |
|
---|
100 | try {
|
---|
101 | String[] files = ds.getIncludedFiles();
|
---|
102 | scanDir(srcDir, destDir, files);
|
---|
103 | if (filecopyList.size() > 0) {
|
---|
104 | log("Copying " + filecopyList.size() + " file"
|
---|
105 | + (filecopyList.size() == 1 ? "" : "s")
|
---|
106 | + " to " + destDir.getAbsolutePath());
|
---|
107 | Enumeration e = filecopyList.keys();
|
---|
108 | while (e.hasMoreElements()) {
|
---|
109 | String fromFile = (String) e.nextElement();
|
---|
110 | String toFile = (String) filecopyList.get(fromFile);
|
---|
111 | try {
|
---|
112 | getProject().copyFile(fromFile, toFile, filtering,
|
---|
113 | forceOverwrite);
|
---|
114 | } catch (IOException ioe) {
|
---|
115 | String msg = "Failed to copy " + fromFile + " to "
|
---|
116 | + toFile + " due to " + ioe.getMessage();
|
---|
117 | throw new BuildException(msg, ioe, getLocation());
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | } finally {
|
---|
122 | filecopyList.clear();
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void scanDir(File from, File to, String[] files) {
|
---|
127 | for (int i = 0; i < files.length; i++) {
|
---|
128 | String filename = files[i];
|
---|
129 | File srcFile = new File(from, filename);
|
---|
130 | File destFile;
|
---|
131 | if (flatten) {
|
---|
132 | destFile = new File(to, new File(filename).getName());
|
---|
133 | } else {
|
---|
134 | destFile = new File(to, filename);
|
---|
135 | }
|
---|
136 | if (forceOverwrite
|
---|
137 | || (srcFile.lastModified() > destFile.lastModified())) {
|
---|
138 | filecopyList.put(srcFile.getAbsolutePath(),
|
---|
139 | destFile.getAbsolutePath());
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|