This article is a second part of a tool (ANT) series of articles for building and deploying Java projects. In my first article "Ant Introduction" (Oracle magazine, 2002, 2002, I introduced some basic tasks that may be required during Java development. I use Ant to automatically compile Java source code, create JAR files, run the program, and generate javadoc. In this second article, I will discuss some of the more advanced ANT features in the two task packages (core task packages and optional task packages). The core package includes tasks for building, packaged, and deploying Java projects, including tasks for performing SQL statements and tasks integrated with concrerent version control systems (CVS). The optional package is extended in a variety of ways, including: Integration with other source code control systems, running Unit testing and precompilation JavaServer Pages (JSP). I will also discuss it with Oracle9i JDeveloper 9.0.3, and provide some useful ANT tips and techniques. Packing an app To distribute applications, you may need to generate a document library: tar.gz and / or zip file (for UNIX and Windows platforms, respectively). In order to generate these two files, you should write the target shown in Listing 1. In this goal, I use two special tasks to generate Tar and ZIP files. They are very similar; their root elements include an attribute (attribute tarfile and zipfile) that describes the name of the document library to be generated, each nested a special FileSet to include files to be included in this document library. This goal seems to be lengthy, but this helps solve two headache problems: file licensing and installation directory. When unpacking a document library, assume that the file has a reasonable file license and install it into their own directory. In order to solve the file license problem, I will integrate the files in this library according to the file into two groups: a pattern of PatternSet (specified DIST-NON-EXEC) to define a mode for unsuccessful files, and will execute the file ( Shell scripts and .bat files) Configure another Patternset in the bin directory (specified DIST-EXEC). TARFileSet - Special FileSet for TAR tasks, use the Mode property management file license, which is the digital file license value of the file in this collection. In order to solve the installation directory problem, I use the TarfileSet and ZipFileSet's prefix properties. The prefix value will be attached to the file path of the file library. This way, if I write prefix = "$ {name}", all files and directories of the library will be included in the root directory named the project name. Therefore, when decompressed, the generated document library will install these files in their own directory. Running the Unit test with JUnit is a place where the Unit test runs. Write the Unit test to find problems as soon as possible and modify the code. JUnit is a test framework for developing a Unit test in the Java project. The JUnit task included in the ANT options allows you to perform these tests from Ant Buildfile. This way, use the JUnit task, you can easily run all Unit tests every time you create or deploy Java projects. The following example shows how to use the JUnit task: Description = "Run Junit Tests"> PRINTSUMMARY = "withoutanderr"> INCLUDES = "** / *. jar" /> claspath> batchtest> junit> target> If a test fails, the haltonfailure = "true" property requires Ant to stop; PrintsumMary = "withoutanderr" property Prints the standard and error output of the test on the console (it is useful when developing tests). The classpath property defines the classpath that runs the test; ClassPath must include a JUnit JAR file, which must also be displayed in the lib directory where Ant is installed. BatchTest element defines the test file to run (its class name is ended in Test). Send SQL Command Ant Provide SQL Tasks to perform a simple SQL statement and script files that contain multiple SQL statements. The SQL task can be used to run the SQL statement based on any data source, and you have drivers that match the Java database connection API (JDBC) in accordance with any data source. Here is an example of connecting to an Oracle database and cancels, creating, and inserting a table TEST: URL = "JDBC: Oracle: Thin: @foo: 1521: Bar" Userid = "scott" Password = "tiger"> DROP TABLE TEST; Create Table Test ID integer, Name varchar (20), ); INSERT INTO TEST VALUES (1, 'Jeff'); INSERT INTO TEST VALUES (2, 'Rob'); INSERT INTO TEST VALUES (3, 'Michel'); sql> target> In this example, the SQL task is connected to the database specified in the URL attribute using the driver specified by the Driver property. You must install the JDBC driver library for the database in the LIB directory installed in Ant. After establishing a connection, the included SQL statement is executed. This SQL task type is extremely useful in the case where you want to perform a limited SQL statement from the Ant Buildfile. However, you can imagine a scenario of a large SQL statement, such as rebuilding database indexes. In this case, an external SQL file is executed instead of the entire SQL command sequence is easier in your Ant Buildfile. You can use the FileSet type to specify one or more SQL script files that contain any number of SQL statements you want to perform. The following example will be described: Userid = "scott" Password = "tiger"> fileset> sql> target> In this example, the fileset type specifies the directory / OPT / Build / Foobar / SQL, the include type specifies the rebuild_indices.sql file in that directory to execute. The FileSet type allows you to isolate the external SQL code from your Ant BuildFile, which simplifies maintenance of Ant BuildFile and SQL scripts. When using Ant, you may encounter such situations: SQL statements include XML characters such as left arrow keys or & ( URL = "JDBC: Oracle: Thin: @foo: 1521: Bar" Userid = "scott" Password = "tiger">
Update Some_Table Set Column1 = COLUMN1 1 WHERE column2 <42; ]]]] sql> target> In this example, the CDATA block includes a SQL statement including the left arrow key (<) in its WHERE. In order to avoid the ANT XML parser explanation as an XML tag, I embed this statement into the CDATA block. Integrated to source control system ANT provides tasks for performing basic source code control (eg, check out, check out and label code, etc.). Ant now supports the following source code control systems: Clearcase, Continuous / Synergy, CVS, Perforce, PVCS, StarTeam, and Visual SourceSafe. Support for all systems other than CVS can be obtained from an optional task package. Note that in order to integrate with source code control systems other than CVS, Ant may need to access additional libraries. For example, in order to work with StarTeam and Ant, you must place the StarTeam-SDK.jar file in the LIB directory installed in the Ant installation. In order to check and create the latest developments of Ant, run the buildfile shown in Listing 2. If the Build target of this file creates an Ant failure, try to create an ORO project. Change the value of the attribute module to Jakarta-ORO. BuildFile Check the project (in the Checkout target) and Ant task in the Checkout target to build it with Ant (in the build target). The CVS task must be verified on the CVS server using the data in the .cvspass file located in the user's home directory. Listing 2 BuildFile may be empty because the CVS information library is available in public access. The Login target uses the CVSPASS task to write the CVSROOT and password of the information library to access it in the .cvspass file. For security considerations, you should not hardcode your password in Buildfile, but should pass it to the script by command line, using the command line ant -dcvsroot = foo -dpassword = bar login boot Ant. Using buildfiles such as list 2, you can easily distribute engineering to developers, which is extremely important to welcome any developer's open source project. But this method is also a way of extracting and building automation on a regular basis. JAVAC can inform you this error when you encounter an error when you encounter and compile the Java source file. This allows you to stop building, rather than deploying an incorrect application on the previously existing applications. However, if you are using JSP, you will not be able to use Javac to find a syntax error while compile, because in general, JSP files are compiled only when the user requests a JSP file. Therefore, lack of Javac features very easily cause deployment of JSP code defects. You can avoid this problem by precompiling your JSP code using Ant's JSPC tasks. The JSPC task will convert your JSP file to the correct format of Java Servlet, so you can use Javac tasks to compile them and capture any syntax errors thus what happen. The following example illustrates this process: destdir = "$ {src}" Package = "com.roacle.example.jsp"> jspc> destdir = "$ {TMP}" /> target> In this example, the JSPC task places the JSP file in the directory specified by the SRCDIR property and converts them to the Java servlet file. These Java Servlet files are then placed in the directory specified by the DESTDIR property, and they will be compiled by the Javac task in this directory. The Javac task then compiles the source code generated by JSPC into the JAVA bytecode in the $ {src} directory, and placed it in the directory specified by the DESTDIR property, in this example is $ {TMP}. In this buildfile segment, the JSPC task enables us to find out the error that may be accidentally discovered by the user. Tips for improving ANT production efficiency use properties. By using properties, you only need to minimize your job, you can reuse your buildfile. You can also develop a reusable target collection to quickly develop new BuildFile. Use the relative file path. The relative path is a good way to ensure the transplantability of the project. The absolute file path is not flexible, and it will also make it more difficult to change the Java project directory structure than the desired. Comment for Buildfile. Note BuildFile has many methods. One of the methods is to add a description property to the target. This will make the code's readability better. Read the documentation. Ant's documentation is comprehensive and includes many useful examples. Help the Ant user group. You may encounter an issue where Ant's formal documentation cannot be resolved. In this case, you can help with the user mailing list of Ant. Extending Ant features You may encounter the core tasks of Ant and optional tasks that cannot meet your needs. However, there may be a third party has solved this problem. To understand more, please refer to the Ant website (Jakarta.apache.org/ant) and click on the "External Tools and Tasks" link. Write your own task. When the standard Ant task and third-party groups can provide the task to meet your needs, it is time to consider yourself. In this case, you can extend the functionality of Ant by writing your own task. Remember, Ant is open from the source code, so you can see and reuse the source code for all core tasks. Use an internal script. When the task is too targeted without being reused, you can write an internal script nested in the Script task. The internal script is portable (they will run in any environment where Ant is running), but requires a third-party library. Use external scripts cautiously. The external scripts written for implementing the tasks that Ant are not supported may be very attractive, but you should carefully write external scripts because it may affect the portability of BuildFile. Conclusion Ant is a very powerful and comprehensive tool for building and deploying Java projects. For Java developers, Ant provides a large amount of useful, saving tasks, helping you make Java projects higher. Michel Casabianca (CASA@sweetohm.net) is a software engineer of French company in-fusio, which provides mobile operators, and other people with other people with books (XML Pocket Reference) (2001) The year was published by O'Reilly). Other ways to use Ant