From 59d2888ac777b5aab93fcd2d080536fdfe2a5784 Mon Sep 17 00:00:00 2001
From: Gintas Grigelionis
After finishing the java-only step we have to think about our build process. We have to compile our code, +
After finishing the Java-only step we have to think about our build process. We have to compile our code,
otherwise we couldn't start the program. Oh—start
—yes, we could provide a target for
that. We should package our application. Now it's only one class—but if you want to provide a download,
no one would download several hundreds files ... (think about a complex Swing GUI—so let us create a jar file. A
@@ -165,8 +165,8 @@ java -jar build\jar\HelloWorld.jar
Now that we have a working buildfile, we could do some enhancements: many time you are referencing the same -directories, main-class and jar-name are hard coded, and while invoking you have to remember the right order of build +
Now that we have a working buildfile, we could do some enhancements: many times you are referencing the same +directories, main-class and jar-name are hardcoded, and while invoking you have to remember the right order of build steps.
The first and second point would be addressed with properties, the third with a special property—an
attribute of the <project>
tag and the fourth problem can be solved using dependencies.
Somehow told us not to use sys-statements. For log-statements we should use a Logging API—customizable to a -high degree (including switching off during usual life (= not development) execution). We use Log4J for that, -because
+Somebody told us not to use System
-statements. For output, we should use a Logging
+API—customizable to a high degree (including switching off during usual life (= not development) execution). We
+use Log4J for that, because
In this example we start our application not via its Main-Class
manifest-attribute, because we could not
-provide a jarname and a classpath. So add our class in the red line to the already defined path and start as
+provide a jar-name and a classpath. So add our class in the red line to the already defined path and start as
usual. Running ant would give (after the usual compile stuff):
[java] 0 [main] INFO oata.HelloWorld - Hello World@@ -320,14 +320,14 @@ usual. Running ant would give (after the usual compile stuff):
-
separatorHello World
the messageFor another layout ... have a look inside Log4J's documentation about using other PatternLayout's.
+For another layout ... have a look inside Log4J's documentation about using other PatternLayouts.
Why we have used Log4J? "It's highly configurable"? No—all is hardcoded! But that is not the fault of
Log4J—it's ours. We had coded BasicConfigurator.configure();
which implies a simple, but
hardcoded configuration. More comfortable would be using a property file. In the Java source file, delete
the BasicConfiguration
line from the main()
method (and the
-related import
statement). Log4J will search then for a configuration as described in it's manual. Then
+related import
statement). Log4J will search then for a configuration as described in its manual. Then
create a new file src/log4j.properties. That's the default name for Log4J's configuration and using that
name would make life easier—not only the framework knows what is inside, you too!
+package oata; + import org.junit.Test; +import static org.junit.Assert.fail; + public class HelloWorldTest { @Test @@ -412,11 +416,11 @@ junit instruction to our buildfile: ...-
We reuse the path to our own jar file as defined in run-target by giving it an ID and making it globally available.
-The printsummary=yes
lets us see more detailed information than just a "FAILED" or "PASSED" message.
-How much tests failed? Some errors? printsummary lets us know. The classpath is set up to find our classes.
-To run tests the batchtest
here is used, so you could easily add more test classes in the future just by
-naming them *Test.java
. This is a common naming scheme.
We reuse the path to our own jar file as defined in run
-target by giving it an id and making it
+globally available. The printsummary=yes
lets us see more detailed information than just a "FAILED"
+or "PASSED" message. How much tests failed? Some errors? printsummary lets us know. The classpath is set up
+to find our classes. To run tests the batchtest
here is used, so you could easily add more test classes in
+the future just by naming them *Test.java
. This is a common naming scheme.
After a ant junit you'll get:
@@ -430,8 +434,9 @@ junit: BUILD SUCCESSFUL ... -We can also produce a report. Something that you (and other) could read after closing the shell .... There are two
-steps: 1. let <junit>
log the information and 2. convert these to something readable (browsable).
+
We can also produce a report. Something that you (and others) could read after closing the shell .... There are two
+steps: 1. let <junit>
log the information and 2. convert these log files to something readable
+(browsable).
...