java project打包生成jar包(通用)
1. 在工程目录下新建一个build.xml文件,如下图所示,注意必须是在工程目录下,而不是在工程目录的src目录里。
2.编写ant脚本,内容如下,jar文件名称(com.anllin.rup.bookshop.stub.jar)需要更改,其他通用。
<?xml version="1.0" encoding="utf-8"?> <project name="buildJarFile" default="compile" basedir="."> <!-- the directory containing source code--> <property name="src.dir" value="src" /> <!-- Temporary build directories --> <property name="build.dir" value="build" /> <property name="build.classes" value="${build.dir}/classes" /> <property name="build.lib" value="${build.dir}/lib" /> <!-- Target to create the build directories prior to the compile target--> <target name="init"> <mkdir dir="${build.dir}" /> <mkdir dir="${build.classes}" /> <mkdir dir="${build.lib}" /> </target> <target name="clean" description="Remove all generted files"> <delete dir="${build.dir}" /> </target> <target name="compile" depends="init" description="Compiles all source code"> <javac srcdir="${src.dir}" destdir="${build.classes}" /> </target> <target name="jar" depends="compile" description="Generates jar file in 'dist' diretory"> <!-- Exclude unit tests from the final JAR file--> <jar jarfile="${build.lib}/com.anllin.rup.bookshop.stub.jar" basedir="${build.classes}" excludes="**/*Test.class" /> </target> <target name="alll" depends="clean, jar" description="Cleans, complies, then builds the JAR files."> </target> </project>
3.选中build.xml,右键选择run as -> ant build, 执行脚本后,生成build目录如下图: