Ant Fifteenth Best Practice: Eric M. Burke from: onjava appear before the Ant build and deploy Java applications need to use scripts include platform-specific, Make files, various versions of the IDE even manually operated "hodgepodge ". Now, almost all open source Java projects are using Ant, most companies' internal projects are also using ANT. The wide use of Ant in these projects naturally leads to an urgent need for readers' best practices in Ant. This article summed up my favorite Ant skill or best practice, mostly from the "horror" story that I personally experienced or the "terror" story I have heard of the "horror" story. For example, someone tells me that there is a project to put the code generated by the XDoclet into the version control tool with the lock file function. When the 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, only when he can let Ant compile code, this method has some problems: □ The generated code cannot be stored in the version control system. □ ANT (XDoclet in this case) should automatically determine the source file involved next time, and should not be determined by the programmer. □ Ant's build file should define the correct task dependencies, so that the programmer does not have to bundle the task in a specific order. When I started a new project, I first wrote Ant build files. The ANT file explicitly defines the built-in process and is used by each programmer in the team. The techniques listed herein are based on such assumptions: ANT build files are important files that must be carefully prepared, which should be maintained in the version control system and regularly reconstructed. Below is my fifteen big Ant best practices. 1. Adoption of the consistent coding specification Instead of jumping into this confused debate, let us first look at some methods that keep the XML build files. First of all, it is also the most important, spending time formatting your XML makes it look very clear. Whether the XML is beautiful, Ant can work. But ugly XML is difficult to read. If you leave an empty line between the tasks, there is rule indentation, each line is not more than 90 columns, then XML is surprisingly easy to read. Coupled with an excellent editor or IDE tool that is highlighting XML syntax, you will not have trouble. Similarly, the selected words are clearly readily read to name tasks and properties. For example, Dir.Reports is better than RPTS. The specific coding specification is not important, just take a set of specifications and insist on using it. 2. Put Build.xml in the project root directory This is the most commonly used specification, developers can find expected build.xml in the top directory. Place the build file in the root directory and enable people to easily understand the logical relationship between the different directories in the project directory tree. The following is a typical project directory level: [root dir] | build.xml - src - lib (including third-party JAR package) - Build (generated by Build Task) - Dist (by Build Task) Generation) When build.xml is in the top-level directory, suppose you in a subdirectory, just enter: Ant -find compile command, you do not 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. Use a single build document Some people like to break a large project into several small build files, each build file sharing a small part of the entire build process.
This is indeed different problems, but it should be recognized that the building file segmentation will increase the difficulty of understanding the overall build process. Be careful not to be an Over-Engineer if the single build file can clearly manifest the build level. 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 be as soon as possible to build a documentation from the 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 simplest rule is to put all you want to make the programmer to call the command line. The task is plus described. For internal tasks typically used to perform intermediate processing, such as generating code or establish an output directory, etc., the description properties cannot be used. At this time, it can be processed by adding an XML annotation in the build file. Or specifically define a Help task, showing a detailed instructions when the programmer inputs Ant Help. Detailed help ... echo> target >5. Providing clear tasks) Each build file should contain a clear Tasks, used to delete all generated files and directories, so that the system returns to the initial state before the build file is executed. The files that are still existing after the emptying task should be under the management of the version control system. For example: < / Target> Do not automatically call the Clean task unless you have a special task of the entire system version. When the programmer only performs compilation tasks or other tasks, they don't need to build files in advance, which is both annoying and unnecessary empty tasks. To believe that programmers can determine when all files need to be emptied. 6. Use the ANT management task from the relationship assume your application consisting of Swing GUI components, web interfaces, EJB layers, and public application code. In a large system, you need to clearly define which layer of each Java belongs to the system. Otherwise, any changes must be re-compiled into 100 thousand files. The bad task is from the relationship management that leads to excessive complex and fragile systems. Change the design of the GUI panel should not cause recompilation of servlet and EJB. When the system becomes huge, it is a slight notice that it may be introduced to the server depending on the code of the client. This is because typical IDE project files compile any files using a single ClassPath. And Ant allows you to control build activities more effectively. Design your Ant build file to compile large projects: First, compile the public application code, and make the compilation result into the JAR package file. Then, compile the previous item code, rely on the JAR file generated by the first step when compiling. Continue to repeat this process until the highest layer code is completed. The construction strengthens the management of tasks. If you work on the underlying Java frame, you accidentally reference the high-level GUI template assembly, and the code does not need to be compiled. This is because the build file does not include the code of the high-rise GUI panel component in the source path when compiling the underlying frame.
7. Definitions and reuse file paths If the file path is defined in one place, and the build file is more readily understood in the entire build file. The following is an example of doing this: ... etc path> path> javac> target > project> When the project is growing, this technology is more complicated. You may need to define your file paths for compilation of different levels of applications, such as running unit testing, running the application, running xDoclet, generating javadocs, etc. Different paths. This method of component-defined path definitions is much more advantageous to define a path individually for each task. Otherwise, it is easy to lose the trajectory of the task depending on the relationship. 8. Define the appropriate task Binding relationship Assuming the DIST task is from the JAR task, which task is from the COMPILE task from the prepare task? The ANT build file finally defines the dependgram of the task, which must be carefully defined and maintained. The dependence of the task should be checked regularly to ensure that the construction work is implemented correctly. Big build documents tend to increase more tasks over time, so it may be very difficult to build work due to unnecessary dependence. For example, you may find that the EJB code is regenerated when programmers only need to compile some GUI code that does not use EJB. ignored the dependence of the task in the name of "Optimization" is another common error. Such a mistake forces the programmer to remember and call a string task in a specific order. Better approach is to provide a clear public task that includes the correct task dependence; additional "Expert" tasks allow you to manually perform individual build steps, these tasks do not provide a complete build process, but Let those expert users skip certain steps during fast and annoying encoding. 9. Use properties any information that needs to be configured or possible, should be defined as an ANT attribute. For values that appear multiple times in the build file, also processed. Attributes can be defined in a separate attribute file in building a file header or for better flexibility.
The following is a style that defines attributes in the build file: etc ... project> or you can use Property file: etc ... project> Dir.build = builddir.src = srcjdom.home = .. / java-tools / jdom-b8jdom.jar = jdom.jarjdom.jar.withpath = $ {jdom.home} / build / $ {jdom.jar} Define attributes with a separate file that is good, it can clearly define the configurable parts in the built. In addition, in the case of developers working in different operating systems, you can provide different versions of the file on different platforms. 10. Maintaining the construction process Independence In order to maximize extension, do not apply external paths and library files. The most important thing is to rely on programmers' ClassPath settings. Instead, the relative path is used in the build file and define its own path. If you reference absolute paths such as C: / Java / Tools, other developers may not use the same directory structure as you, so you can't use your build file. If you deploy an open source project, you should provide the release version of all JAR files needed to compile code. Of course, this is based on the license agreement. For internal projects, the relevant JAR files should be in the management of the version control system, and pick out to everyone. When you must reference an external path, the path should be defined as attributes. Make the programmer to overload these properties with parameters suitable for their own machine environment. You can also use the following syntax to reference environment variables:
The following is an example: zipfileset> eAr> In this example, all JAR files are placed in the lib directory of the EAR file package. Hr.jar and billing.jar are from building a directory. 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 task 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 do not have to perform such tasks. When the project size grows, even small inefficient work will become a big problem. 15. Avoid a specific platform ANT package No matter what reason, some people like to carry their products or scripts called Compile. When you go to see the script, you will find the following: Please do not use only the script of a specific platform to call Ant. This will only make other people add a biotuda of learning and understanding when using your scripts for the first time. In addition, you can't provide a script for each operating system, which is a place really bothering other users. Summary too many companies rely on manual methods and special procedures to compile code and generate software releases.