网际大鱼-www.xwin32.cn

MSN: polugen@msn.com
http://www.xwin32.cn
http://www.dot9.cn
http://www.dbclick.com.cn
向IT相关的一切技术冲锋

 

ant实现JAVA程序自动增加程序的版本号

程序每编译一次,版本号就自动增加1。编译时动态生成运行时获得当前版本号的类

public class Project
{
 static  public String getBuildVersion() { return "0.0.28"; }
 static  public String getBuildNumber() {return "28";}
 static  public String getVersion() {return "0";}
 static  public String getRevision() {return "0";}
 static  public String getBuildDate() {return "Wed Sep 13 16:33:20 CST 2006";}
}
改写build.xml,在编译任务之前加入任务:
<target name="init" description="Use the Task" depends="compileversion" >
         <taskdef name="buildVersion" classname="com.tixa.base.keywordextract.config.BuildVersion" classpath="${classes.dir}"/>
         <buildVersion/>
  </target>

主执行任务代码
package com.tixa.base.keywordextract.config;
/********************************************************************
* Simple class that generates a java file containing a
* build version sequence number.
*
* Version 1.0: By polugen@163.com 
* Build sequence numbers are of the form:
*
* <version>.<subversion>:<buildnumber>
*
* The class generated exposes the
*
*  getBuildVersion();         // the whole lot
*  getBuildNumber();         // the build number
*  getVersion();          // the build/product version
*  getRevision();          // the build/product update/revision
*  toString();              // the build/product update/revision
*
* The ant task works by keeping track of the buildnumber in a file
* stored within the /var/ant directory and incrementing it after
* creating the simple java class file.
*
*********************************************************************/

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import java.util.*;
import java.io.*;

public class BuildVersion extends Task
{
  private String version="0";
  private String revision="0";
  private long  buildNumber = -1;
  private String baseDir="src";  // The root of the source tree
  private String projectName="KeyWordExtract";
  private String packageName="com.tixa.base.keywordextract.config";
 
/*********************************************************************
* Creates the file
**********************************************************************/
  public void execute() throws BuildException
  {
    buildNumber = getNextBuildNumber();
 PrintStream out = null;
 System.out.println("    [build] creating : "+getFilename());
 try
  {
    out = new PrintStream( new FileOutputStream(getFilename()));
      out.println("/***************************************************/");
       out.println("// Author:Panyg     AUTO GENERATED BY ANT ");
    out.println("// "+projectName+" Build = "+version+"."+revision+"."+buildNumber);
    out.println("// File : "+getFilename());
    out.println();
    if(packageName != null )
     {
   out.println("package "+packageName+";");
     out.println();
     }
   out.println("import java.util.Date;");
   out.println();
   out.println("public class Project");
   out.println("{");
   out.println(" static  public String getBuildVersion() { return \""+version+"."+revision+"."+buildNumber+"\"; }");
   out.println(" static  public String getBuildNumber() {return \""+buildNumber+"\";}");
   out.println(" static  public String getVersion() {return \""+version+"\";}");
   out.println(" static  public String getRevision() {return \""+revision+"\";}");
   out.println(" static  public String getBuildDate() {return \""+(new Date()).toString()+"\";}");
   out.println("}");
   out.println("/******************** END OF FILE ***************/");
   out.println();
  }
  catch(Exception ex)
  {
    throw new BuildException(ex.getMessage());
  }
  finally
  {
    if( out != null )
     {
    try{ out.close(); out=null;} catch(Exception ex1){;}
  }
  }
  }
 
/*********************************************************************
* generates the output file name
**********************************************************************/
File getFilename()
{
  String par = null;
 
  if( packageName == null ) return new File("Project.java");
  par = packageName.replace('.','/');
  par += "/Project.java";
  if(baseDir!= null) par = ""+baseDir+"/"+par;
 
  return new File(par);
}

/*********************************************************************
* Sets the product version
**********************************************************************/
  public void setVersion(String msg)
  {
    version = msg;
  }
 
 
/*********************************************************************
* Sets the product revision
**********************************************************************/
  public void setReVision(String msg)
  {
    revision = msg;
  }
 
 
 /*********************************************************************
* Sets the basedir of the source tree.
**********************************************************************/
public void setBaseDir(String dir)
{
  baseDir = dir;
}
  
 /*********************************************************************
* Sets the package name
**********************************************************************/
public void setPackageName(String packageName)
{
  this.packageName = packageName;
}
 
 /*********************************************************************
* Sets the project name of the source tree.
**********************************************************************/
public void setProductName(String name)
{
  projectName = name;
}

/*********************************************************************
* This function gets the next build sequence generating
* storage in "/var/ant" as appropriate and overwriting it
* afterwards.
*
* Returns -1 if cannot access file etc
**********************************************************************/
long getNextBuildNumber()
{
  File theFile = new File(projectName+"_"+version+"_"+revision+".txt");
  Properties prop = new Properties();
  long ver = -1;
  InputStream input = null;
  OutputStream output = null;
 
  try
  {
             // 1 - Read file
 //     System.out.println("\t\tReading Configuration in '"+theFile+"'");
      if( theFile.exists())
   {
        input = new FileInputStream(theFile);
        prop.load(input);
        input.close();
  input = null;
   }
     if( prop.containsKey("buildnumber"))
    ver = (new Integer((String)prop.get("buildnumber"))).longValue();
  else
    ver = 0;
             // 2 - update and re-write
 prop.put("buildnumber",""+(++ver));
    output = new FileOutputStream(theFile);
 prop.store(output,"Build Number System for "+projectName);
 output.close();
 output = null;
// System.out.println("Build number = "+ver);
  }
  catch(Exception ex)
  {
    ver =  -1;
  }
    finally
    {
      try
      {
        if( input != null ) input.close();
        if( output != null ) output.close();
      }
      catch(Exception n){;}
    }
  return ver;
}
 
}


/*************** END OF FILE *************************************/

 

posted on 2006-09-13 17:58  网际大鱼  阅读(3926)  评论(0编辑  收藏  举报

导航