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 ...
随机推荐
- Springboot - 学习笔记 ②
前言 这一篇是关于spring boot中的配置(configuration)的介绍,我们接下来要说的男主就是 “application.properties”. “男神”默认是生成在“/src/ma ...
- Relocation 状态压缩DP
Relocation Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 入坑IT十年(二)技术以外
上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...
- VBA /VB/VB中合成分散数据方法
公司用于项目号的合成,怕忘记,特此放上这里.若能帮助其它道友,善莫大焉. 比如:001,004,006,007,008,009,010 结果可以输出:001,004,006-010 逻辑:1.获得数据 ...
- Log4net日志使用教程-控制台、文本、数据库三种记录方式
一.log4net简介: 1. Log4net的优点: 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的 ...
- python3.6如何安装pymssql
使用pip install pymssql安装时,总是会出现UnicodeDecodeError.于是下载了适用的pymssql.whl进行安装,详细可参考 https://docs.microsof ...
- Ubuntu配置OpenStack 一:主机环境配置以及问题总结
本文包含openstack配置的实验环境的基本步骤.在下面的步骤中将逐步讲解如何操作. 1.准备三台虚拟机 主机名字分别命名为controller.network.computer[desktop版或 ...
- 初学者易上手的SSH-hibernate02 三种查询方式
在上一章中已经搭建好了一个hibernate的环境,那么这一章我们就使用这个环境来进行基本CRUD.在这之前我们先了解一个东西:主键生成策略.就是当向数据库表中插入记录的时候,这个记录的主键该如何生成 ...
- PHP 单例模式解析和实战
一.什么是单例模式? 1.含义 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 2. ...
- IDEA启动后页面没有tomcat server选项,显示灰色问号和红叉不能使用
说明:自己好几次硬盘莫名其妙读不出来导致电脑重启后idea没有了tomcat选项,原来的tomcat上显示灰色的问号和红色小叉子,网上搜了好久加上自己摸索,终于解决了.现在记一下也分享一下,省的下回又 ...