You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tutorial-HelloWorldWithAnt.html 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <html>
  2. <head>
  3. <title>Tutorial: Hello World with Ant</title>
  4. <meta name="author" content="Jan Mat&egrave;rne">
  5. <style type="text/css">
  6. <!--
  7. .code { background: #EFEFEF; margin-top: }
  8. .output { color: #FFFFFF; background: #837A67; }
  9. -->
  10. </style>
  11. </head>
  12. <body>
  13. <h1>Tutorial: Hello World with Ant</h1>
  14. <p>This document provides a step by step tutorial for starting java programming with Ant.
  15. It does <b>not</b> contain deeper knowledge about Java or Ant. This tutorial has the goal
  16. to let you see, how to do the easiest steps in Ant.</p>
  17. <h2>Content</h2>
  18. <p><ul>
  19. <li><a href="#prepare">Preparing the project</a></li>
  20. <li><a href="#four-steps">Enhance the build file</a></li>
  21. <li><a href="#enhance">Enhance the build file</a></li>
  22. <li><a href="#ext-libs">Using external libraries</a></li>
  23. </ul></p>
  24. <a name="prepare"></a>
  25. <h2>Preparing the project</h2>
  26. <p>We want to separate the source from the generated files, so our java source files will
  27. be in <tt>src</tt> folder. All generated files should be under <tt>build</tt>, and there
  28. splitted into several subdirectories for the individual steps: <tt>classes</tt> for our compiled
  29. files and <tt>jar</tt> for our own JAR-file.</p>
  30. <p>The later directories are created by our buildfile, so we have to create only the <tt>src</tt>
  31. directory. (Because I am working on Windows, here is the win-syntax - translate to your shell):</p>
  32. <pre class="code">
  33. md src
  34. </pre>
  35. <p>This is not a Java tutorial, so just write this code into <tt>src/oata/HelloWorld.java</tt> -
  36. you should guess it's meaning ;-)</p>
  37. <pre class="code">
  38. package oata;
  39. public class HelloWorld {
  40. public static void main(String[] args) {
  41. System.out.println("Hello World");
  42. }
  43. }
  44. </pre>
  45. <a name="four-steps"></a>
  46. <h2>Four steps to a running application</h2>
  47. <p>Oki-doki - now we have to think about our build process. We <i>have</i> to compile our code, otherwise we couldn't
  48. start the program. Oh - "start" - yes, we could provide a target for that. We <i>should</i> package our application.
  49. Now it's only one class - but if you want to provide a download, no one would download several hundreds files ...
  50. (think about a complex Swing GUI :) - so let us create a jar file. A startable jar file would be nice ... And it's a
  51. good practise to have a "clean" target, which deletes all the generated stuff. Many failures could be solved just
  52. by a "clean build" :-)</p>
  53. <p>The buildfile describing that would be:</p>
  54. <pre class="code">
  55. &lt;project&gt;
  56. &lt;target name="clean"&gt;
  57. &lt;delete dir="build"/&gt;
  58. &lt;/target&gt;
  59. &lt;target name="compile"&gt;
  60. &lt;mkdir dir="build/classes"/&gt;
  61. &lt;javac srcdir="src" destdir="build/classes"/&gt;
  62. &lt;/target&gt;
  63. &lt;target name="jar"&gt;
  64. &lt;mkdir dir="build/jar"/&gt;
  65. &lt;jar destfile="build/jar/HelloWorld.jar" basedir="build/classes"&gt;
  66. &lt;manifest&gt;
  67. &lt;attribute name="Main-Class" value="oata.HelloWorld"/&gt;
  68. &lt;/manifest&gt;
  69. &lt;/jar&gt;
  70. &lt;/target&gt;
  71. &lt;target name="run"&gt;
  72. &lt;java jar="build/jar/HelloWorld.jar" fork="true"/&gt;
  73. &lt;/target&gt;
  74. &lt;/project&gt;
  75. </pre>
  76. <p>Now you can compile, package and run the application via</p>
  77. <pre class="code">
  78. ant compile
  79. ant jar
  80. ant run
  81. </pre>
  82. <p>Or shorter with</p>
  83. <pre class="code">
  84. ant compile jar run
  85. </pre>
  86. <a name="enhance"></a>
  87. <h2>Enhance the build file</h2>
  88. </p>Ok, the build works - but it is not as nice as it should: many time you are referencing the same directories,
  89. main-class and jar-name are hard coded, and while invocation you have to remember the right order of build steps.</p>
  90. <p>The first and second point would be adressed with <i>properties</i>, the thirs with a special property - an attribute
  91. of the &lt;project&gt;-tag and the fourth problem can be solved using dependencies.</p>
  92. <pre class="code">
  93. &lt;project name="HelloWorld" basedir="." default="main"&gt;
  94. &lt;property name="src.dir" value="src"/&gt;
  95. &lt;property name="build.dir" value="build"/&gt;
  96. &lt;property name="classes.dir" value="${build.dir}/classes"/&gt;
  97. &lt;property name="jar.dir" value="${build.dir}/jar"/&gt;
  98. &lt;property name="main-class" value="oata.HelloWorld"/&gt;
  99. &lt;target name="clean"&gt;
  100. &lt;delete dir="${build.dir}"/&gt;
  101. &lt;/target&gt;
  102. &lt;target name="compile"&gt;
  103. &lt;mkdir dir="${classes.dir}"/&gt;
  104. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}"/&gt;
  105. &lt;/target&gt;
  106. &lt;target name="jar" depends="compile"&gt;
  107. &lt;mkdir dir="${jar.dir}"/&gt;
  108. &lt;jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"&gt;
  109. &lt;manifest&gt;
  110. &lt;attribute name="Main-Class" value="${main-class}"/&gt;
  111. &lt;/manifest&gt;
  112. &lt;/jar&gt;
  113. &lt;/target&gt;
  114. &lt;target name="run" depends="jar"&gt;
  115. &lt;java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/&gt;
  116. &lt;/target&gt;
  117. &lt;target name="clean-build" depends="clean,jar"/&gt;
  118. &lt;target name="main" depends="clean,run"/&gt;
  119. &lt;/project&gt;
  120. </pre>
  121. <p>Now it's easier, just do a <tt>ant</tt> and you will get</p>
  122. <pre class="output">
  123. Buildfile: build.xml
  124. clean:
  125. compile:
  126. [mkdir] Created dir: C:\...\build\classes
  127. [javac] Compiling 1 source file to C:\...\build\classes
  128. jar:
  129. [mkdir] Created dir: C:\...\build\jar
  130. [jar] Building jar: C:\...\build\jar\HelloWorld.jar
  131. run:
  132. [java] Hello World
  133. main:
  134. BUILD SUCCESSFUL
  135. </pre>
  136. <a name="ext-libs"></a>
  137. <h2>Using external libraries</h2>
  138. <p>Somehow told us not to use syso-statements. For log-Statements we should use a Logging-API - customizable on a high
  139. degree (including switching off during usual life (= not development) execution). We use Log4J, because <ul>
  140. <li>it is not part of the JDK (1.4+) and we want to show how to use external libs</li>
  141. <li>it can run under JDK 1.2 (as Ant)</li>
  142. <li>it's highly configurable</li>
  143. <li>it's from Apache :-)</li>
  144. </ul></p>
  145. <p>We store our external libraries in a new directory <tt>lib</tt>. Log4J can be
  146. <a href="http://www.apache.org/dist/logging/log4j/1.2.9/logging-log4j-1.2.9.zip">downloaded</a> from Logging's Homepage.
  147. Create the <tt>lib</tt> directory and extract the log4j-1.2.9.jar into that lib-directory. After that we have to modify
  148. our java source to use that library and our buildfile so that this library could be accessed during compilation and run.
  149. </p>
  150. <p>Working with Log4J is documented inside its manual. Here we use the <i>MyApp</i>-example from the
  151. <a href="http://logging.apache.org/log4j/docs/manual.html">Short Manual</a>. First we have to modify the java source to
  152. use the logging framework:</p>
  153. <pre class="code">
  154. package oata;
  155. <b>import org.apache.log4j.Logger;</b>
  156. <b>import org.apache.log4j.BasicConfigurator;</b>
  157. public class HelloWorld {
  158. <b>static Logger logger = Logger.getLogger(HelloWorld.class);</b>
  159. public static void main(String[] args) {
  160. <b>BasicConfigurator.configure();</b>
  161. <font color="blue"><b>logger.info("Hello World");</b></font>
  162. }
  163. }
  164. </pre>
  165. <p>Most of the modifications are "framework overhead" which has to be done once. The red line is our "old System-out"
  166. statement.</p>
  167. <p>Don't try to run <tt>ant</tt> - you will only get lot of compiler errors. Log4J is not inside the classpath so we have
  168. to do a little work here. But do not change the CLASSPATH environment variable! This is only for this project and maybe
  169. you would break other environments (this is one of the most famous mistakes when working with Ant). We introduce Log4J
  170. into our buildfile:</p>
  171. <pre class="code">
  172. &lt;project name="HelloWorld" basedir="." default="main"&gt;
  173. ...
  174. <b>&lt;property name="lib.dir" value="lib"/&gt;</b>
  175. <b>&lt;path id="classpath"&gt;</b>
  176. <b>&lt;fileset dir="${lib.dir}" includes="**/*.jar"/&gt;</b>
  177. <b>&lt;/path&gt;</b>
  178. ...
  179. &lt;target name="compile"&gt;
  180. &lt;mkdir dir="${classes.dir}"/&gt;
  181. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}" <b>classpathref="classpath"</b>/&gt;
  182. &lt;/target&gt;
  183. &lt;target name="run" depends="jar"&gt;
  184. &lt;java fork="true" <b>classname="${main-class}"</b>&gt;
  185. <b>&lt;classpath&gt;</b>
  186. <b>&lt;path refid="classpath"/&gt;</b>
  187. <font color="blue"><b>&lt;path location="${jar.dir}/${ant.project.name}.jar"/&gt;</b></font>
  188. <b>&lt;/classpath&gt;</b>
  189. &lt;/java&gt;
  190. &lt;/target&gt;
  191. ...
  192. &lt;/project&gt;
  193. </pre>
  194. <p>In this example we start our application not via its Main-Class manifest-attribute, because we could not provide
  195. a jarname <i>and</i> a classpath. So add our class in the red line to the already defined path and start as usual. Running
  196. <tt>ant</tt> would give (after the usual compile stuff):</p>
  197. <pre class="output">
  198. [java] 0 [main] INFO oata.HelloWorld - Hello World
  199. </pre>
  200. <p>What's that? <ul>
  201. <li><i>[java]</i> Ant task running at the moment</li>
  202. <li><i>0</i> <font size="-1">sorry don't know - some Log4J stuff</font></li>
  203. <li><i>[main]</i> the running thread from our application </li>
  204. <li><i>INFO</i> log level of that statement</i>
  205. <li><i>oata.HelloWorld</i> source of that statement</i>
  206. <li><i>-</i> <font size="-1">sorry don't know - some Log4J stuff</font></li>
  207. <li><i>Hello World</i> the message</li>
  208. </ul>
  209. For another layout ... have a look inside Log4J's documentation about using other PatternLayout's.</p>
  210. <a name="config-files">
  211. <h2>Configuration files</h2>
  212. <p>Why we have used Log4J? "It's highly configurable"? No - all is hard coded! But that is not the debt of Log4J - it's
  213. ours. We had coded <tt>BasicConfigurator.configure();</tt> which implies a simple, but hard coded configuration. More
  214. confortable would be using a property file. In the java source delete the BasicConfiguration-line from the main() method.
  215. Log4J will search then for a configuration as described in it's manual. Then create a new file <tt>src/log4j.properties</tt>.
  216. That's the default name for Log4J's configuration and using that name would make life easier - not only the framework knows
  217. what is inside, you too!</p>
  218. <pre class="code">
  219. log4j.rootLogger=DEBUG, <b>stdout</b>
  220. log4j.appender.<b>stdout</b>=org.apache.log4j.ConsoleAppender
  221. log4j.appender.<b>stdout</b>.layout=org.apache.log4j.PatternLayout
  222. log4j.appender.<b>stdout</b>.layout.ConversionPattern=<font color="blue"><b>%m%n</b></font>
  223. </pre>
  224. <p>This configuration creates an output channel ("Appender") to console named as <tt>stdout</tt> which prints the
  225. message (%m) followed by a line feed (%n) - same as the earlier System.out.println() :-) Oooh kay - but we haven't
  226. finished yet. We should deliver the configuration file, too. So we change the buildfile:</p>
  227. <pre class="code">
  228. ...
  229. &lt;target name="compile"&gt;
  230. &lt;mkdir dir="${classes.dir}"/&gt;
  231. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/&gt;
  232. <b>&lt;copy todir="${classes.dir}"&gt;</b>
  233. <b>&lt;fileset dir="${src.dir}" excludes="**/*.java"/&gt;</b>
  234. <b>&lt;/copy&gt;</b>
  235. &lt;/target&gt;
  236. ...
  237. </pre>
  238. <p>This copies all resources (as long as they haven't the suffix ".java") to the build directory, so we could
  239. start the application from that directory and these files will included into the jar.</p>
  240. <hr>
  241. <p align="center">Copyright &copy; 2005 The Apache Software Foundation. All rights Reserved.</p>
  242. </body>
  243. </html>