PageRenderTime 80ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/apache-ant-1.10.2-bin/apache-ant-1.10.2/manual/tutorial-HelloWorldWithAnt.html

https://bitbucket.org/xcsolutionsqa/dpmc_selenium
HTML | 520 lines | 395 code | 109 blank | 16 comment | 0 complexity | 468270704930a4b8e3c74d8cb3cc9d9a MD5 | raw file
Possible License(s): Apache-2.0
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <title>Tutorial: Hello World with Apache Ant</title>
  18. <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
  19. </head>
  20. <body>
  21. <h1>Tutorial: Hello World with Apache Ant</h1>
  22. <p>This document provides a step by step tutorial for starting java programming with Apache Ant.
  23. It does <b>not</b> contain deeper knowledge about Java or Ant. This tutorial has the goal
  24. to let you see, how to do the easiest steps in Ant.</p>
  25. <h2>Content</h2>
  26. <p><ul>
  27. <li><a href="#prepare">Preparing the project</a></li>
  28. <li><a href="#four-steps">Enhance the build file</a></li>
  29. <li><a href="#enhance">Enhance the build file</a></li>
  30. <li><a href="#ext-libs">Using external libraries</a></li>
  31. <li><a href="#resources">Resources</a></li>
  32. </ul></p>
  33. <a name="prepare"></a>
  34. <h2>Preparing the project</h2>
  35. <p>We want to separate the source from the generated files, so our java source files will
  36. be in <tt>src</tt> folder. All generated files should be under <tt>build</tt>, and there
  37. splitted into several subdirectories for the individual steps: <tt>classes</tt> for our compiled
  38. files and <tt>jar</tt> for our own JAR-file.</p>
  39. <p>We have to create only the <tt>src</tt> directory. (Because I am working on Windows, here is
  40. the win-syntax - translate to your shell):</p>
  41. <pre class="code">
  42. md src
  43. </pre>
  44. <p>The following simple Java class just prints a fixed message out to STDOUT,
  45. so just write this code into <tt>src\oata\HelloWorld.java</tt>.</p>
  46. <pre class="code">
  47. package oata;
  48. public class HelloWorld {
  49. public static void main(String[] args) {
  50. System.out.println("Hello World");
  51. }
  52. }
  53. </pre>
  54. <p>Now just try to compile and run that:
  55. <pre class="code">
  56. md build\classes
  57. javac -sourcepath src -d build\classes src\oata\HelloWorld.java
  58. java -cp build\classes oata.HelloWorld
  59. </pre>
  60. which will result in
  61. <pre class="output">
  62. Hello World
  63. </pre>
  64. </p>
  65. <p>Creating a jar-file is not very difficult. But creating a <i>startable</i> jar-file needs more steps: create a
  66. manifest-file containing the start class, creating the target directory and archiving the files.</p>
  67. <pre class="code">
  68. echo Main-Class: oata.HelloWorld&gt;myManifest
  69. md build\jar
  70. jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
  71. java -jar build\jar\HelloWorld.jar
  72. </pre>
  73. <p><b>Note:</b> Do not have blanks around the &gt;-sign in the <tt>echo Main-Class</tt> instruction because it would
  74. falsify it!</p>
  75. <a name="four-steps"></a>
  76. <h2>Four steps to a running application</h2>
  77. <p>After finishing the java-only step we have to think about our build process. We <i>have</i> to compile our code, otherwise we couldn't
  78. start the program. Oh - "start" - yes, we could provide a target for that. We <i>should</i> package our application.
  79. Now it's only one class - but if you want to provide a download, no one would download several hundreds files ...
  80. (think about a complex Swing GUI - so let us create a jar file. A startable jar file would be nice ... And it's a
  81. good practise to have a "clean" target, which deletes all the generated stuff. Many failures could be solved just
  82. by a "clean build".</p>
  83. <p>By default Ant uses <tt>build.xml</tt> as the name for a buildfile, so our <tt>.\build.xml</tt> would be:</p>
  84. <pre class="code">
  85. &lt;project&gt;
  86. &lt;target name="clean"&gt;
  87. &lt;delete dir="build"/&gt;
  88. &lt;/target&gt;
  89. &lt;target name="compile"&gt;
  90. &lt;mkdir dir="build/classes"/&gt;
  91. &lt;javac srcdir="src" destdir="build/classes"/&gt;
  92. &lt;/target&gt;
  93. &lt;target name="jar"&gt;
  94. &lt;mkdir dir="build/jar"/&gt;
  95. &lt;jar destfile="build/jar/HelloWorld.jar" basedir="build/classes"&gt;
  96. &lt;manifest&gt;
  97. &lt;attribute name="Main-Class" value="oata.HelloWorld"/&gt;
  98. &lt;/manifest&gt;
  99. &lt;/jar&gt;
  100. &lt;/target&gt;
  101. &lt;target name="run"&gt;
  102. &lt;java jar="build/jar/HelloWorld.jar" fork="true"/&gt;
  103. &lt;/target&gt;
  104. &lt;/project&gt;
  105. </pre>
  106. <p>Now you can compile, package and run the application via</p>
  107. <pre class="code">
  108. ant compile
  109. ant jar
  110. ant run
  111. </pre>
  112. <p>Or shorter with</p>
  113. <pre class="code">
  114. ant compile jar run
  115. </pre>
  116. <p>While having a look at the buildfile, we will see some similar steps between Ant and the java-only commands:
  117. <table>
  118. <tr>
  119. <th>java-only</th>
  120. <th>Ant</th>
  121. </tr>
  122. <tr>
  123. <td valign="top"><pre class="code">
  124. md build\classes
  125. javac
  126. -sourcepath src
  127. -d build\classes
  128. src\oata\HelloWorld.java
  129. echo Main-Class: oata.HelloWorld>mf
  130. md build\jar
  131. jar cfm
  132. build\jar\HelloWorld.jar
  133. mf
  134. -C build\classes
  135. .
  136. java -jar build\jar\HelloWorld.jar
  137. </pre></td>
  138. <td valign="top"><pre class="code">
  139. &lt;mkdir dir="build/classes"/&gt;
  140. &lt;javac
  141. srcdir="src"
  142. destdir="build/classes"/&gt;
  143. <i>&lt;!-- automatically detected --&gt;</i>
  144. <i>&lt;!-- obsolete; done via manifest tag --&gt;</i>
  145. &lt;mkdir dir="build/jar"/&gt;
  146. &lt;jar
  147. destfile="build/jar/HelloWorld.jar"
  148. basedir="build/classes"&gt;
  149. &lt;manifest&gt;
  150. &lt;attribute name="Main-Class" value="oata.HelloWorld"/&gt;
  151. &lt;/manifest&gt;
  152. &lt;/jar&gt;
  153. &lt;java jar="build/jar/HelloWorld.jar" fork="true"/&gt;
  154. </pre></td>
  155. </tr></table>
  156. </p>
  157. <a name="enhance"></a>
  158. <h2>Enhance the build file</h2>
  159. <p>Now we have a working buildfile we could do some enhancements: many time you are referencing the
  160. same directories, main-class and jar-name are hard coded, and while invocation you have to remember
  161. the right order of build steps.</p>
  162. <p>The first and second point would be addressed with <i>properties</i>, the third with a special property - an attribute
  163. of the &lt;project&gt;-tag and the fourth problem can be solved using dependencies.</p>
  164. <pre class="code">
  165. &lt;project name="HelloWorld" basedir="." default="main"&gt;
  166. &lt;property name="src.dir" value="src"/&gt;
  167. &lt;property name="build.dir" value="build"/&gt;
  168. &lt;property name="classes.dir" value="${build.dir}/classes"/&gt;
  169. &lt;property name="jar.dir" value="${build.dir}/jar"/&gt;
  170. &lt;property name="main-class" value="oata.HelloWorld"/&gt;
  171. &lt;target name="clean"&gt;
  172. &lt;delete dir="${build.dir}"/&gt;
  173. &lt;/target&gt;
  174. &lt;target name="compile"&gt;
  175. &lt;mkdir dir="${classes.dir}"/&gt;
  176. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}"/&gt;
  177. &lt;/target&gt;
  178. &lt;target name="jar" depends="compile"&gt;
  179. &lt;mkdir dir="${jar.dir}"/&gt;
  180. &lt;jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"&gt;
  181. &lt;manifest&gt;
  182. &lt;attribute name="Main-Class" value="${main-class}"/&gt;
  183. &lt;/manifest&gt;
  184. &lt;/jar&gt;
  185. &lt;/target&gt;
  186. &lt;target name="run" depends="jar"&gt;
  187. &lt;java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/&gt;
  188. &lt;/target&gt;
  189. &lt;target name="clean-build" depends="clean,jar"/&gt;
  190. &lt;target name="main" depends="clean,run"/&gt;
  191. &lt;/project&gt;
  192. </pre>
  193. <p>Now it's easier, just do a <tt class="code">ant</tt> and you will get</p>
  194. <pre class="output">
  195. Buildfile: build.xml
  196. clean:
  197. compile:
  198. [mkdir] Created dir: C:\...\build\classes
  199. [javac] Compiling 1 source file to C:\...\build\classes
  200. jar:
  201. [mkdir] Created dir: C:\...\build\jar
  202. [jar] Building jar: C:\...\build\jar\HelloWorld.jar
  203. run:
  204. [java] Hello World
  205. main:
  206. BUILD SUCCESSFUL
  207. </pre>
  208. <a name="ext-libs"></a>
  209. <h2>Using external libraries</h2>
  210. <p>Somehow told us not to use syso-statements. For log-Statements we should use a Logging-API - customizable on a high
  211. degree (including switching off during usual life (= not development) execution). We use Log4J for that, because <ul>
  212. <li>it is not part of the JDK (1.4+) and we want to show how to use external libs</li>
  213. <li>it can run under JDK 1.2 (as Ant)</li>
  214. <li>it's highly configurable</li>
  215. <li>it's from Apache ;-)</li>
  216. </ul></p>
  217. <p>We store our external libraries in a new directory <tt>lib</tt>. Log4J can be
  218. <a href="http://www.apache.org/dist/logging/log4j/1.2.13/logging-log4j-1.2.13.zip">downloaded [1]</a> from Logging's Homepage.
  219. Create the <tt>lib</tt> directory and extract the log4j-1.2.9.jar into that lib-directory. After that we have to modify
  220. our java source to use that library and our buildfile so that this library could be accessed during compilation and run.
  221. </p>
  222. <p>Working with Log4J is documented inside its manual. Here we use the <i>MyApp</i>-example from the
  223. <a href="http://logging.apache.org/log4j/docs/manual.html">Short Manual [2]</a>. First we have to modify the java source to
  224. use the logging framework:</p>
  225. <pre class="code">
  226. package oata;
  227. <b>import org.apache.log4j.Logger;</b>
  228. <b>import org.apache.log4j.BasicConfigurator;</b>
  229. public class HelloWorld {
  230. <b>static Logger logger = Logger.getLogger(HelloWorld.class);</b>
  231. public static void main(String[] args) {
  232. <b>BasicConfigurator.configure();</b>
  233. <font color="blue"><b>logger.info("Hello World");</b></font> // the old SysO-statement
  234. }
  235. }
  236. </pre>
  237. <p>Most of the modifications are "framework overhead" which has to be done once. The blue line is our "old System-out"
  238. statement.</p>
  239. <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
  240. to do a little work here. But do not change the CLASSPATH environment variable! This is only for this project and maybe
  241. you would break other environments (this is one of the most famous mistakes when working with Ant). We introduce Log4J
  242. (or to be more precise: all libraries (jar-files) which are somewhere under <tt>.\lib</tt>) into our buildfile:</p>
  243. <pre class="code">
  244. &lt;project name="HelloWorld" basedir="." default="main"&gt;
  245. ...
  246. <b>&lt;property name="lib.dir" value="lib"/&gt;</b>
  247. <b>&lt;path id="classpath"&gt;</b>
  248. <b>&lt;fileset dir="${lib.dir}" includes="**/*.jar"/&gt;</b>
  249. <b>&lt;/path&gt;</b>
  250. ...
  251. &lt;target name="compile"&gt;
  252. &lt;mkdir dir="${classes.dir}"/&gt;
  253. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}" <b>classpathref="classpath"</b>/&gt;
  254. &lt;/target&gt;
  255. &lt;target name="run" depends="jar"&gt;
  256. &lt;java fork="true" <b>classname="${main-class}"</b>&gt;
  257. <b>&lt;classpath&gt;</b>
  258. <b>&lt;path refid="classpath"/&gt;</b>
  259. <font color="red"><b>&lt;path location="${jar.dir}/${ant.project.name}.jar"/&gt;</b></font>
  260. <b>&lt;/classpath&gt;</b>
  261. &lt;/java&gt;
  262. &lt;/target&gt;
  263. ...
  264. &lt;/project&gt;
  265. </pre>
  266. <p>In this example we start our application not via its Main-Class manifest-attribute, because we could not provide
  267. 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
  268. <tt>ant</tt> would give (after the usual compile stuff):</p>
  269. <pre class="output">
  270. [java] 0 [main] INFO oata.HelloWorld - Hello World
  271. </pre>
  272. <p>What's that? <ul>
  273. <li><i>[java]</i> Ant task running at the moment</li>
  274. <li><i>0</i> <font size="-1">sorry don't know - some Log4J stuff</font></li>
  275. <li><i>[main]</i> the running thread from our application </li>
  276. <li><i>INFO</i> log level of that statement</i>
  277. <li><i>oata.HelloWorld</i> source of that statement</i>
  278. <li><i>-</i> separator</li>
  279. <li><i>Hello World</i> the message</li>
  280. </ul>
  281. For another layout ... have a look inside Log4J's documentation about using other PatternLayout's.</p>
  282. <a name="config-files">
  283. <h2>Configuration files</h2>
  284. <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
  285. ours. We had coded <tt>BasicConfigurator.configure();</tt> which implies a simple, but hard coded configuration. More
  286. comfortable would be using a property file. In the java source delete the BasicConfiguration-line from the main() method
  287. (and the related import-statement). Log4J will search then for a configuration as described in it's manual. Then create
  288. a new file <tt>src/log4j.properties</tt>. That's the default name for Log4J's configuration and using that name would make
  289. life easier - not only the framework knows what is inside, you too!</p>
  290. <pre class="code">
  291. log4j.rootLogger=DEBUG, <b>stdout</b>
  292. log4j.appender.<b>stdout</b>=org.apache.log4j.ConsoleAppender
  293. log4j.appender.<b>stdout</b>.layout=org.apache.log4j.PatternLayout
  294. log4j.appender.<b>stdout</b>.layout.ConversionPattern=<font color="blue"><b>%m%n</b></font>
  295. </pre>
  296. <p>This configuration creates an output channel ("Appender") to console named as <tt>stdout</tt> which prints the
  297. message (%m) followed by a line feed (%n) - same as the earlier System.out.println() :-) Oooh kay - but we haven't
  298. finished yet. We should deliver the configuration file, too. So we change the buildfile:</p>
  299. <pre class="code">
  300. ...
  301. &lt;target name="compile"&gt;
  302. &lt;mkdir dir="${classes.dir}"/&gt;
  303. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/&gt;
  304. <b>&lt;copy todir="${classes.dir}"&gt;</b>
  305. <b>&lt;fileset dir="${src.dir}" excludes="**/*.java"/&gt;</b>
  306. <b>&lt;/copy&gt;</b>
  307. &lt;/target&gt;
  308. ...
  309. </pre>
  310. <p>This copies all resources (as long as they haven't the suffix ".java") to the build directory, so we could
  311. start the application from that directory and these files will included into the jar.</p>
  312. <a name="junit">
  313. <h2>Testing the class</h2>
  314. <p>In this step we will introduce the usage of the JUnit [3] testframework in combination with Ant. Because Ant
  315. has a built-in JUnit 3.8.2 you could start directly using it. Write a test class in <tt>src\HelloWorldTest.java</tt>: </p>
  316. <pre class="code">
  317. public class HelloWorldTest extends junit.framework.TestCase {
  318. public void testNothing() {
  319. }
  320. public void testWillAlwaysFail() {
  321. fail("An error message");
  322. }
  323. }</pre>
  324. <p>Because we dont have real business logic to test, this test class is very small: just show how to start. For
  325. further information see the JUnit documentation [3] and the manual of <a href="Tasks/junit.html">junit</a> task.
  326. Now we add a junit instruction to our buildfile:</p>
  327. <pre class="code">
  328. ...
  329. &lt;path <b>id="application"</b> location="${jar.dir}/${ant.project.name}.jar"/&gt;
  330. &lt;target name="run" depends="jar"&gt;
  331. &lt;java fork="true" classname="${main-class}"&gt;
  332. &lt;classpath&gt;
  333. &lt;path refid="classpath"/&gt;
  334. <b>&lt;path refid="application"/&gt;</b>
  335. &lt;/classpath&gt;
  336. &lt;/java&gt;
  337. &lt;/target&gt;
  338. <b>&lt;target name="junit" depends="jar"&gt;
  339. &lt;junit printsummary="yes"&gt;
  340. &lt;classpath&gt;
  341. &lt;path refid="classpath"/&gt;
  342. &lt;path refid="application"/&gt;
  343. &lt;/classpath&gt;
  344. &lt;batchtest fork="yes"&gt;
  345. &lt;fileset dir="${src.dir}" includes="*Test.java"/&gt;
  346. &lt;/batchtest&gt;
  347. &lt;/junit&gt;
  348. &lt;/target&gt;</b>
  349. ...
  350. </pre>
  351. <p>We reuse the path to our own jar file as defined in run-target by
  352. giving it an ID and making it globally available.
  353. The <tt>printsummary=yes</tt> lets us see more detailed information than just a "FAILED" or "PASSED" message.
  354. How much tests failed? Some errors? Printsummary lets us know. The classpath is set up to find our classes.
  355. To run tests the <tt>batchtest</tt> here is used, so you could easily add more test classes in the future just
  356. by naming them <tt>*Test.java</tt>. This is a common naming scheme.</p>
  357. <p>After a <tt class="code">ant junit</tt> you'll get:</p>
  358. <pre class="output">
  359. ...
  360. junit:
  361. [junit] Running HelloWorldTest
  362. [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0,01 sec
  363. [junit] Test HelloWorldTest FAILED
  364. BUILD SUCCESSFUL
  365. ...
  366. </pre>
  367. <p>We can also produce a report. Something that you (and other) could read after closing the shell ....
  368. There are two steps: 1. let &lt;junit&gt; log the information and 2. convert these to something readable (browsable).<p>
  369. <pre class="code">
  370. ...
  371. <b>&lt;property name="report.dir" value="${build.dir}/junitreport"/&gt;</b>
  372. ...
  373. &lt;target name="junit" depends="jar"&gt;
  374. <b>&lt;mkdir dir="${report.dir}"/&gt;</b>
  375. &lt;junit printsummary="yes"&gt;
  376. &lt;classpath&gt;
  377. &lt;path refid="classpath"/&gt;
  378. &lt;path refid="application"/&gt;
  379. &lt;/classpath&gt;
  380. <b>&lt;formatter type="xml"/&gt;</b>
  381. &lt;batchtest fork="yes" <b>todir="${report.dir}"</b>&gt;
  382. &lt;fileset dir="${src.dir}" includes="*Test.java"/&gt;
  383. &lt;/batchtest&gt;
  384. &lt;/junit&gt;
  385. &lt;/target&gt;
  386. <b>&lt;target name="junitreport"&gt;
  387. &lt;junitreport todir="${report.dir}"&gt;
  388. &lt;fileset dir="${report.dir}" includes="TEST-*.xml"/&gt;
  389. &lt;report todir="${report.dir}"/&gt;
  390. &lt;/junitreport&gt;
  391. &lt;/target&gt;</b>
  392. </pre>
  393. <p>Because we would produce a lot of files and these files would be written to the current directory by default,
  394. we define a report directory, create it before running the <tt>junit</tt> and redirect the logging to it. The log format
  395. is XML so <tt>junitreport</tt> could parse it. In a second target <tt>junitreport</tt> should create a browsable
  396. HTML-report for all generated xml-log files in the report directory. Now you can open the ${report.dir}\index.html and
  397. see the result (looks something like JavaDoc).<br>
  398. Personally I use two different targets for junit and junitreport. Generating the HTML report needs some time and you dont
  399. need the HTML report just for testing, e.g. if you are fixing an error or a integration server is doing a job.
  400. </p>
  401. <a name="resources"></a>
  402. <h2>Resources</h2>
  403. <pre>
  404. [1] <a href="http://www.apache.org/dist/logging/log4j/1.2.13/logging-log4j-1.2.13.zip">http://www.apache.org/dist/logging/log4j/1.2.13/logging-log4j-1.2.13.zip</a>
  405. [2] <a href="http://logging.apache.org/log4j/docs/manual.html">http://logging.apache.org/log4j/docs/manual.html</a>
  406. [3] <a href="http://www.junit.org/index.htm">http://www.junit.org/index.htm</a>
  407. </pre>
  408. </body>
  409. </html>