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 | package org.apache.tools.ant.taskdefs;
|
---|
18 |
|
---|
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.util.FileUtils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This task sets a property to the name of a temporary file.
|
---|
26 | * Unlike the Java1.2 method to create a temporary file, this task
|
---|
27 | * does work on Java1.1. Also, it does not actually create the
|
---|
28 | * temporary file, but it does guarantee that the file did not
|
---|
29 | * exist when the task was executed.
|
---|
30 | * <p>
|
---|
31 | * Examples
|
---|
32 | * <pre><tempfile property="temp.file" /></pre>
|
---|
33 | * create a temporary file
|
---|
34 | * <pre><tempfile property="temp.file" suffix=".xml" /></pre>
|
---|
35 | * create a temporary file with the .xml suffix.
|
---|
36 | * <pre><tempfile property="temp.file" destDir="build"/></pre>
|
---|
37 | * create a temp file in the build subdir
|
---|
38 | *@since Ant 1.5
|
---|
39 | *@ant.task
|
---|
40 | */
|
---|
41 |
|
---|
42 | public class TempFile extends Task {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Name of property to set.
|
---|
46 | */
|
---|
47 | private String property;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Directory to create the file in. Can be null.
|
---|
51 | */
|
---|
52 | private File destDir = null;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Prefix for the file.
|
---|
56 | */
|
---|
57 | private String prefix;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Suffix for the file.
|
---|
61 | */
|
---|
62 | private String suffix = "";
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Sets the property you wish to assign the temporary file to.
|
---|
67 | *
|
---|
68 | * @param property The property to set
|
---|
69 | * @ant.attribute group="required"
|
---|
70 | */
|
---|
71 | public void setProperty(String property) {
|
---|
72 | this.property = property;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Sets the destination directory. If not set,
|
---|
78 | * the basedir directory is used instead.
|
---|
79 | *
|
---|
80 | * @param destDir The new destDir value
|
---|
81 | */
|
---|
82 | public void setDestDir(File destDir) {
|
---|
83 | this.destDir = destDir;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Sets the optional prefix string for the temp file.
|
---|
89 | *
|
---|
90 | * @param prefix string to prepend to generated string
|
---|
91 | */
|
---|
92 | public void setPrefix(String prefix) {
|
---|
93 | this.prefix = prefix;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Sets the optional suffix string for the temp file.
|
---|
99 | *
|
---|
100 | * @param suffix suffix including any "." , e.g ".xml"
|
---|
101 | */
|
---|
102 | public void setSuffix(String suffix) {
|
---|
103 | this.suffix = suffix;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Creates the temporary file.
|
---|
109 | *
|
---|
110 | *@exception BuildException if something goes wrong with the build
|
---|
111 | */
|
---|
112 | public void execute() throws BuildException {
|
---|
113 | if (property == null || property.length() == 0) {
|
---|
114 | throw new BuildException("no property specified");
|
---|
115 | }
|
---|
116 | if (destDir == null) {
|
---|
117 | destDir = getProject().resolveFile(".");
|
---|
118 | }
|
---|
119 | FileUtils utils = FileUtils.newFileUtils();
|
---|
120 | File tfile = utils.createTempFile(prefix, suffix, destDir);
|
---|
121 | getProject().setNewProperty(property, tfile.toString());
|
---|
122 | }
|
---|
123 | }
|
---|