Deploy .Net project automatically with MsBuild and MsDeploy (1)
Q: How to change parameter values in configuration files dynamically
In the first section http://www.cnblogs.com/delexios/p/4933300.html, I mentioned 5 questions regarding auto-deployment in my project. Now I will answer the first question.
For isolating from my project files, I create a new file named VRent.csproj with the root element ‘Project’. All works about auto-deployment are defined in this file.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0" DefaultTargets="BuildConfig">
</Project>
First, I should define some variables on the top of the auto-deployment script file to make sure that these values of variables can be referred in other parts. I have two choices to do it.
- PropertyGroup
- ItemGroup
The meaning of PropertyGroup is like its name. Properties with a custom name and a value can be the child elements of PropertyGroup. So I defined many property groups include the properties regarding application path and name, the properties regarding the path and name of application package, the properties regarding the path and name of application copy and so on. For example
<!-- App path property in IIS-->
<PropertyGroup>
<IisProxySiteName>\api</IisProxySiteName>
<IisDAProxySiteName>\dataAccessProxy</IisDAProxySiteName>
<IisUPProxySiteName>\upl</IisUPProxySiteName>
<DeployIisAppPathProxy>VRent_Dev$(IisProxySiteName)</DeployIisAppPathProxy>
<DeployIisAppPathDAProxy>VRent_Dev$(IisDAProxySiteName)</DeployIisAppPathDAProxy>
<DeployIisAppPathUPProxy>VRent_Staging$(IisUPProxySiteName)</DeployIisAppPathUPProxy>
</PropertyGroup>
Using $({Property Name}) to refer another value of property has defined before to join with constant values ensures isolation and low coupling to reduce the complication of maintenance.
<DeployIisAppPathUPProxy>VRent_Staging$(IisUPProxySiteName)</DeployIisAppPathUPProxy>
In my project, there are too many properties belonged to one group to manage them. From my side, PropertyGroup is not the best way to use in this case although it is the best practice to store my variables according to Microsoft……
So I chose ItemGroup. Actually, ItemGroup is not used in this way. It is used to group some stuffs because they are similar and have common meta data. Here, I used it to store the variables as meta data of a particular group with a meaningful name. For example
<ItemGroup>
<!-- email properties -->
<EmailConfig Include="Email">
<SendMailFrom>test@crm-factory.org</SendMailFrom>
<SendMailFromPwd>123</SendMailFromPwd>
<SendMailWay>true</SendMailWay>
<ServiceCenterEmail1>adam@crm-factory.org</ServiceCenterEmail1>
<EmailTemplatePackage>C:\Project\EmailTemplate\</EmailTemplatePackage>
</EmailConfig>
<!-- union pay properties -->
<UnionPayConfig Include="UnionPay">
<sdksignCertpath>C:\Project\certs\PM_700000000000001_acp.pfx</sdksignCertpath>
<sdksignCertpwd>000000</sdksignCertpwd>
<sdksignCerttype>PKCS12</sdksignCerttype>
<sdkencryptCertpath>C:\Project\certs\encrypt.cer</sdkencryptCertpath>
<sdkvalidateCertdir>C:\Project\certs\</sdkvalidateCertdir>
<sdkfrontTransUrl>https://101.231.204.80:5000/gateway/api/frontTransReq.do</sdkfrontTransUrl>
<sdkbackTransUrl>https://101.231.204.80:5000/gateway/api/backTransReq.do</sdkbackTransUrl>
<sdksingleQueryUrl>https://101.231.204.80:5000/gateway/api/queryTrans.do</sdksingleQueryUrl>
<sdkfileTransUrl>https://101.231.204.80:5000/gateway/api/fileTransRequest.do</sdkfileTransUrl>
<sdkbatTransUrl>https://101.231.204.80:5000/gateway/api/batchTrans.do</sdkbatTransUrl>
<sdkcardRequestUrl>https://101.231.204.80:5000/gateway/api/cardTransReq.do</sdkcardRequestUrl>
<sdkappRequestUrl>https://101.231.204.80:5000/gateway/api/appTransReq.do</sdkappRequestUrl>
</UnionPayConfig>
</ItemGroup>
In this example, I defined two items ‘EmailConfig’and ‘UnionPayConfig’.Each of them has its own meta data. For using these values, I can use them with the pattern of %({ItemName.MetaData}) for example %(EmailCofig.SendMailFrom). It follow the Object-Oriented Design so I think using item group is more suitable in concept and semantics in my project.
After I defined all the variables completely. I can use them to change the attributes and parameters in configuration files.
In MS Build technology, one Task element which is the child element of Target represents a job run by MS Build. I need a special task which support modifying .Net configuration file that is a actual XML file. I found one named XmlPoke can be used. For example
<XmlPoke
XmlInputPath="$(ProjectPathDAProxy)Web.config"
Query="//VRentEmail/host/@address"
Value="%(EmailConfig.SendMailHost)">
</XmlPoke>
I refer the value of ProjectPathDAProxy I defined in one property group as the value of XmlInputPath which represents the physical path of a configuration file. I used XPath to find the attribute I would like to change and refer the value of EmailConfig.SendMailHost in one item as the value used to change the attribute. I wrote down all the attributes and parameters I would like to change and each of them has a related XmlPoke task.
Finally, I use a target as the container of all the XmlPoke tasks.
<Target Name="BuildConfig">
<XmlPoke
XmlInputPath="$(ProjectPathProxy)Web.config"
Query="//client/endpoint[@name='BasicHttpBinding_IDataService']/@address"
Value="%(ServiceConfig.dataServiceAddress)">
</XmlPoke>
</Target>
If I use VRent.csproj as parameter to MS Build, it works. everything goes well.
Deploy .Net project automatically with MsBuild and MsDeploy (1)的更多相关文章
- Deploy .Net project automatically with MsBuild and MsDeploy (0)
I will use a example of my project to show how to use MS Build and MS Deploy in a real project and s ...
- Intellij IDEA – How to build project automatically
By default, Intellij IDEA doesn’t compile classes automatically. But, you can enable the auto compil ...
- [AWS] Deploy react project on EC2
如何在aws部署项目 申请到亚马逊AWS免费账户后,我们可以拥有很多的免费云服务产品项目,其中包括: EC2云服务器. Amazon S3存储. Amazon RDS数据库. Amazon Cloud ...
- How to deploy a Delphi OSX project from the command line
Delphi has a well developed command line build process (via MSBuild) for Windows projects. After the ...
- .NET Core 工具从 project.json 移动到基于 MSBuild 的项目后的使用
.NET Core 从preview 4 开始弃用project.json 可以从这下载最新版本: https://github.com/dotnet/cli 使用VS2017 RC新建.net co ...
- Headless MSBuild Support for SSDT (*.sqlproj) Projects
http://sqlproj.com/index.php/2012/03/headless-msbuild-support-for-ssdt-sqlproj-projects/ Update: bre ...
- 配置 Web Deploy 的步骤 -摘自网络
今天的文章里,我会介绍Microsoft Web Deploy—一个采用全面的发布和部署机制的免费服务器技术.Web Deploy不仅仅让你发布文件—还可以部署数据库结构/数据,运行变更的数据库脚本, ...
- Headless MSBuild Support for SSDT (*.sqlproj) Projects [利用msbuild自动化部署 .sqlproj]- 摘自网络
Update: breaking change: http://sqlproj.com/index.php/2012/10/dacfx-sept-2012-updates-break-headless ...
- faceted project validation builder
Should I keep Eclipse Java facet? Facets automate some parts of project configuration and deployment ...
随机推荐
- Painter's Problem poj1681 高斯消元法
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4420 Accepted: 2143 ...
- H264常见术语名称
一.术语 帧(frame)和场(field):一帧包含一个亮度矩阵采样点和俩个对应的色度矩阵采样点,一帧包含俩个场:顶场和底场: 条带:特定条带组按光栅扫描顺序排列的整数个宏块或宏块对: 条带组:图像 ...
- windown快速安装xgboost
记录xgboost的快速安装方式,该方式适合pyhton3.5/3.6版本. 系统: win10 64bit python版本:3.6 1. 下载xgboost编译好的whl包 下载路径为:http: ...
- 我真的知道JavaScript吗?
JavaScript 说说JavaScript 接触JavaScript时间其实已经不短了,之前一直是半瓶酱油,东凑西凑的收集相关的知识.并没有完整系统的学习过JavaScript,觉得JavaScr ...
- 悟透JavaScript (一)
首先说明,这是别人写的一篇文章,写得很好,对理解JavaScript很有好处,所以转帖过来. 引子 编程世界里只存在两种基本元素,一个是数据,一个是代码.编程世界就是在数据和代码千丝万缕的纠缠中 ...
- 详解m4文件
最近在分析speex代码,发现编译过程中需要的一个speex.m4文件不知道是何方神圣,怀着对未知知识的渴望,跑到 某哥和某基问了一下,算是认识了,为了方便以后经常见面,这里就做个记录吧. M4实际上 ...
- mysql中将时间转为秒
项目中遇到的问题,需要将时间(时 分 秒)转为秒,业务上处理有些麻烦,尝试找了多种处理函数,然而并没有用 完美解决办法: TIME_TO_SEC 格式'HH:MM:SS'或HHMMSS SELEC ...
- WCF的客户端与服务端
服务端 : 1.新增一个Winform的服务端 2.选择以上建的项目,增加一个WCF服务 3.定义三个方法,一个不返回结果不带参数.返回结果带参数.返回结果以类的方式传递参数 4.Service1继承 ...
- SAP 动态设置 GUI STATUS 灰色不可用 或者隐藏(转)
http://blog.sina.com.cn/s/blog_66110f6201017rul.html 有时候需要根据用户的权限或者是操作动态设置gui状态上的某些按钮的可用和不可用. 1.先定 ...
- EF框架搭建小总结--ModelFirst模型优先
前言:去年刚工作的时候,也是刚刚正式接触.net,当时了解了EF以及三种开发模式,Database First.Model First .Code First.公司用的开发模式是Database Fi ...