ANT Fifteenth Best Practice: Eric M. Burke, coauthor of Java Extreme Programming Cookbook original: http: //www.onjava.com/pub/a/onjava/2003/12/17/ant_bestpractices.html Translator: StoneXuqqqq: 19722707msn: xt121@hotmail.com Before Ant appears, compile and deploy Java applications requires hodgepodges that include scripts including specific platforms, Make files, different IDEs, and handmade operations. Now, almost all open source Java projects are using Ant, and many company development projects are also using ANT. Aunt's large use, naturally brought urgent needs to summarize Ant best practices. This article summarizes my good ANT best practice, many of the projects that have experienced projects, or from other developers' "horror" story inspiration. For example, someone tells me that there is a project to put the code generated by the XDoclet into the version control tool of the locked file. When a single developer modifies the source code, he must remember the check out and lock all files that will be regenerated. Then, manually run the code generator, when he enables Ant to compile the code, this method has some problems: L The generated code cannot be stored in the version control system L ANT (this case is xDoclet) should be automatically determined next time Build the source file involved, and should not be manually determined by the programmer. l Ant's build file should define the right task dependencies so that the programmer does not have to call the task in a specific order. When I started a new project, I first write Ant build files. The process of defining the built-in builds and uses each programmer in the team. All best practices in this article assume that Ant build files are important files that must be carefully prepared, which should be maintained in the version control system, and reconstruct it regularly. Below is my fifteen big Ant best practices. 1. Adopting a consistent coding specification ANT user is willing to jump into this charming debate whether you like or hate XML build files. Let's take some way to keep XML build files. First of all, it is also the most important, time formatting your XML makes it look very clear. However, XML is beautiful, Ant can work. But ugly XML is hard to read. If you leave a blank line between the tasks, there is rule indentation, each line is not more than 90 columns, then XML is surprising to read. Plus a good editor or IDE highlight the corresponding statement, you will not read the trouble. Similarly, the selection is clear and easy to read to naming tasks and properties. For example, Dir.Reports is better than RPTS. It does not require specific coding specifications, as long as there is a norm and stick to it. 2. Place Build.xml in the project root directory Ant build file build.xml can be placed on, but placed in the top of the project to keep the project is concise. This is the most common specification that makes developers to find it at the root directory. At the same time, it is also possible to understand the logical relationship between different directories in the project. The following is a typical project level: [root dir] | Build.xml - src - lib (including third-party JAR package) - Build (generated by Build Task) - Dist (Generated by Build Task) BUILD.XML When you are in a top directory, if you enter a subdirectory, just enter: Ant -Find compile command, you don't need to change the work directory to compile the code. Parameters -Find tells Ant to find Build.xml existing in the superior directory and execute. 3. Some people use a single build file like to decompose a large project to several small build files, each build file sharing a small part of the entire build process.
However, it should be appreciated that the construction of file segmentation will increase the difficulty of understanding the entire build process. Be careful not to be an Over-Engineer if the single build file can be clearly manifesting. Even if you divide the project into multiple build files, you should also make programmers to find core build.xml in the project root directory. Although the document is just appointed the actual build work to the lower-level build file, it should be guaranteed that the file is available. 4. Provide a good help note should try to make the build file from documentation. Increasing task description is the easiest way. When you enter Ant -ProjectHelp, you can see the task list with a description. For example, you can define the task:
The following is an example of doing this:
The following is a style that defines attributes in the build file:
It is usually avoided to store build output in the version control system. If your source code is well received, you can regenerate any version of the product by building a building process. 12. Take Ant as a "minimum blend mother" to assume your development team to use IDE, why is you worried about the programmer by clicking the icon? The issue of IDE is a problem about consistency and reproducibility in the team. Almost all IDE design is to improve the personal productivity of programmers, not the continuous construction of the development team. Typical IDE requires each programmer to define its own project files. Programmers may have different directory structures, which may use different versions of library files, which may also work on different platforms. This will lead to this situation: run a good code there, where you can't run it. No matter what IDE your development team uses, you must build all the Ant build files that all programmers can use. To create a programmer, you must perform the rules of the ANT build file before submitting a new code control system. This will ensure that the code is built by the same ANT build file. When there is a problem, use the project standard Ant build file instead of an IDE to perform a clean built. Programmers can freely choose any IDE they used to use. But Ant should be used as a public baseline to ensure that it is always built. 13. Use the ZipFileSet property that people often use ANT to generate WAR, JAR, ZIP, and EAR files. These files usually require a specific internal directory structure, but it is often mismatched with the directory structure of your source code and compilation. A most common method is to write an Ant task to copy a large pile of files into the temporary directory in accordance with the desired directory structure, and then generate a compressed file. This is not the most effective way. Using ZipFileSet properties is a better solution. It allows you to select files from any location and put them in different directory structures into the compressed file. The following is an example:
So we use the ZipFileSet property to move them to the lib directory inside the EAR file. The Prefix property specifies its target path in the EAR file. 14. Test the Clean build task assumes that there is Clean and Compile tasks in your build file, perform the following tests. The first step is to execute Ant Clean; step 2, execute Ant Compile; third step, execute Ant Compile. The third step should not be anything. If the file is compiled again, you have a problem with your building file. The building file should only perform tasks only when the input file associated with the output file is changed. A build file is inefficient if you do not have to perform these tasks when you don't have to compile, copy, or other tasks. When the project size grows, even small inefficient work will become a big problem. 15. Avoid Platform-Specific Ant Wrappers No matter what reason, some people like to load their products or scripts with a batch file or script called Compile. When you go to the script's content, you will find the following: Ant Compile actually developers familiar with Ant and fully typing Ant Compile. Please do not use only the script of a specific platform to call Ant. This will only increase the harassment of learning and understanding when others use your script for the first time. In addition, you can't provide a script for each operating system, which is a place really bothering other users. Summarizing too many companies rely on manual methods and procedures to compile code and generate software release versions. Those developing teams that do not use the Ant or similar tools, spend a surprising time to capture problems in the code compilation process, which compiles successful code in some developers, but also some developers there Failed. Generating and maintaining building scripts is not a charming job, but it is a necessary job. A good Ant build file will make you concentrate on a more favorite job - write code! Reference · Ant · AntGraph: Ant-dependent visualization tools · Ant: The Definitive Guide, O'Reilly · Java Extreme Programming Cookbook, O'Reillyhttp: //www.cjsdn.net/post/view bid = 2 & id = 101256 & sty =? 1 & tpg = 1 & agn = 0