Nant - .NET Construction Tool [Reserved]

xiaoxiao2021-03-06  42

1. Introduction In Visual Studio.net, we can construct and compile a large quantum item (for example, a web page collection, executive file, DLL assembly, etc.) .NET project. However, for large, complex software projects, relying on a programmer to click "Compile" button Sometimes it is not possible. What should I do if someone is not happy to install vs.net on each machine? If you can automate the construction process of the software, you will never have anyone to click the "Compile" button, then how good! Many benefits of automation of the construction process, but to do this, there must be an appropriate constructor. Construction tools solve the problem related to the software compilation process. Small development groups write some simple software that may not be constructed - you only start the compiler, compile the code into binary execution files, even if you do it. However, the current software is generally componentized, dependent on one or more sub-projects; these sub-projects may be written by many different people, and they may check another version of the Check IN code. As long as there is a component compilation, or a outdated component is used in the configuration, the entire project will be dragged down. Therefore, when constructing complex items, people tend to solve the problems faced in multiplayer development. The compiler will issue an error warning for the problem existing in the code, however, if the project contains some binary execution modules and multiple components that are closely dependent, it is quite difficult to find out the root cause of the truly incorrect. It is best to have a tool to clarify the dependencies of external modules. Once the problem is found, warning. This tool is Ant. Ant is originally designed by Apache Jakarta Project, which has solved the shortcomings of many existing constructors (such as Make tools in UNIX environments), which is important that conventional structural tools are always limited to specific operating systems. Environment or development language, with this, Ant is a platform neutral. To keep the platform, it is used to tell Ant how to compile the project (constructed file) is also an XML format. This means that the use of Ant tools is not limited to a particular platform (of course, the development platform - Java, .net, etc., and Ant itself). Another advantage of Ant is another advantage of Ant in addition to the platform neutrality. It is a declarative (rather than process). This means that we can do a lot of work without having to write a line of work - most of the seemingly heavy tasks can be done by adding declarations in the XML file. (If the construction process is extremely complicated, you must use the program logic to complete, and Ant also provides this support, allowing users to write code expansion this tool). In addition, since the construct file is an XML format, we can create and modify any XML editing tool. The ANT is the version of the .NET is called Nant. Nant itself is written in C #, but can be used for any .NET language (there are several examples of C #, VB.NET, and JScript.net in Nant's release package), and even one project can be combined with multiple .NET languages, If you want to construct a VB.NET client application, even if it relies on multiple assemblies written by VB.NET and C #, Nant can easily complete.

If you feel that this is not enough, Nant can run multiple compilers, for example, if you want to use Microsoft tools and Mono C # compilers (a tool to run C # and .NET software on Linux, see "on Linux Run C # and .NET "), NANT is also incorrect. To use nant, it is best to understand some of the knowledge of the .NET command line compiler. This article uses a C # compiler CSC as an example, but you can easily switch to VBC or other compiler - or if you are happy, use multiple compilers. Second, NANT Getting Started To use Nant, the first step is of course downloading this tool from the Nant website. Developers who are using Nant as integration and construction tools to pay attention, the latest NANT integrates excellent unit test tool NUnit 2.0. NUNIT 2.0 is significantly improved on the basis of 1.0. If you use NUnit 2, you should use the latest NANT to give full play to its advantage. Below we take a look at the use of NANT through a simple example - constructed a C # console program consisting of a single execution file. The code of the application is as follows: public class helloworld {

Static void main () {

System.Console.writeline ("Hello World.");

}

}

Of course, for such a simple project, it can be easily compiled with a C # command line compiler, as long as a "CSC * .cs" command can be executed. The result of compiling is a binary executable HelloWorld.exe. To complete the same task with NANT, first create an extension-called .build's XML construct file, the following is an example of a Nant constructor default.build, which is the same as executing a simple CSC compile command: [Listing 1: Create a simple NANT constructive script for a single execution file]

After creating a building file, execute the nant command to construct the project. As long as the current directory contains the default.build file, and the Nant itself executes the NANT command in the PATH environment variable of the operating system, Nant will analyze the default.build file to complete the tasks specified in the default.build file. Of course, for such a project with only one class, the tools such as using NANT are really a cow knife to kill chickens, and large materials are small. However, if we want to construct the execution file and immediately execute it, or construct one or more related modules, then construct the primary executor, and how? For such tasks, constructors such as ANT can save a lot of time. The NANT construction file is mainly composed of three parts: Target, Task, and Dependency. A task is a task that requires NANT, for example, Nant supported tasks include running compilers, copying / deleting files, sending email, or even compressing a set of files (for the full task list of NANT, see Here, please see here ). The goal describes a set of tasks that require NANT execution, which is a means of dividing tasks into logical groups. For example, suppose we require NANT to delete the contents of the bin directory, compile 5 execution files, copy the compiled binaries to a location, and organize these actions into a target. Relevance can be regarded as the relationship between two targets. However, Listing 1 has only one Target, its name is Build, its task is to run the compiler to compile the specified source file. Set the marked default property to build, NANT handles the name of the TARGET named Build.

There is one in the CSC mission

The child node, which specifies the source file to be compiled.

Third, define correlation

Now let's join the second Target - to perform it immediately after compiling the HelloWorld.exe file. The modified constructed file is shown in Listing 2.

[Listing 2: Construction script containing two related Target]

The newly added target name is RUN, which contains only one action exec for executing the program, and it has a correlation to the Build target. This correlation means that before executing Run Target, you must first implement this Target and must be successful. Pay attention

In the node, we change the Default property from the original build to Run. Since Run depends on Build, it ensures that compiling applications before running applications. If the build target is not reached due to some reason (usually because the compiler has discovered an error), the RUN target will not be executed. You can try it out: first add a syntax error in the HelloWorld code, then run NANT again, Nant will display the compiler's error message to the console, which can easily see where there is an error.

Fourth, start construction from scratch

If there is a compiled binary than the source file is new, Nant will not perform compilation operations - in other words, Nant does not compile any files that do not need to be compiled. In addition, if the construct file defines multiple relevance (ie, two or more components depend on another component), Nant is "smart", which only constructs the dependent component, and does not repeat the same component . This way of treatment has greatly improved the time required to construct large projects, but sometimes, people need to say "whether I have anything, you are compiled" rights, that is, to clear all compiled Binary file, constructed from scratch.

To this end, many constructive will contain a Clean target, developers can use it to clear all the files left by the previous compilation. Below is an example of constructor containing the Clean target:

[Listing 3: Construction script containing Clean targets]

The Clean target is not run every time it is constructed, just occasionally you need to run. To run the Clean target, simply execute the nant clean command. Nant Clean Commands Requires NANT to perform only Clean destination (that is, do not perform the operation of the construction project, just clear the contents of the bin directory). It is also possible to see that the constructor after this modification contains an MKDIR action to create a bin subdirectory to store compilation binaries. If you need to clear the bin directory, you have to construct the project, execute the command: Nant Clean Build.

V. Execute unit test

If the construction process and other operations are combined, such as Email reminder and automated unit testing, NANT can also be done well. Detailed discussion of NUNIT unit testing framework has exceeded the scope of this article, but NANT and NUNIT have indeed collaborative. Listing 4 is an example of such a construction file that constructs an app and performs NUNIT as part of the construction process. [Listing 4: Integrated constructed files for unit tests]

The construction file first specifies the location of the project file in the form of a Property tag. Put some of the possible changes or possibly to use the value to be used in attribute variables, but it is not required; attributes are usually declared at the beginning of the constructive, but it can be changed to the command line parameters if necessary. In this example, specifying the project file in the form of an attribute, because we have to pass this information to NUnit during the rear construction process.

Next, the construction file sequentially constructs Account.dll component and test tool Account-Test.dll. Both constructors contain Target = "library" options, which tells the compiler that we have to construct a component assembly, not a .exe file. In addition, it is also possible to see from Listing 4 that the test tool also references two it-dependent assemblies - Test business logic components Accent.dll and NUnit frameworks. This node is used when our project depends on the external library.

After constructing test tools and business logic components, construct scripts call NUnit, and specify the name of the assembly containing the test components, and requires a file log test result to generate an XML format. About NUnit integration, there must be a little attention: If you are using NUnit 2.0, you must use the latest NANT version, because NUNIT has recently modified, some "stable NANT can't run with NUnit 2.0, but The latest Nant's support for NUnit 2.0 is quite stable.

I hope this article is useful for you. To learn more about NANT features, see Nant's documentation, especially

Task Reference, which includes a concise list of NANT which tasks can complete.

转载请注明原文地址:https://www.9cbs.com/read-54353.html

New Post(0)