使用ant脚本读取jar文件中的版本信息

使用ant脚本读取jar文件中的版本信息

步骤1、下载并配置ant环境,下载ant-contrib-1.0b3.jar,拷贝ant-contrib-1.0b3.jar到ANT_HOME/lib下面

步骤2、编写build.xml脚本

<?xml version="1.0"?>
<project name="VersionManager" default="all" basedir=".">
    <!-- Initialization variables -->
    <property environment="env" />

    <property name="lib.dir.home" value="${basedir}/lib"/>
    <property name="unpackage.dir.home" value="${basedir}/unpackage"/>
    <property name="build.dir" value="build"/>

   <path id="ant.contrib.lib">
        <fileset dir="${env.ANT_HOME}/lib">
            <include name="**/*jar" />
        </fileset>
    </path>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <path refid="ant.contrib.lib" />
        </classpath>
    </taskdef>
    <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach">
        <classpath>
            <path refid="ant.contrib.lib" />
        </classpath>
    </taskdef>
    <!-- ============================= -->
    <target name="all" depends="list-jar" description="Does the whole enchilada"/>
    <target name="unjar">
      <unjar src="${lib.dir.home}/${jarName}" dest="${unpackage.dir.home}/${jarName}" />
    </target>
  
    <target name="echo-manifest" depends="unjar">
    <loadfile property="test" srcFile="${unpackage.dir.home}/${jarName}/META-INF/MANIFEST.MF" encoding="GBK"/>
    <echo message="jar file name : ${jarName}"/>
    <echo file="${basedir}/versionInfo.txt" message="jar file name : ${jarName}&#xD;&#xA;" append="true"/>
        <echo message="${test}"/>
        <echo file="${basedir}/versionInfo.txt" message="${test}" append="true"/>
    </target>
  
    <!--
    <target name="echo-many-manifest">
        <foreach target="echo-manifest" param="jarName" list="commons-codec-1.3.jar;commons-dbcp-1.2.1.jar" delimiter=";" />
    </target>
    -->
  
    <target name="list-jar">
        <foreach list="" target="echo-manifest" param="jarPathName" delimiter=",">
            <fileset dir="${lib.dir.home}">
                <include name="**/*.jar"/>
            </fileset>
        </foreach>
    </target>
  
    <propertyregex property="jarName" input="${jarPathName}" regexp="lib\\(.*)" select="\1"/>
  
</project>

你可能感兴趣的:(ant)