source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/BuildNumber.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 6.1 KB
Line 
1/*
2 * Copyright 2002-2005 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 */
17package org.apache.tools.ant.taskdefs;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.util.Properties;
24import org.apache.tools.ant.BuildException;
25import org.apache.tools.ant.Task;
26import org.apache.tools.ant.Project;
27import org.apache.tools.ant.util.FileUtils;
28
29/**
30 * Read, increment, and write a build number in a file
31 * It will first
32 * attempt to read a build number from a file, then set the property
33 * "build.number" to the value that was read in (or 0 if no such value). Then
34 * it will increment the build number by one and write it back out into the
35 * file.
36 *
37 * @since Ant 1.5
38 * @ant.task name="buildnumber"
39 */
40public class BuildNumber
41 extends Task {
42 /**
43 * The name of the property in which the build number is stored.
44 */
45 private static final String DEFAULT_PROPERTY_NAME = "build.number";
46
47 /** The default filename to use if no file specified. */
48 private static final String DEFAULT_FILENAME = DEFAULT_PROPERTY_NAME;
49
50 /** The File in which the build number is stored. */
51 private File myFile;
52
53
54 /**
55 * The file in which the build number is stored. Defaults to
56 * "build.number" if not specified.
57 *
58 * @param file the file in which build number is stored.
59 */
60 public void setFile(final File file) {
61 myFile = file;
62 }
63
64
65 /**
66 * Run task.
67 *
68 * @exception BuildException if an error occurs
69 */
70 public void execute()
71 throws BuildException {
72 File savedFile = myFile; // may be altered in validate
73
74 validate();
75
76 final Properties properties = loadProperties();
77 final int buildNumber = getBuildNumber(properties);
78
79 properties.put(DEFAULT_PROPERTY_NAME,
80 String.valueOf(buildNumber + 1));
81
82 // Write the properties file back out
83 FileOutputStream output = null;
84
85 try {
86 output = new FileOutputStream(myFile);
87
88 final String header = "Build Number for ANT. Do not edit!";
89
90 properties.store(output, header);
91 } catch (final IOException ioe) {
92 final String message = "Error while writing " + myFile;
93
94 throw new BuildException(message, ioe);
95 } finally {
96 if (null != output) {
97 try {
98 output.close();
99 } catch (final IOException ioe) {
100 getProject().log("error closing output stream " + ioe, Project.MSG_ERR);
101 }
102 }
103 myFile = savedFile;
104 }
105
106 //Finally set the property
107 getProject().setNewProperty(DEFAULT_PROPERTY_NAME,
108 String.valueOf(buildNumber));
109 }
110
111
112 /**
113 * Utility method to retrieve build number from properties object.
114 *
115 * @param properties the properties to retrieve build number from
116 * @return the build number or if no number in properties object
117 * @throws BuildException if build.number property is not an integer
118 */
119 private int getBuildNumber(final Properties properties)
120 throws BuildException {
121 final String buildNumber =
122 properties.getProperty(DEFAULT_PROPERTY_NAME, "0").trim();
123
124 // Try parsing the line into an integer.
125 try {
126 return Integer.parseInt(buildNumber);
127 } catch (final NumberFormatException nfe) {
128 final String message =
129 myFile + " contains a non integer build number: " + buildNumber;
130
131 throw new BuildException(message, nfe);
132 }
133 }
134
135
136 /**
137 * Utility method to load properties from file.
138 *
139 * @return the loaded properties
140 * @throws BuildException
141 */
142 private Properties loadProperties()
143 throws BuildException {
144 FileInputStream input = null;
145
146 try {
147 final Properties properties = new Properties();
148
149 input = new FileInputStream(myFile);
150 properties.load(input);
151 return properties;
152 } catch (final IOException ioe) {
153 throw new BuildException(ioe);
154 } finally {
155 if (null != input) {
156 try {
157 input.close();
158 } catch (final IOException ioe) {
159 getProject().log("error closing input stream " + ioe, Project.MSG_ERR);
160 }
161 }
162 }
163 }
164
165
166 /**
167 * Validate that the task parameters are valid.
168 *
169 * @throws BuildException if parameters are invalid
170 */
171 private void validate()
172 throws BuildException {
173 if (null == myFile) {
174 myFile = getProject().resolveFile(DEFAULT_FILENAME);
175 }
176
177 if (!myFile.exists()) {
178 try {
179 FileUtils.newFileUtils().createNewFile(myFile);
180 } catch (final IOException ioe) {
181 final String message =
182 myFile + " doesn't exist and new file can't be created.";
183
184 throw new BuildException(message, ioe);
185 }
186 }
187
188 if (!myFile.canRead()) {
189 final String message = "Unable to read from " + myFile + ".";
190
191 throw new BuildException(message);
192 }
193
194 if (!myFile.canWrite()) {
195 final String message = "Unable to write to " + myFile + ".";
196
197 throw new BuildException(message);
198 }
199 }
200}
201
Note: See TracBrowser for help on using the repository browser.