首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

用Ant打Jar包-在Manifest中引用Classpath

2013-10-21 
用Ant打Jar包--在Manifest中引用Classpath?在打jar包时,往往需要在manifest文件中设置Class-Path,包含一些

用Ant打Jar包--在Manifest中引用Classpath

?

在打jar包时,往往需要在manifest文件中设置Class-Path,包含一些依赖的第三方jar包,使得以“java -jar xxx.jar”方式启动的jar包能正确找到依赖的类。

网上大多演示使用打jar包的例子都是像这样的:

<target name="jar" >

?? ? ? ?<jar destfile="${dest.jar}">

?? ? ? ? ? ?<fileset dir="${source.class}">

?? ? ? ? ? ? ? ?<include name="**/*.class"/>

?? ? ? ? ? ?</fileset>

?? ? ? ? ? ?<manifest>

?? ? ? ? ? ? ? ?<attribute name="Main-Class" value="${mianclass}"/>

?? ? ? ? ? ? ? ?<attribute name="Class-Path" value="xxxx.jar xxxx.jar xxxx.jar xxxx.jar"/>

?? ? ? ? ? ?</manifest>

?? ? ? ?</jar>

在依赖的jar包比较多的情况下,手工设置所有的jar包是非常令人崩溃而且容易出错的事情。一旦引用的外部jar包有变动,还需要到这里维护,时间一长,可能就会忘记。

经过一番查找,发现了一种配置:

<path id="classpath">

?? ? ? ?<fileset ?dir="${lib}" include="**/*.jar"/>

?</path>

<target name="jar" depends="compile">

?? ? ? ? <pathconvert property="mf.classpath" pathsep=" ">

?? ? ? ? ? ? <path refid="classpath" />

?? ? ? ? ? ? <flattenmapper />

?? ? ? ? </pathconvert>

?? ? ? ?<jar destfile="${dest.jar}">

?? ? ? ? ? ?<fileset dir="${source.class}">

?? ? ? ? ? ? ? ?<include name="**/*.class"/>

?? ? ? ? ? ?</fileset>

?? ? ? ? ? ?<manifest>

?? ? ? ? ? ? ? ?<attribute name="Main-Class" value="${mainclass}"/>

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

?? ? ? ? ? ?</manifest>

?? ? ? ?</jar>

?可以把classpath中的jar包,转换成jar Class-Path格式

热点排行