Here is the place we can learn Java programming besides the language itself. Topics like, how to compile, run the program; how to unit test; how to write a makefile for your Java project will be covered here.
----How to compile and run your Java program?
Every programming language starts with HelloWorld examples. As far as I read it starts from "C Programming Language". Here is our version:
HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
In the command line, you type:
bash$ javac HelloWorld.java
to compile the java program and type:
bash$ java HelloWorld
to see the result
There are some alternatives to these commands we just see. For example, in gcc system, you may use gcj -C and you may use java -jar to run a jar file.
----How to write build file for Java project?
When our Java project grows larger and larger, we may think put our source code into a directory tree and have build content in a separate place. Similar to make/Makefile build system, Ant build system for Java is the choice and lucky enough, it's included in the JDK. Instead of having Makefile, usually we have build.xml (Note that build file in Ant is written in XML. Ant has default to read build.xml as the build file. If you in case you want to use another build file name, use ant -buildfile option, or simply as ant -file|ant -f) as the file for Ant. And like make/Makefile, you call on a task like: ant build if we have a target named build.
Every build.xml should have one project elements, so we can start our build.xml like this:
<project basedir="." default="clean" name="HelloWorld">
<target name="clean">
<delete dir="build">
</target>
</project>
We can see that there are 3 attributes for project, all of them are optional and their meaning is self-explained.
This short build file also gives us clue about the structure of it.
--What's the structure of Ant build file?
Since Ant build file use XML, we expect the tree structure. The top level is the project elements, the second level is the target elements and under target elements there are task elements. You can write your own task elements or use built-in ones. Besides these elements, people usually use property elements to refer to directory and property elements usually put under project elements along with targets.
Using property to define directory to use is a common practice when write build file for Ant. For example, we can add following lines to the build file we already have:
<project ...>
...
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
...
</project>
The reason here is these directory may be used several times and you don't want to hard code them. Problem can always be easier if we add another layer. So we set property and use them later in targets. You can look up Web to check how to use property and a list of built-in property. Instead of using general property, you can also use path.
--<path> or <pathelement>?
path property usually stays at the same level as other property and can be composed of pathelements. While pathelement are usually component of other task, like classpath.
To compile our Java project, we need include some jar files in classpath. We don't usually change CLASSPATH environment variable, since we prefer to have different classpath for each project. For compile process, javac task has parameter classpath, which we need to have a directory as its argument; or javac also has classpathref, which we can have a id contains path structure. For execution, java task usually have nested classpath elements composed of path structure.
--Wildcard in Ant?
There are 3 wildcard in Ant: ?, * and **. ? is used least frequently as it matches any single character; * matches zero or more character, it's often used for wildcard of file names; ** is the wildcard for directory.
----How to build unit test for Java project?
Java has its default unit test framework JUnit.
----Maven, more than Ant
Maven is a project management system, including functions provided by Ant.
No comments:
Post a Comment