/************************************************************************** /* This class implements the simple ant-task "VersionManager". /* /* Copyright (c) 2004 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ import java.io.*; import java.util.*; import org.apache.tools.ant.*; /** This class implements the ant-task "VersionManager". The tasks manages major-version, minor-version and patch-level of a project. If you increment the major-version, both minor-version and patch-level are set to zero. If you increment the minor-version, the patch-level is set to zero.

Basically, the version is an ant-property: version=a.b.c in a file (default: version.properties). The VersionManager task reads the file, updates the corresponding values, and rewrites it. @version $Revision: 1.5 $ @author $Author: bablokb $ */ public class VersionManager extends Task { /** The File with the version-information. */ private File iVersionFile = new File("version.properties"); /** The increment-type. */ private String iInc; /** Increment-type constant. */ private final String TYPE_MAJOR = "major", TYPE_MINOR = "minor", TYPE_PL = "patchlevel"; /** Version variable. */ private int iMajorVersion, iMinorVersion, iPatchLevel; ////////////////////////////////////////////////////////////////////////////// /** Set the File with the version-information. */ public void setFile(File versionFile) { iVersionFile = versionFile; } ////////////////////////////////////////////////////////////////////////////// /** Set the increment-type. */ public void setInc(String inc) { iInc = inc; validateInc(); } ////////////////////////////////////////////////////////////////////////////// /** Validate the increment-type. */ private void validateInc() { if (! iInc.equals(TYPE_MAJOR) && ! iInc.equals(TYPE_MINOR) && ! iInc.equals(TYPE_PL)) throw new BuildException("Invalid value of inc: " + iInc); } ////////////////////////////////////////////////////////////////////////////// /** Increment the type (major, minor or patch-level) - this executes the task. */ public void execute() { try { readFile(); if (iInc.equals(TYPE_MAJOR)) { log("incrementing major-version"); iMajorVersion += 1; iMinorVersion = 0; iPatchLevel = 0; } else if (iInc.equals(TYPE_MINOR)) { log("incrementing minor-version"); iMinorVersion += 1; iPatchLevel = 0; } else { log("incrementing patch-level"); iPatchLevel += 1; } writeFile(); log("new version is: " + iMajorVersion + "." + iMinorVersion + "." + iPatchLevel); } catch (IOException e) { throw new BuildException("Failed with IOException: " + e.toString()); } } ////////////////////////////////////////////////////////////////////////////// /** Read the version-file and set variables. If it does not exist, set default-values. */ private void readFile() throws IOException { if (!iVersionFile.exists()) { log("Version-File " + iVersionFile.toString() + " does not exist."); iMajorVersion = 0; iMinorVersion = 0; iPatchLevel = 0; return; } BufferedReader reader = new BufferedReader(new FileReader(iVersionFile)); String line; while ((line = reader.readLine()) != null) { if (line.startsWith("app.version")) { StringTokenizer tok = new StringTokenizer(line," ="); tok.nextToken(); // skip app.version = iMajorVersion = Integer.parseInt(tok.nextToken(".= ")); iMinorVersion = Integer.parseInt(tok.nextToken()); iPatchLevel = Integer.parseInt(tok.nextToken()); break; } } reader.close(); } ////////////////////////////////////////////////////////////////////////////// /** Save the version-file after update. */ private void writeFile() throws IOException { PrintWriter writer = new PrintWriter(new FileWriter(iVersionFile)); writer.println("# created by VersionManager " + (new Date()).toString()); writer.println("#\n# modify at your own risk!\n"); writer.println("app.version = " + iMajorVersion + "." + iMinorVersion + "." + iPatchLevel); writer.close(); } }