ANT构建JAR包时设置MANIFEST.MF的Class-Path属性的技巧  

MANIFEST.MF是ANT打jar包时自动加入META-INF目录下的一个文件,上面默认记录了Manifest-Version、Ant-Version、Created-By等属性信息,如:



Manifest-Version: 1.0


Ant-Version: Apache Ant 1.8.1


Created-By: 1.5.0-b64 (Sun Microsystems Inc.)


当jar包中的class文件引用了第三方类库时,就要在Class-Path属性中写入这些类库的引用路径,注意,这个路径是相对于第一层Archive来说的。如你把第三方的jar包都放到了你要的目标文件app.jar的lib目录下。


在ant的build文件中定义lib所在目录的classpath:



<path id="queue-classpath">


<fileset dir="${queue.dir}" includes="**/*.jar" />


</path>


target定义:



<target name="queue-deploying" description="队列部署">


<unjar src="${svn.src.basedir}/lib/sfframe.jar" dest="${tmp.queue.dir}" />


<delete file="${tmp.queue.dir}/META-INF/MANIFEST.MF" />


<pathconvert property="mf.classpath" pathsep=" " description="把上面定义的path转换成property,path中jar包的路径用空格分开">


 <mapper>


      <chainedmapper>


        <!-- 移除绝对路径 -->


        <flattenmapper />


        <!-- 加上lib前缀 -->


        <globmapper from="*" to="lib/*" />


       </chainedmapper>


     </mapper>


       <path refid="queue-classpath" description="path引用了上面定义的queue-classpath" />


    </pathconvert>


    <jar jarfile="${queue.dir}/sfframe.jar" basedir="${tmp.queue.dir}">


  <!-- define MANIFEST.MF -->


  <manifest>


  <attribute name="Main-Class" value="com.ai.sfframe.common.RmiServer" />


   <!--section name="common">


   <attribute name="Specification-Title" value="${component.name}" />


  <attribute name="Specification-Version" value="${component.version}" />


   <attribute name="Specification-Vendor" value="${component.vendor}" />


      <attribute name="Implementation-Title" value="${component.name}" />


      <attribute name="Implementation-Version" value="${component.version} ${TODAY}" />


      <attribute name="Implementation-Vendor" value="${component.vendor}" />


    </section-->


      <!-- finally, use the magically generated libs path -->


     <attribute name="Class-Path" value="${mf.classpath}" />


    </manifest>


 </jar>


</target>