Use the Node.js build tool grunt to publish the ASP.net MVC project _node.js

Source: Internet
Author: User
Tags install node

Grunt Introduction
Grunt is based on JS and node.js build tools, because this time node.js more and more popular, Grunt has a wealth of open source community support, produced a lot of plug-ins. There are also some plug-ins scattered in the node community. Construction is a and broad expression, the traditional understanding is to compile, package, copy, now, as the technology is more and more rich, the building also includes preprocessing of front-end components, such as sass, less preprocessing into css,css and JS compression and merge. Grunt Plug-ins can support these new build concepts well and are more suitable for projects built with open source technology. Although grunt is more used for program construction, it is essentially grunt a task-running tool to solve repetitive labor.

Getting Started with grunt
Installation
Download and install Node.js. Download Address

To check the installation and viewing version:

Node-v
v0.10.25

Install the grunt command-line tool GRUNT-CLI and use the-G global installation, which can be used in any directory. The following command will add grunt to your system search path, so you can use this command in any directory.

NPM install-g GRUNT-CLI

It should be noted that under Linux or Mac, there are times when you report an error without permissions, and you need to precede a sudo

sudo npm install grunt-cli-g

View version:

Grunt–version
GRUNT-CLI v0.1.13

Unloading. If you have previously installed the global grunt, then delete it first.

NPM Uninstall-g Grunt

GRUNT-CLI is just a grunt command-line interface that requires the use of grunt and its plug-ins, which must be installed in the path of the project (usually under the project root), and the plug-in module is required for the grunt module itself. Whenever the grunt command is executed, it looks for installed grunt locally through the Nodejs require command. Because of this, you can run the grunt command in any subdirectory. If the CLI finds a locally installed grunt, it loads the grunt library, then applies the configuration you have written in Gruntfile, and then performs the corresponding task.

Configuration file
Package.json
Package.json is used to save the node modules that are installed and required under the current directory, for example:

{
 ' name ': ' My-project-name ', '
 version ': ' 0.1.0 ',
 ' author ': ' Your name ',

 ' devdependencies ': {
 ' Grunt ': ' ~0.4.1 '
 }
}

You can create this file manually, or through the NPM init command, and follow the prompts to complete the creation of the Package.json file. If you create a Package.json manually, you can simply download and install the required modules through NPM install. When the module is installed, it is saved in the Node_modules directory.

If you want to add the required modules later, use the following command to make the Package.json file synchronized

NPM Install <module>--save-dev

Gruntfile.js
the status of this file, like Makefile, is a document that directs grunt to perform tasks, requiring the configuration of parameters required by each plug-in module, loading Plug-ins, and defining tasks. More about Gruntfile can refer here. The reader is advised to have a holistic understanding of gruntfile and to continue.

Building asp.net MVC using grunt
Msbuild
is built using grunt. NET project, you must first understand MSBuild. MSBuild is Microsoft's tool for building programs, and Visual Studio is now fully using MSBuild to compile projects. MSBuild consists of an MSBuild tool, a set of compilation or builder programs, and an XML file. In fact, the project files in Visual Studio, such as. sln and. csproj, are an XML that MSBuild can recognize (hereinafter called an MSBuild configuration file), and Visual Studio by calling MSBuild, MSBuild recognizes the parameters and constructs the behavior identity to complete the build. We can also call msbuild ourselves by command line ourselves.

There are two key concepts in MSBuild: Task and property. A task is an entry that MSBuild can execute directly as a target, and either point to the default task when you execute MSBuild, or you must specify what the target task is. A property is a variable, just as a variable in a program can affect the execution of a program, and the property can affect the behavior of the build.

Visual Studio produces an MSBuild configuration file that is actually very complex, ostensibly with only a few upstream, but it imports some predefined configuration files into the current file, making it impossible to fully view the full configuration file so that the key task item cannot be found. Fortunately, there is a tool that can be used to help analyze the structure of the MSBuild configuration file.

Also, when MSBuild is invoked, the default properties and tasks can be overridden by command-line arguments, such as the following invocation representation, targeting the "Rebuild" task and setting the configuration property to debug:

MSBuild Consoleapplication1.csproj/target:rebuild/property:configuration=debug

For more on MSBuild, please refer to Microsoft's documentation

Manually use MSBuild instead of Visual Studio
to publish to this machine as an example, through the author in the environment under the VS2012 test, using VS in the call to MSBuild using the following key parameter overrides:

    • Configuration:debug or release, believe that students who use vs will not be unfamiliar with this
    • Visualstudioversion:vs when installed, some common, Visual Studio-related, msbuild configuration files exist in advance of a version, and when Visual Studio generates a project file, it contains a $ The visualstudioversion variable, which is combined with the path to point to these pre-configured configuration files. At 2012, you need to set this value to 11.0
    • WarningLevel: Alarm level at compile time
    • Deleteexistingfiles: Options for whether or not to delete existing files using the Publish feature
    • Webpublishmethod: The way to publish, the author commonly used is filesystem, that is, published to the local or remote share of a directory
    • PublishURL: If Webpublishmethod is filesystem, this value indicates the disk path of the publication

Key tasks:

    • Build: The compilation capability for a project in VS
    • Rebuild: The recompile feature for a project in VS
    • Webpublish: vs. publishing functionality for a project

At this point, we can use the MSBuild command line instead of some of the build actions of vs. As the focus of this article is grunt, the reader can refer to Microsoft's instructions and test it yourself:

Ps:msbuild are usually located in a directory like this: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
using Grunt-msbuild to invoke MSBuild
finally to introduce the protagonist of this article: Grunt-msbuild. This is a personal development of MSBuild invocation middleware. As a grunt plug-in, after the author's close test, can be used completely. Combined with other grunt Plug-ins, the author simplifies the process of releasing the project.

You can add this plugin to the project's gruntfile as follows, and automate the release:

MSBuild: {
  fontend: {    
    src: [' web.fontend/web.fontend.csproj '],
    options: {
      projectconfiguration: ' Release ',
      targets: [' webpublish '],
      stdout:true,
      maxcpucount:4,
      buildparameters: {
        Warninglevel:2,
  visualstudioversion: "11.0",
  deleteexistingfiles: ' True ',
  webpublishmethod: ' FileSystem ',
  publishurl: [Font_pwd]
      },
      verbosity: ' Quiet '
    }
  }
}

The code above is implemented to publish the Web.FontEnd.csproj project in release mode via FileSystem publishing to the FONT_PWD variable reference directory. Note here that you may need to modify the Visualstudioversion parameter according to your version of VS, by looking at a directory similar to the following: C:\Program Files (x86) \msbuild\microsoft\visualstudio \v11.0 to check. Font_pwd is a JS variable, adjusted as needed.

The complete configuration of the grunt is not given, mainly to know these key parameter settings.

Deleteexistingfiles This parameter does not seem to work during actual use, so it may be necessary to include the task of emptying the target folder in addition. The following is a partial screenshot of the actual task at run time:

Related Article

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.