You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuildNumber.java 6.1 kB

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