Ant编译、FatJar编译方式
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
FatJar方式:
在Eclipse下生成jar包分很多種情況。最簡單的情況是沒有用到第三方j(luò)ar包,那么直接Export就可以生成jar包。但是如果用到了第三方j(luò)ar包,那么就比較繁瑣了,很不方便。FatJar可以解決這個(gè)問題,使用FatJar,即使包含了很多第三方j(luò)ar包,也可以很方便的生成jar包。
安裝方法:
1. eclipse在線更新方法:help > Install New Sofware > Add...
name:FatJar
URL:http://kurucz-grafika.de/fatjar
2. eclipse插件安裝方法:
鏈接:https://pan.baidu.com/s/1eS2HZpw 密碼:0dz5
解壓后,將解壓出的plugins中的文件復(fù)制到eclipse中的plugins目錄下
然后重啟eclipse,避免Fat Jar被認(rèn)不出來,在eclipse啟動(dòng)時(shí)使用-clean參數(shù)
參考文檔:http://blog.csdn.net/ithomer/article/details/16805269
?
Ant編譯方式:
環(huán)境搭建:鏈接:https://pan.baidu.com/s/1gf0EhO3 密碼:w6h3
jdk和ant下載:鏈接:https://pan.baidu.com/s/1sln2AiL 密碼:mdku
使用Ant打包是比較方便的,我們可以在項(xiàng)目根目錄下建立一個(gè)名為build.xml的xml文件,然后在xml文件里面定義我們的打包任務(wù),如下:
打jar 包時(shí)文件內(nèi)容:
<project default="jar" name="edi_salary_pfm"> <property name="lib.dir" value="lib"/> <property name="src.dir" value="src"/> <property name="resource.dir" value="${src.dir}/resources"/><property name="output.dir" value="build"/> <property name="classes.dir" value="${output.dir}/classes"/> <property name="jarname" value="edi_salary_pfm"/> <!-- 第三方j(luò)ar包的路徑 --> <path id="lib-classpath"> <fileset dir="${lib.dir}"> <include name="**/*.jar"/> </fileset> </path> <path id="base.path"><!-- 需要在window->preferences->ant->runtime->properties 加上common_lib變量,指向hrms_common_lib的根目錄 --><fileset dir="D:\source\truck\common_lib"><include name="*.jar" /></fileset></path><!-- 1. 初始化工作,如創(chuàng)建目錄等 --> <target name="init"> <mkdir dir="${classes.dir}"/> <mkdir dir="${output.dir}"/> </target> <!-- 2. 編譯 --> <target name="compile" depends="init"> <javac srcdir="${src.dir}" destdir="${classes.dir}"> <compilerarg line="-encoding UTF-8"/> <classpath refid="lib-classpath"/> <classpath refid="base.path"/> </javac><copy todir="${classes.dir}"><fileset dir="${resource.dir}"></fileset></copy></target> <!-- 3. 打包jar文件 --> <target name="jar" depends="compile"> <copy todir="${output.dir}/lib"> <fileset dir="${lib.dir}"/> </copy> <!--Create a property containing all .jar files, prefix lib/, and seperated with a space--> <pathconvert property="mf.classpath" pathsep=" "> <mapper> <chainedmapper> <!-- jar包文件只留文件名,去掉目錄信息 --> <flattenmapper/> <!-- add lib/ prefix --> <globmapper from="*" to="lib/*"/> </chainedmapper> </mapper> <path refid="lib-classpath"/> <path refid="base.path"/> </pathconvert> <!-- jar文件的輸出路徑 需要比對(duì)一下先前的打包buid.xml文件看看要過濾掉那些文件 --><jar jarfile="${output.dir}/${jarname}.jar"><fileset dir="${classes.dir}"><!--<exclude name="**/config/**"/><exclude name="**/spring/dataAccessContext.xml"/><exclude name="ibatis/sql-map-config.xml"/><exclude name="spy.properties"/>--></fileset></jar></target> </project>?
打war包文件內(nèi)容:
<project name="empleave" default="war-with-compiledjsp-weblogic"> <!--default 默認(rèn)執(zhí)行的任務(wù),此處任務(wù)包含tomcat和weblogic的任務(wù),因此可針對(duì)項(xiàng)目運(yùn)行環(huán)境選擇打包相應(yīng)的war包 --><description>empleave.application</description><property name="project.name" value="empleave" /><property name="project.version" value="1.0" /><property name="project.encoding" value="UTF-8" /><property name="dir.src.java" value="${basedir}/src/java" /><property name="dir.src.resource" value="${basedir}/src/resources" /><property name="dir.src.test" value="${basedir}/src/test" /><property name="dir.src.web" value="${basedir}/WebRoot" /><property name="dir.src.lib" value="${basedir}" /><property name="dir.build" value="build" /><property name="dir.build.doc" value="${dir.build}/docs" /><property name="dir.build.doc.api" value="${dir.build.doc}/api" /><property name="dir.build.classes" value="${dir.build}/classes" /><property name="dir.build.test-classes" value="${dir.build}/test-classes" /><property name="dir.build.testreport" value="${dir.build}/test-reports" /><property name="dir.build.jspc" value="${dir.build}/jspc" /><property name="compile.debug" value="true" /><property name="javac.target" value="1.5" /><path id="base.path"><!-- 需要在window->preferences->ant->runtime->properties 加上common_lib變量,指向hrms_common_lib的根目錄 --><fileset dir="D:\source\branches_compile_all\common_lib"><include name="*.jar" /></fileset></path><!-- project class path --><path id="classpath"><path refid="base.path" /><fileset dir="${dir.src.lib}"><include name="*.jar" /></fileset><fileset dir="${dir.src.web}/WEB-INF/lib"><include name="*.jar" /></fileset></path><!--tomcat jsp compile class path --><path id="tomcatjspcclasspath"><fileset dir="${tomcat.jspc.lib}"><include name="*.jar" /></fileset><path refid="base.path" /><fileset dir="${dir.src.lib}"><include name="*.jar" /></fileset><fileset dir="${dir.src.web}/WEB-INF/lib"><include name="*.jar" /></fileset></path><!--weblogic jsp compile class path <path id="weblogicjspcclasspath"><fileset dir="${weblogic.jspc.lib}"><include name="*.jar" /></fileset><path refid="base.path"/><fileset dir="${dir.src.lib}"><include name="*.jar" /></fileset> <fileset dir="${dir.src.web}/WEB-INF/lib"><include name="*.jar" /></fileset></path>--><path id="weblogicjspcclasspath"><fileset dir="D:\branches_compile_all\weblogic9.2.2.0_lib"><include name="*.jar" /></fileset><fileset dir="C:\Program Files (x86)\Java\jdk1.5.0_22\lib"><include name="*.jar" /></fileset><path refid="base.path" /><fileset dir="${dir.src.lib}"><include name="*.jar" /></fileset><fileset dir="${dir.src.web}/WEB-INF/lib"><include name="*.jar" /></fileset></path><!-- init task --><target name="init" depends="clean"><mkdir dir="${dir.build}" /><mkdir dir="${dir.build.doc}" /><mkdir dir="${dir.build.doc.api}" /><mkdir dir="${dir.build.classes}" /><mkdir dir="${dir.build.test-classes}" /><mkdir dir="${dir.build.testreport}" /><mkdir dir="${dir.build.jspc}" /></target><!-- default build task --><target name="tomcatbuild" depends="clean"><ant target="jar" /><ant target="tomcatwar" /><ant target="javadoc" /><echo message="Build completed!" /></target><!-- default build task --><target name="weblogicbuild" depends="clean"><ant target="jar" /><ant target="weblogicwar" /><ant target="javadoc" /><echo message="Build completed!" /></target><!-- tomcat jspc --><target name="tomcatjspc"><taskdef classname="org.apache.jasper.JspC" name="tomcatjasper"><classpath refid="tomcatjspcclasspath" /></taskdef><tomcatjasper javaencoding="UTF-8" trimspaces="true" uriroot="${dir.src.web}" webxmlfragment="${dir.src.web}/WEB-INF/jsp-web.xml" outputDir="${dir.build.jspc}" failonerror="false" /><loadfile property="jspc.webxml.fragment.contents" srcFile="${dir.src.web}/WEB-INF/jsp-web.xml" /><copy file="${dir.src.web}/WEB-INF/web.xml" tofile="${dir.src.web}/WEB-INF/merge-web.xml" /><replace file="${dir.src.web}/WEB-INF/merge-web.xml" encoding="UTF-8"><replacefilter token="<!--@JSPC-INSERT@-->" value="${jspc.webxml.fragment.contents}" /></replace></target><target name="tomcat-compile-jsp" depends="tomcatjspc"><javac destdir="${dir.src.web}/WEB-INF/classes" optimize="on" debug="${compile.debug}" failonerror="false" srcdir="${dir.build.jspc}" encoding="UTF-8" target="${javac.target}"><classpath refid="tomcatjspcclasspath" /><include name="**/*.*" /><exclude name="**/storage/query_jsp*.*" /><exclude name="loader/**/*.*" /><exclude name="*.ser" /></javac></target><!-- tomcat jspc --><!-- weblogic jspc --><target name="weblogicjspc"><java classname="weblogic.jspc" classpathref="weblogicjspcclasspath" fork="true" failonerror="false" maxmemory="512m"><arg line="-webapp ${dir.src.web} -d ${dir.src.web}/WEB-INF/classes -compileAll" /></java></target><target name="weblogic-compile-jsp" depends="weblogicjspc"><!--<javac destdir="${dir.src.web}/WEB-INF/classes" optimize="on" debug="${compile.debug}" failonerror="false" srcdir="${dir.build.jspc}" encoding="UTF-8" target="${javac.target}"><classpath refid="weblogicjspcclasspath" /><include name="**/*.*" /><exclude name="**/storage/query_jsp*.*" /><exclude name="loader/**/*.*" /><exclude name="*.ser" /></javac>--></target><!-- weblogic jspc --><target name="deploy-web" depends="compile"><mkdir dir="${dir.src.web}/WEB-INF/classes" /><copy todir="${dir.src.web}/WEB-INF/classes"><fileset dir="${dir.build.classes}"></fileset></copy></target><!-- archive the web app folder with war--><target name="war" depends="deploy-web"><war destfile="${dir.build}/${project.name}.war" webxml="${dir.src.web}/WEB-INF/web.xml"><fileset dir="${dir.src.web}"><include name="**/*.*" /><exclude name="**/build.xml" /><exclude name="**/build.properties" /><exclude name="**/deploy.xml" /><exclude name="**/resin-web.xml" /><exclude name="**/targets.xml" /><exclude name="**/undeploy.xml" /><!--<exclude name="**/weblogic.xml"/>--><exclude name="**/jsp-web.xml" /><exclude name="**/merge-web.xml" /><exclude name="**/portal-filter.config" /><exclude name="**/portal-listener.config" /><exclude name="html/mytest/" /><exclude name="html/sample/" /><exclude name="html/test/" /></fileset></war></target><!-- archive the web app folder with war tomcat--><target name="war-with-compiledjsp-tomcat" depends="deploy-web,tomcat-compile-jsp"><war destfile="${dir.build}/${project.name}.war" webxml="${dir.src.web}/WEB-INF/web.xml"><fileset dir="${dir.src.web}"><include name="**/*.*" /><exclude name="**/build.xml" /><exclude name="**/build.properties" /><exclude name="**/deploy.xml" /><exclude name="**/resin-web.xml" /><exclude name="**/targets.xml" /><exclude name="**/undeploy.xml" /><!--<exclude name="**/weblogic.xml"/><exclude name="**/jsp-web.xml" /><exclude name="**/merge-web.xml" />--></fileset></war></target><!-- archive the web app folder with war weblogic--><target name="war-with-compiledjsp-weblogic" depends="deploy-web,weblogic-compile-jsp"><war destfile="${dir.build}/${project.name}.war" webxml="${dir.src.web}/WEB-INF/web.xml"><fileset dir="${dir.src.web}"><include name="**/*.*" /><exclude name="**/build.xml" /><exclude name="**/build.properties" /><exclude name="**/deploy.xml" /><exclude name="**/resin-web.xml" /><exclude name="**/targets.xml" /><exclude name="**/undeploy.xml" /><!--<exclude name="**/weblogic.xml"/><exclude name="**/jsp-web.xml" /><exclude name="**/merge-web.xml" />--></fileset></war></target><!-- clean all the builded resources --><target name="clean" depends="clean-web"><delete dir="${dir.build}" excludes=".svn" includeemptydirs="true" /></target><!-- clean all the builded resources --><target name="clean-web"><delete dir="${dir.src.web}/WEB-INF/classes" excludes=".svn" includeemptydirs="true" /></target><!-- compile java source only --><target name="compile" depends="init"><javac destdir="${dir.build.classes}" encoding="${project.encoding}" nowarn="true" debug="${compile.debug}" target="${javac.target}"><src><pathelement location="${dir.src.java}" /></src><classpath refid="classpath" /></javac><copy todir="${dir.build.classes}"><fileset dir="${dir.src.java}"><exclude name="**/*.java" /><include name="**/*.*" /></fileset><fileset dir="${dir.src.resource}"><exclude name="**/*.java" /><include name="**/*.*" /></fileset></copy></target><!-- jar task,jar the compiled java classes,does not include the test classes. --><target name="jar" depends="compile" description="Create the jar"><jar jarfile="${dir.build}/${project.name}-${project.version}.jar"><fileset dir="${dir.build.classes}"><include name="**/*" /></fileset></jar><copy file="${dir.build}/${project.name}-${project.version}.jar" tofile="${dir.build}/${project.name}.jar" /></target><!-- javadoc task,generate java source code api doc,does not include the test source--><target name="javadoc"><javadoc Encoding="${project.encoding}" sourcepath="${dir.src.java}" destdir="${dir.build.doc.api}" author="true" version="true" use="true" packagenames="net.bingosoft.*" Windowtitle="api documentation"><classpath refid="classpath" /></javadoc></target> </project>?
轉(zhuǎn)載于:https://my.oschina.net/yabushan/blog/1596830
總結(jié)
以上是生活随笔為你收集整理的Ant编译、FatJar编译方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不为人知的网络编程(七):如何让不可靠的
- 下一篇: Kubernetes学习笔记(一)