Ant
> Sample Ant Build File - JAR
| ||
Sample Ant Build File - JAR
In this example you will see how to structure the project. If the grows bigger, it will become a problem to manages the files if all the files are there in the same directory. For a easier maintenanace all the source file should be kept in the src directory, the compressed jar file should be kept in the dist directory and all the intermediate class files should be kept in the build/classes directory. By imposing structure cleaning the project becomes easy, we can just delete the directory and recreate it. Using the <mkdir> task we create build/classes and dist directory.
在这个例子中你会看到怎样构建工程,如果项目较大,如果所有的文件都在相同目录下的话,文件会变得难以管理。为了更容易的维护应该把所有的源码放到src目录下。压缩jar文件应该放在dist目录下和其中的class文件应该放在build/classes目录下 令人印象深刻structure 处理工程变得简单。你可以删除目录和重新创建它。 用<mkdir> 任务我们创建build/classes和dist目录
<target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist" /> </target>
During the compilation process all the java files in the src directory will be compiled and the generated class files will be placed in the build/classes directory. 当编译处理时src目录下的所有java文件会被编译并产生class文件在build/classes目录下. Since we placed all the class files under the build/classes directory, creating jar file becomes easier, you can simply specify the basedir attribute as "build/classes" instead of specifying basedir="." and includes="*.class". After creating the jar file we place it in the dist directory (destfile="dist/HelloWorld.jar"). 当我们把所有的class文件放到build/classes目录中时,创建jar文件变得更加简单.你可以简单的指定basedir属性=“build/classes” 代替指定的basedir="."和includes="*.class".创建jar之后我们会把它放到dist目录中 (destfile="dist/HelloWorld.jar").
<target name="compile" depends="init"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="compress" depends="compile"> <jar destfile="dist/HelloWorld.jar" basedir="build/classes" /> </target>
You can use the java task to execute a class file as shown below. The classname attribute refers to the java class to be executed and the classpath attribute refers to the directory in which the class is located. 我们可以用java任务运行一个class文件如下所示.classname属性指向java 类和classpath属性指向class指定的目录。
<target name="execute" depends="compile"> <java classname="com.vaannila.helloworld.HelloWorld" classpath="build/classes" /> </target>
Since all the class files and jar files are isolated, we can easily clean the project by deleting the respective directories. 当所有的class文件和jar都是孤立的。我们可以容易的clean工程通过删除各自的目录
<target name="clean"> <delete dir="build" /> <delete dir="dist" /> </target>
The default target is compress, so when you run the build file the compress target will be executed. The compress target depends on compile target which in turn depends on the init target, so first the init target will be executed and the two directories will be created, then the compile target will be execute, later the jar file is created. default target是compress,所有当你运行构建文件的时候compress target会运行.而compress target依赖于compile target->depends init target.所有init target会先运行.和2个目录会被创建,所以compile target 会在创建jar之后运行.
When you run the "ant execute" command the execute target will be invoked. Execute target also depends on compile target which in turn depends on init target, but now the directories won't be created again because it already exist. Since the java file is already compiled it won't be compiled again. Ant checks the timestamp of the file and compiles only the updated files. The HelloWorld.class file will be execute and the "HelloWorld!" message will be displayed on the console. 当你运行"ant execute" 命令时execute target会被调用. execute target 依赖于compile target ->depends init target. 但是现在目录不会再次创建,因为它已经存在.当java文件已经编译过时.它就不会再去编译了.Ant 会去检查文件的timestamp 和编译更新的文件. HelloWorld.class文件会运行并在命令行上显示信息"HelloWorld!"
To clean and execute the program run the "ant clean execute" command. First the clean target will be executed and the directories will be deleted, later the execute task will be invoked. clean 和execute 程序运行"ant clean execute" 命令. 首先clean target 会被运行.并且目录会被删除,然后execute 任务会被调用.
You can download the build file here.
|