Package oata;
Public class helloworld {
Public static void main (string [] ARGs ){
System. Out. println ("Hello World ");
}
}
After finishing the Java-only step we have to think about our build process. WeHaveTo compile our code, otherwise we couldn't start the program. Oh-"start"-Yes, We cocould provide a target for that. WeShocouldPackage our application. now it's only one class-but if you want to provide a download, no one wocould download several hundreds files... (think about a complex swing Gui-so let us create a jar file. A startable JAR file wocould be nice... and it's a good practice to have a "clean" target, which deletes all the generated stuff. failed failures cocould be solved just by a "clean build ".
By default ant usesBuild. xmlAs the name for a buildfile, so our. \ Build. xmlWocould be:
<Project> <target name = "clean"> <Delete dir = "build"/> </Target> <target name = "compile"> <mkdir dir = "build/classes "/> <javac srcdir =" src "destdir =" build/classes "/> </Target> <target name =" jar "> <mkdir dir =" build/jar" /> <jar destfile = "build/JAR/helloworld. jar "basedir =" build/classes "> <manifest> <attribute name =" Main-class "value =" oata. helloworld "/> </manifest> </jar> </Target> <target name =" run "> <Java jar =" build/JAR/helloworld. jar "fork =" true "/> </Target> </Project>
Now you can compile, package and run the application
Ant compileant jarant run
Or shorter
Ant compile jar run
While having a look at the buildfile, we will see some similar steps between ANT and the Java-only commands:
Java-only |
Ant |
MD build \ Classes javac-sourcepath Src-d build \ Classes SRC \ oata \ helloworld. java echo main-class: oata. helloworld> mf md build \ Jar CFM build \ jar \ helloworld. jar mf-C build \ Classes. java-jar build \ jar \ helloworld. jar |
|