Utilize shell scripts,linux commands, open source tools,java, to Maximize the Power of Linux.
Focused on working with linux and shell, search engine technology including Chinese segmenter
Any questions please contact me at gmail: david.ullua

1/12/2007

Use eclipse with ant to deploy your java project -- from beginning of env setup to ant build

1.export project to ant build file.
2.run junit testing default build file
3.make your own build file,add unit test and junitreport to build.
4.create target of : makepackage,deployapp
5.run ant under linux terminal or win command line, or add it to scheduled tasks.
Q1: "Ant-junit, build failed: Could not create task or type of type: junit"
Q2: "ant: /bin/sh^M: bad interpreter: No such file or directory."

This tutorial shows how to use ant within eclipse to build, test, make package and deploy your app using eclipse step by step. Any questions, please feel free to contact me at david.ullua at gmail, or leave a comment. The tutorial is tested under eclipse 3.1.2, ant 1.6.5. in other higher version in the future should also works. The tutorial is oriented for readers without any knowledge about make,ant or similar build tools, while also can be as a reference for middle experienced developer.

1. export project to ant build file.
Open your java project in eclipse,select your project, right click mouse, click "export", choose "General" => "Ant BuildFiles", click "Finish", it will generate a build.xml file. The default build file includes project build target and junitreport target. we can use the default build.xml as a start.

Open build.xml, here is the first line( space after > is added for web display ) :
< basedir="." default="build" name="twoway-analyzer">< /project>
It mean the build file would take project path as basedir, and use build target as default task when running "ant" or "ant -f build.xml" in project dir without args specifying target(s).

Look at this line in build.xml:
< depends="build-subprojects,build-project" name="build">
the line means build task depends on 2 sub-tasks, and does not contain any other independent actions in the build project. that's to say, if we run "ant -f build.xml", or "ant build", it will call 2 sub-tasks: build-subprojects, build-project.

ok, now you can right click "build.xml" => "Run As" => "Ant Build" (choose the one which you can select task else it would build default tasks) , then choose targets to excute, click "run", it will run the build task.

If you met the error of "Could not create task or type of type: junit", see Q1 in the end of this article as reference.


2. run junit testing default build file
the default build file contains tests and junitreport,
you can merge seperate unit tests to one target as follows,
it combines all 4 unit tests into one target called unitTest:

<target name="unitTest">
<mkdir dir="${junit.output.dir}"/>
    <junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="com.roboo.segmenter.unitTest.DictionaryTest" todir="${junit.output.dir}"/>
<test name="com.roboo.segmenter.unitTest.MMTokenizerTest" todir="${junit.output.dir}"/>
<test name="com.roboo.segmenter.unitTest.OriginTokenizerTest" todir="${junit.output.dir}"/>
<test name="com.roboo.segmenter.unitTest.RMMTokenizerTest" todir="${junit.output.dir}"/>
<classpath refid="twoway-analyzer.classpath"/>

target>
now in eclipse you can choose unitTest to run all tests.
and you can choose junitreport, this time the report would be generated. Attention if you have chosen junitreport before unitTest, then need to adjust targets order to make sure that unitTest is done before junitreport. else the report would be generated by old data.


3.make your own build file,add unit test and junitreport to build.

every time when you export Ant Buildfiles, the default build.xml will be override. so just make your own build file. save build.xml to custbuild.xml. Also we want ant run not only as eclipse plugin but also in terminal by a single command, ok, we can add these targets to the depends of build target. find this line in custbuild.xml(which we copied from build.xml):

<target depends="build-subprojects,build-project" name="build"/>

change it to :
<target depends="build-subprojects,build-project,unitTest,junitreport" name="build"/>

now can run "ant -f custbuild.xml" in command line to run project build,unitTest and junitreport in 1 step.

when running ant, if you met the error of "ant: /bin/sh^M: bad interpreter: No such file or directory.", see Q2 as reference in the end of this article.


4.create target of : makepackage,deployapp
now we want to make a jar package for the project, then add the following target:

-- jar task to make package -->
<target name="makepackage">
< echo message="Making jar package ....."/>
< delete file="${package.name}${package.version}.jar" />
< jar destfile="${package.name}${package.version}.jar"basedir="bin" excludes="*.*,**/allwords.dict" update="false">
-- exclude normal files under bin directory -->


don't forget to add the target name to the depends for "build" target.
here package.name and package.version is predefined in the properties of the build file(after xml file's root element):
<property name="package.name" value="roboosegmenter"/>
<property name="package.version" value="1.3.2.0"/>

after making the jar package, we need to deploy it to web apps.
here app_lib_dir,tomcat_web_lib_dir is predefined in the properties of the build file(after xml file's root element)
 -- deploy segmenter lib to app_lib_dir and tomcat_web_lib -->
<target name="deployapp">
<delete>
<fileset dir="${app_lib_dir}" includes="${package.name}*jar"/> < /delete>

<copy file="${package.name}${package.version}.jar" todir="${app_lib_dir}"/>

<delete>
<fileset dir="${tomcat_web_lib_dir}" includes="${package.name}*.jar"/> < /delete

<copy file="${package.name}${package.version}.jar" todir="${tomcat_web_lib_dir}"/>
don't forget to add the deployapp target to your build define, like this:
<target depends="build-subprojects,build-project,unitTest,junitreport,makepackage,deployapp"name="build"/>


5.run ant under linux terminal or windows command line, or add it to scheduled tasks.

to build project, run unit test, make jar package and deploy app:
%ant -f custbuild.xml

to build project do not run unit test or deploy:
%ant -f custbuild.xml unitTest

to build project and run just unitTest without deploy:
%ant -f custbuild.xml build-project unittest


Q1: Ant - junit, build failed: Could not create task or type of type: junit

When running JUnit tasks in ant and you get the following error:

BUILD FAILED
path\to\...\build.xml:259: Could not create task or type of type: junit.
Ant could not find the task or a class this task relies upon.

To fix this problem, you need to copy a JUnit.jar file to your eclipse/plugins/org.apache.ant_1.65/lib directory,
Go into Eclipse then click on Window->Preferences->ant->Runtime,
select 'Ant Home Entries (Default). Click on the button 'Add External JARs'. Locate the junit.jar file you copied, select it and hit 'OK'. Hit 'Apply', then 'OK' and try your ant task again. That should fix it.


Q2:when i run ant in unix/linux terminal it shows the following error: eclipse/plugins/org.apache.ant_1.6.5/bin/ant: /bin/sh^M: bad interpreter: No such file or directory.
A2:caused by file format under dos/windows is different to unix ,
%fromdos <> file2 or use dos2unix to change the file format.

No comments:

About Me

I am a senior developer and a team leader with 3 years development experience in Suzhou, China, focus on mobile web search, linux, Java and machine learning in NLP (natural language processing). My goal is to improve people's life with computer technology.