1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.util.Properties ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.util.FileUtils; 29 30 41 public class BuildNumber 42 extends Task { 43 46 private static final String DEFAULT_PROPERTY_NAME = "build.number"; 47 48 49 private static final String DEFAULT_FILENAME = DEFAULT_PROPERTY_NAME; 50 51 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 52 53 54 private File myFile; 55 56 57 63 public void setFile(final File file) { 64 myFile = file; 65 } 66 67 68 73 public void execute() 74 throws BuildException { 75 File savedFile = myFile; 77 validate(); 78 79 final Properties properties = loadProperties(); 80 final int buildNumber = getBuildNumber(properties); 81 82 properties.put(DEFAULT_PROPERTY_NAME, 83 String.valueOf(buildNumber + 1)); 84 85 FileOutputStream output = null; 87 88 try { 89 output = new FileOutputStream (myFile); 90 91 final String header = "Build Number for ANT. Do not edit!"; 92 93 properties.store(output, header); 94 } catch (final IOException ioe) { 95 final String message = "Error while writing " + myFile; 96 97 throw new BuildException(message, ioe); 98 } finally { 99 if (null != output) { 100 try { 101 output.close(); 102 } catch (final IOException ioe) { 103 log("error closing output stream " + ioe, Project.MSG_ERR); 104 } 105 } 106 myFile = savedFile; 107 } 108 109 getProject().setNewProperty(DEFAULT_PROPERTY_NAME, 111 String.valueOf(buildNumber)); 112 } 113 114 115 122 private int getBuildNumber(final Properties properties) 123 throws BuildException { 124 final String buildNumber = 125 properties.getProperty(DEFAULT_PROPERTY_NAME, "0").trim(); 126 127 try { 129 return Integer.parseInt(buildNumber); 130 } catch (final NumberFormatException nfe) { 131 final String message = 132 myFile + " contains a non integer build number: " + buildNumber; 133 throw new BuildException(message, nfe); 134 } 135 } 136 137 138 144 private Properties loadProperties() 145 throws BuildException { 146 FileInputStream input = null; 147 148 try { 149 final Properties properties = new Properties (); 150 151 input = new FileInputStream (myFile); 152 properties.load(input); 153 return properties; 154 } catch (final IOException ioe) { 155 throw new BuildException(ioe); 156 } finally { 157 if (null != input) { 158 try { 159 input.close(); 160 } catch (final IOException ioe) { 161 log("error closing input stream " + ioe, Project.MSG_ERR); 162 } 163 } 164 } 165 } 166 167 168 173 private void validate() 174 throws BuildException { 175 if (null == myFile) { 176 myFile = FILE_UTILS.resolveFile(getProject().getBaseDir(), DEFAULT_FILENAME); 177 } 178 179 if (!myFile.exists()) { 180 try { 181 FILE_UTILS.createNewFile(myFile); 182 } catch (final IOException ioe) { 183 final String message = 184 myFile + " doesn't exist and new file can't be created."; 185 throw new BuildException(message, ioe); 186 } 187 } 188 189 if (!myFile.canRead()) { 190 final String message = "Unable to read from " + myFile + "."; 191 throw new BuildException(message); 192 } 193 194 if (!myFile.canWrite()) { 195 final String message = "Unable to write to " + myFile + "."; 196 throw new BuildException(message); 197 } 198 } 199 } 200 201 | Popular Tags |