Using Ant to play Jar,war bag

Source: Internet
Author: User
Tags cdata tag name

--Beijing.

Today, Ant is being studied, and the following is the first to post the Ant JUnit to compile and test the code.

First put out the structure of the test project directory:

The jar package used by the project needs to be placed in Lib before compiling

1. Using ant to implement JUnit test

<?xml version= "1.0"? ><project name= "Testpoi" default= "Doc" ><!--properies--><property name= " Src.dir "value=" src "/><property name=" Report.dir "value=" Report "/><property name=" Classes.dir "value=" Classes "/><property name=" Lib.dir "Value=" Lib "/><property name=" Dist.dir "value=" Dist "/>< Property Name= "Doc.dira" value= "doc"/><!--definition classpath--><path id= "Master-classpath" ><fileset File= "${lib.dir}/*.jar"/><pathelement path= "${classes.dir}"/></path><!--initialization Task--><target Name= "Init" ></target><!--compile--><target name= "compile" depends= "init" description= "Compile the Source files "><mkdir dir=" ${classes.dir} "/><javac srcdir=" ${src.dir} "destdir=" ${classes.dir} "target=" 1.6 "includeantruntime=" on "><classpath refid=" Master-classpath "/></javac></target><!--test --><target name= "test" depends= "compile" description= "Run JUnit test" ><mkdir DIr= "${report.dir}"/><junit printsummary= "on" haltonfailure= "false" failureproperty= "tests.failed" Showoutput = "true" ><classpath refid= "Master-classpath"/><formatter type= "plain"/><batchtest todir= "${ Report.dir} "><fileset dir=" ${classes.dir} "><include name=" **/*test.* "/></fileset></ Batchtest></junit><fail if= "tests.failed" >****************************************************** One or more tests failed! Check the output ... ***************************************************************</fail></target> <!--packaged as jar--><target name= "pack" depends= "test" description= "make. jar file" ><mkdir dir= "${dist.dir}" /><jar destfile= "${dist.dir}/testpoi.jar" basedir= "${classes.dir}" ><exclude name= "**/*Test.*"/> <exclude name= "**/test*.*"/></jar></target><!--output API documentation--><target Name= "Doc" depends= " Pack "description=" Create API Doc "><mkdir dir=" ${doc.dir} "/><javadoc destdir= "${doc.dir}" author= "true" version= "true" use= "true" windowtitle= "Test API" >< Packageset dir= "${src.dir}" defaultexcludes= "yes" ><include name= "example/**"/></packageset>< doctitle><! [Cdata[
2. Ant will make the code into jar

Post-build.xml configuration

<project name= "Textpoi" default= "Deploy-textpoi" basedir= "." ><target name= "Deploy-textpoi" depends= "Init,clean,get-lib,copymapper,compile,jar" ></target>< Description>deploy-testpoi build.xml </description><property file= "${basedir}\build.properties"/> <property name= "Lib" location= "Lib"/><property name= "src" location= "src"/><property name= "Bin" location= "Bin"/><property name= "Runjar" location= "Runjar"/><!--<property name= "conf" location= " Config "/>--><property name=" resource "location=" resource "/><property name=" Lib-fdas "value=". /testpoi/lib "/><!--<property name=" Fxpayservicebin "value=". /fxpayservicebin "/>--><path id=" classpath "><fileset dir=" ${lib} "><include name=" **/*.jar "/ ></fileset></path><target name= "Init" ><tstamp/></target><target name= "clean" description= "Clean up" ><delete dir= "${bin}"/><delete dir= "${runjAR} "/><mkdir dir=" ${bin} "/><mkdir dir=" ${runjar} "/></target><target name=" Get-lib " description= "Get Java Library" ><copy todir= "${runjar}/lib" ><fileset dir= "${lib}" ><include name= "* . * "/></fileset></copy></target><target name=" Copymapper "description=" copy mapper file "> <mkdir dir= "${bin}/${mapper.path}"/><copy todir= "${bin}/${mapper.path}" ><fileset dir= "${src}/${ Mapper.path} "><include name=" **/*.xml "/></fileset></copy></target><target name=" Compile "description=" compile the source "><javac srcdir=" ${src} "destdir=" ${bin} "encoding=" UTF-8 "debug=" true " Executable= "${java_home}/bin/javac" Includeantruntime= "false" source= "1.6" target= "1.6" ><classpath refid= " Classpath "/><compilerarg value="-xlint:unchecked-xlint:deprecation "/><exclude name=" **/*.svn "/> </javac></target><target name= "jar" description= "jar" ><pathconvert PropertY= "Mf.classpath" pathsep= "><mapper><chainedmapper><flattenmapper/><globmapper from=" *. Jar "to="./lib/*.jar "/></chainedmapper></mapper><path refid=" Classpath "/></pathconvert ><jar destfile= "${runjar}/${build.jar}" basedir= "${bin}" ><!--<fileset dir= "${conf}" includes= "* *"/ >--><!--<fileset dir= "${resource}" includes= "* *"/>--><manifest><attribute name= " Main-class "value="./* "/><attribute name=" Class-path "value=" ${mf.classpath} "/></manifest></jar ></target></project>
The middle involves a configuration file build.properties

Mapper.path=equalsmodel.path=modelbuild.jar=testpoi-1.0.0.jar#build.common.jar= hbservice-common-1.0.0.jarcommon-src=${mapper.path}/** ${model.path}/**

3, using ant to Achieve the war package (war package is mainly to the Web project packaging, so that the war under Tomcat directly can use)

Always appears during the generation of the war

This reason until the quick resolution of the time to understand, the LIB package must be in the project needs to be copied into the jar, in the compile time need to use the jar inside the Lib to carry out the jar package dependency.

This problem has been occurring because there is no jar.

Because it is a Web project, you are now pasting the project's structure map


The complete build.xml is posted below

<?xml version= "1.0"?> <project name= "Hbcall" default= "war" ><path id= "Compile.classpath" >< Fileset dir= "Webroot/web-inf/lib" ><include name= "*.jar"/></fileset></path><target name= " Init "><mkdir dir=" build/classes "/><mkdir dir=" dist "/></target><!--<target name=" Compile "depends=" init "><javac destdir=" build/classes "debug=" true "srcdir=" src "><classpath refid=" Compile.classpath "/></javac></target>--><target name=" Copymapper "description=" Copy mapper file "><mkdir dir=" Build/config "/><copy todir=" Build/config "><fileset dir=" config "><include Name= "**/*.xml"/></fileset></copy></target><target name= "compile" depends= "Init" description= "Compile the source files" ><javac srcdir= "src" destdir= "build/classes" target= "1.6" Includeantruntime= "on" ><classpath refid= "Compile.classpath"/></javac></target><target Name= "war" DEPends= "Compile" ><war destfile= "Dist/antexample.war" webxml= "Webroot/web-inf/web.xml" ><fileset dir= " WebRoot "/><lib dir=" Webroot/web-inf/lib "/><classes dir=" build/classes "/></war></target ><!--<target name= "clean" ><delete dir= "dist"/><delete dir= "Build"/></target>--> </project>

The war package can be successfully generated, placed under Tomcat's WebApps, and rebooting Tomcat will compile the entire compiled code of the project correctly.

In the ointment, Build.xml did not configure the copy configuration file, you need to manually copy the configuration file to the appropriate location.

ok~ the whole ant basic situation is like this.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using Ant to play Jar,war bag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.