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 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.File;
  56. import java.io.FileInputStream;
  57. import java.io.FileOutputStream;
  58. import java.io.IOException;
  59. import java.util.Properties;
  60. import org.apache.tools.ant.BuildException;
  61. import org.apache.tools.ant.Task;
  62. import org.apache.tools.ant.util.FileUtils;
  63. /**
  64. * Read, increment, and write a build number in a file
  65. * It will first
  66. * attempt to read a build number from a file, then set the property
  67. * "build.number" to the value that was read in (or 0 if no such value). Then
  68. * it will increment the build number by one and write it back out into the
  69. * file.
  70. *
  71. * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  72. * @version $Revision$ $Date$
  73. * @since Ant 1.5
  74. * @ant.task name="buildnumber"
  75. */
  76. public class BuildNumber
  77. extends Task {
  78. /**
  79. * The name of the property in which the build number is stored.
  80. */
  81. private static final String DEFAULT_PROPERTY_NAME = "build.number";
  82. /** The default filename to use if no file specified. */
  83. private static final String DEFAULT_FILENAME = DEFAULT_PROPERTY_NAME;
  84. /** The File in which the build number is stored. */
  85. private File m_file;
  86. /**
  87. * The file in which the build number is stored. Defaults to
  88. * "build.number" if not specified.
  89. *
  90. * @param file the file in which build number is stored.
  91. */
  92. public void setFile(final File file) {
  93. m_file = file;
  94. }
  95. /**
  96. * Run task.
  97. *
  98. * @exception BuildException if an error occurs
  99. */
  100. public void execute()
  101. throws BuildException {
  102. File savedFile = m_file;// may be altered in validate
  103. validate();
  104. final Properties properties = loadProperties();
  105. final int buildNumber = getBuildNumber(properties);
  106. properties.put(DEFAULT_PROPERTY_NAME,
  107. String.valueOf(buildNumber + 1));
  108. // Write the properties file back out
  109. FileOutputStream output = null;
  110. try {
  111. output = new FileOutputStream(m_file);
  112. final String header = "Build Number for ANT. Do not edit!";
  113. properties.save(output, header);
  114. } catch (final IOException ioe) {
  115. final String message = "Error while writing " + m_file;
  116. throw new BuildException(message, ioe);
  117. } finally {
  118. if (null != output) {
  119. try {
  120. output.close();
  121. } catch (final IOException ioe) {
  122. }
  123. }
  124. m_file = savedFile;
  125. }
  126. //Finally set the property
  127. getProject().setNewProperty(DEFAULT_PROPERTY_NAME,
  128. String.valueOf(buildNumber));
  129. }
  130. /**
  131. * Utility method to retrieve build number from properties object.
  132. *
  133. * @param properties the properties to retrieve build number from
  134. * @return the build number or if no number in properties object
  135. * @throws BuildException if build.number property is not an integer
  136. */
  137. private int getBuildNumber(final Properties properties)
  138. throws BuildException {
  139. final String buildNumber =
  140. properties.getProperty(DEFAULT_PROPERTY_NAME, "0").trim();
  141. // Try parsing the line into an integer.
  142. try {
  143. return Integer.parseInt(buildNumber);
  144. } catch (final NumberFormatException nfe) {
  145. final String message =
  146. m_file + " contains a non integer build number: " + buildNumber;
  147. throw new BuildException(message, nfe);
  148. }
  149. }
  150. /**
  151. * Utility method to load properties from file.
  152. *
  153. * @return the loaded properties
  154. * @throws BuildException
  155. */
  156. private Properties loadProperties()
  157. throws BuildException {
  158. FileInputStream input = null;
  159. try {
  160. final Properties properties = new Properties();
  161. input = new FileInputStream(m_file);
  162. properties.load(input);
  163. return properties;
  164. } catch (final IOException ioe) {
  165. throw new BuildException(ioe);
  166. } finally {
  167. if (null != input) {
  168. try {
  169. input.close();
  170. } catch (final IOException ioe) {
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Validate that the task parameters are valid.
  177. *
  178. * @throws BuildException if parameters are invalid
  179. */
  180. private void validate()
  181. throws BuildException {
  182. if (null == m_file) {
  183. m_file = getProject().resolveFile(DEFAULT_FILENAME);
  184. }
  185. if (!m_file.exists()) {
  186. try {
  187. FileUtils.newFileUtils().createNewFile(m_file);
  188. } catch (final IOException ioe) {
  189. final String message =
  190. m_file + " doesn't exist and new file can't be created.";
  191. throw new BuildException(message, ioe);
  192. }
  193. }
  194. if (!m_file.canRead()) {
  195. final String message = "Unable to read from " + m_file + ".";
  196. throw new BuildException(message);
  197. }
  198. if (!m_file.canWrite()) {
  199. final String message = "Unable to write to " + m_file + ".";
  200. throw new BuildException(message);
  201. }
  202. }
  203. }