您的位置:首页 > 其它

一个常用的ANT打包脚本

2014-03-10 13:45 190 查看
suppose folder structure is like:

MyProject

---src

---lib

---target

---build.xml

And we want to generate the Jar which will contain the MANIFEST.MF having classpath and main class.

Here we will use ant to build the jar. All you need to do is to change the project name in 1st line and
Main-Class value in 55th line for build.xml, then type ant in command line. (You need to download the ant and include installation folder of ant in PATH system environment variable)

build.xml:

<project name="MyJar" default="package" basedir=".">

<!-- set global properties for this build -->

<property name="src" value="src"/>

<property name="lib" value="lib"/>

<property name="target" value="target"/>

<property name="build" value="target/classes"/>

<property name="dist" value="target/dist"/>



<target name="init">

<!-- Create the time stamp -->

<tstamp/>

<!-- Create the build directory structure used by compile -->

<mkdir dir="${build}"/>

</target>





<path id="compile.class.path">

<!--pathelement location="lib"/-->

<fileset dir="${lib}">

<include name="**/*.jar"/>

</fileset>

</path>

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

<!-- Compile the java code from ${src} into ${build} -->

<javac srcdir="${src}" destdir="${build}" >

<classpath refid="compile.class.path"/>

</javac>

</target>





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

<pathconvert property ="libs.project" pathsep =" " >

<mapper>

<chainedmapper>

<!-- remove absolute path -->

<flattenmapper/>

<!-- add lib/ prefix -->

<globmapper from ="*" to ="${lib}/*" />

</chainedmapper >

</mapper >

<path >

<!-- lib/buildhome contains all jar files, in several subdirectories -->

<fileset dir ="${lib}" >

<include name ="**/*.jar" />

</fileset >

</path >

</pathconvert >

<!-- Create the jar -->

<!-- Put everything in ${build} into the jar file -->

<jar jarfile="HadoopExec.jar" basedir="${build}">

<manifest >

<attribute name ="Main-Class" value ="xxx.XxxxYyyy" />

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

<attribute name ="Class-Path" value ="${libs.project}" />

</manifest>

</jar>

</target>

<target name="clean">

<!-- Delete the ${build} and ${dist} directory trees -->

<delete dir="${build}"/>

<delete dir="${dist}"/>

</target>

</project>



执行jar:

java -jar MyJar

这样省去了设置classpath和指定main class的麻烦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: