一个常用的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的麻烦
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的麻烦
相关文章推荐
- iQQ 学习笔记3 :编写代码打包Ant脚本
- ant脚本打包入门
- Gradle 的一个打包脚本
- 利用ant从svn更新项目编译打包并发布项目的脚本
- Android自动打包、签名、优化、上传ANT脚本
- Android ant 自动打包脚本:自动替换友盟渠道、版本号、包名
- android 应用 Ant脚本自动编译、打包、代码混淆、签名、安装等
- 比较常用的一个ant文件
- 不会用ant打包、部署项目的工程师,不是一个好程序员(测试)
- Inno Setup一个打包安装脚本示范
- 【script】一个打包应用的脚本
- 转载关于使用Ant打包Flex的一些脚本
- 不会用ant打包、部署项目的工程师,不是一个好程序员(测试)
- 一个android sdk工程的ant脚本样例
- gradle打包android (实现外部导入签名文件、多渠道打包、导入ant脚本)
- 不会用ant打包、部署项目的工程师,不是一个好程序员
- 把所有的jar包打进一个包里的Ant脚本Merge All jars into One jar
- 整理一个ANT在J2EE项目中的应用,含预编译JSP和打包WAR/EAR文件!
- Ant将多个jar打包为一个jar
- Eclipse下配置Ant脚本 自己主动打包带签名的Android apk