Ant in Dot Net

Source: Internet
Author: User
Tags dot net net command net command line
I. Introduction





In Visual Studio. NET, we only need to use a simple menu command to construct and compile a large quantum project (for example, a collection of associated web pages, execution files,DLLProgram. Net project. However, for large and complex software projects, it is sometimes impossible to rely on a programmer to click the "compile" button. What if someone is unhappy about installing vs. Net on every machine? How nice it would be to be able to automatically execute the construction process of the software without the need to click the "compile" button! There are many advantages to automate the construction process, but to do so, appropriate construction tools must be available.



The constructor solves problems related to the software compilation process. Small developers may not use constructor to write some simple software-you only need to start the compilerCodeCompile it into a binary execution file.



However, the current software is usually componentized and dependent on one or more sub-projects. These sub-projects may be compiled by many different people and may be checked in at any time) another version of the code. As long as one component fails to be compiled or an outdated component is used during construction, the entire project will be dragged down. Therefore, when constructing complex projects, people often use construction tools to solve problems faced by collaborative development.



The compiler will issue an error warning for problems in the Code. However, if the project contains some binary execution modules and multiple closely dependent components, it is quite difficult to find out the root cause of the error. It is best to have a tool to clarify the dependency between the application and the external modules, and give a warning once a problem is found.



This tool is ant. Ant was originally designed by the Apache Jakarta project to solve the shortcomings of many existing construction tools (such as the make tool commonly used in UNIX environments). One of the most important points is that, conventional construction tools are always limited to specific operating systems, development environments, or development languages. Ant is platform neutral. To keep the platform neutral, the file (constructor file) used to tell ant how to compile the project is also in XML format. This means that ant tools are not limited to a specific platform (except for Java,. net, and ant itself, of course ).



In addition to the advantages of platform-neutral, ant has another advantage: its constructor file is declarative (rather than procedural ). This means that we can do a lot of work without writing a line of code-most seemingly heavy tasks can be done by adding declarations to the XML file. (If the construction process is extremely complex, you must use the program logic to complete it. Ant also provides support in this regard, allowing you to write code to expand this tool ). In addition, since the constructor file is in XML format, we can use any XML editing tool to create and modify it.



The version ant implements for. NET is called Nant. Nant itself is written in C #, but can be used in any. net Language (there are several C # and VB in the Nant release package. net and JScript.. NET language, if you want to construct a VB.. Net client application, even if it depends on multiple. net and C # programming assembly, Nant can also easily complete. If you think this is not enough, Nant can run multiple compilers. For example, if you want to use Microsoft tool and mono C # compiler at the same time (one runs C # And.. NET software tools, see running C # And. and Nant.



To use Nant, you 'd better understand some. Net command line compiler knowledge. This article uses the C # compiler as an example. However, you can easily switch to vbc or another compiler -- or, if you are willing to, use multiple compilers at the same time.



Ii. Nant Introduction



To use Nant, the first step is to download the tool from the Nant website. Note that Nant is integrated with excellent unit test tools.Nunit2.0.Nunit2.0 has made significant improvements based on 1.0. If you useNunit2. Use the latest Nant to give full play to its advantages.



Let's take a simple example to see how Nant works-construct a C # console program consisting of a single execution file. The application code is as follows:


Public class helloworld {static void main () {system. Console. writeline ("Hello world .");}}


Of course, for such a simple project, the C # command line compiler can also be very convenient for compilation, as long as you execute a " *. cs" command. The compiled result is a binary executable file helloworld.exe. To complete the same task with Nant, you must first create a file with the extension. build XML construction file. The following is an example of an Nant construction file default. build, which completes the same task as executing a simple compilation command:



[Listing 1: create a simple Nant construction script for a single execution file]


     


After creating the constructor file, run the Nant command to construct the project. As long as the current directory contains default. build File, And the Nant execution file is in the PATH environment variable of the operating system. After the Nant command is executed, Nant will analyze default. build File to complete default. the task specified in the build file.



Of course, for such a project with only one category, tools such as Nant are actually useless. However, what if we need to construct an execution file and then execute it immediately, or first construct one or more relevant modules and then construct the main execution program? For such a task, using ant and other construction tools can save a lot of time.



The Nant constructor consists of three parts: Target, task, and dependency. A task is a task that requires Nant to execute. For example, Nant supports running compilers, copying/deleting files, and sending emails, you can even compress a group of files (for a complete list of tasks supported by Nant, see here ).



The goal describes a group of tasks that require Nant to execute. It is a means to divide tasks into logical groups. For example, if we want Nant to delete the content of the bin directory, compile five execution files, and copy the compiled binary files to a specific location, these actions can be organized into a target.



Correlation can be seen as the relationship between two targets. However, listing 1 only has one target. Its name is build, and its task is to run the compiler to compile the specified source file. Mark the default attributeSetNant processes the target named build.



A subnode in the task specifies the source file to be compiled.



Iii. Define relevance



Now we have added the second target--compiled the helloworld.exe file and executed it immediately. The modified construction file is shown in Listing 2.



[Listing 2: contains two target construction scripts]


     


The newly added target is called run and contains only one exec action to execute the program. In addition, it also has a correlation to the build target. This correlation indicates that the build target must be implemented and the execution must be successful before running the target. Note: In the node, we changed the default attribute from the original build to run. Because run depends on build, it ensures that the application is compiled before running the application.



If, for some reason, the build target is not met (usually because the compiler discovers an error in the Code), the run target will not be executed. You can try to add a syntax error to the helloworld code first, and then run Nant again. Nant will display the compiler error information to the console, you can easily see where an error occurs.



4. structure from scratch



If a compiled binary file is newer than the source file, Nant will not perform any compilation operations-in other words, Nant will not compile any files without compilation. In addition, if the constructor defines multiple correlations (that is, two or more components depend on another component), Nant is "smart ", it only constructs the dependent components once and does not repeatedly construct the same component. This processing method greatly improves the time required to construct a large project, but sometimes people need to be able to say "no matter what I have, you can compile it without mistake", that is, to clear all compiled binary files, create them from the beginning.



To this end, many constructor files contain a clean object, which can be used by developers to clear all the files left by the last compilation. The following is an example of a constructor file containing the clean object:



[Listing 3: Construct script containing the clean target]


     


The Clean target does not run every time it is constructed, but occasionally needs to run it. To run the clean target, you only need to execute the Nant clean command. The Nant clean Command requires that Nant only execute the clean target (that is, it does not construct the project, but only clears the content of the bin directory ). In addition, we can see that the modified constructor script contains a mkdir action to create a bin sub-directory to store compiled binary files. If you want to clear the bin directory and construct a project, run the command: Nant clean build.



5. Execute unit test



If you want to combine the constructor process with other operations, such as email reminders and automated unit tests, Nant can do well. Detailed discussionNunitThe unit test framework is beyond the scope of this article, but Nant andNunitCollaboration is really good. Listing 4 is an example of constructing a file. It constructs an application and runsNunitIt is also a natural part of the construction process.



[Listing 4: integrated with the unit test constructor file]


     


The construction file first specifies the location of the project file in the form of a property tag. It is useful but not necessary to put some values that may change or may be used again into attribute variables. attributes are usually declared at the beginning of the constructor file, but if necessary, you can also use the command line parameters. In this example, specifying a project file in the form of properties brings a lot of convenience, because we need to pass this informationNunit.



Next, the constructor constructs the account in sequence.DLLComponent and test tool account-test.DLL. Both of these constructor Processes contain target = "library". This is the compiler. We want to construct a set of programs instead of A. EXE file. In addition, we can see from listing 4 that the test tool also uses the references node to reference two dependent assemblies-the account of the tested business logic component.DLLAndNunitFramework. This node is used when the project we construct depends on the external library.



Construct the test tool and business logic component and construct a script for calling.NunitAnd specify the name of the Assembly containing the test component. A file in XML format must be generated to record the test result.



for integration of nunit , note the following: if you are using nunit 2.0, you must use the latest Nant version, this is because nunit has made major changes recently, some "stable" Nant cannot run together with nunit 2.0, however, the latest Nant support for nunit 2.0 is quite stable..


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.