使用 msdeploy 转换文件
我可以使用MSDeploy的配置转换机制来转换其他文件吗?
Can I use the config transforms mechanism of MSDeploy to transform other files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以使用MSDeploy的配置转换机制来转换其他文件吗?
Can I use the config transforms mechanism of MSDeploy to transform other files?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
(另一种方法)
msdeploy 打包是在项目的 MSbuild 运行期间调用的。
TransformXml 是 .csproj 或 .vsproj 构建的包含任务。
只需修改您的构建过程即可在您需要的任何文件上调用该任务。
例如,我们所做的是编写一个自定义目标
,然后修改您的 .csproj 以在调用发布任务之前运行它。
(another approach)
The msdeploy packaging is jsut invoked during an MSbuild run for your project.
TransformXml is an included task of a .csproj or .vsproj build.
Just modify your build process to invoke that task on whatever file you need.
For example, what we do is write a custom target
Then modify your .csproj to run this BEFORE the Publish task is invoked.
泰勒的回答对我不起作用,他没有提供更多细节。因此,我深入研究 Microsoft.Web.Publishing.targets 文件来寻找解决方案。可以将以下 MSBuild
Target
添加到项目文件中,以转换根应用程序目录中的所有其他配置文件。享受 :)The answer by Taylor didn't work for me and he didn't provide further details. So I went spelunking into the Microsoft.Web.Publishing.targets file to find a solution. The following MSBuild
Target
can be added to project file to transform all other config files in the root application directory. Enjoy :)简短回答:是的,可以。但这是“困难”的。
长答案:
当我们将站点部署到目的地时,我们有通常的 web.test.config 和 web.prod.config。在我们引入 log4net.test.config 和 log4net.prod.config 之前,这一切都很好。 MSBuild 不会自动遍历并替换所有这些。它只会执行 web.config 的操作。
如果您想了解具体情况,请查看最后一个代码片段。它显示了采用一个配置并将其替换为替代配置的功能。但是……如果我描述一下整个过程就会更有意义。
流程:
MSbuild 不会自动替换所有额外的配置。有趣的是 MSBuild 将删除任何“额外”配置。所以你的 log4net.test.config 在构建后就会消失。因此,您要做的第一件事就是告诉 msdbuild 将这些额外的文件保留在适当的位置。
您必须修改 vbProj 文件以包含新设置:
在您喜欢的文本编辑器中打开 Web 应用程序的 vbProj 文件。导航到您希望应用的每个部署配置(发布、生产、调试等)并将该配置添加到其中。这是我们的“发布”配置的示例。
因此,现在 msbduild 将构建项目并将这些额外文件保留在适当的位置,而不进行替换。现在你必须手动完成它们。
我们编写了一个 .net 应用程序来监视这些新的 zip 文件。我编写了一些代码,它将遍历整个 zip 包并找到与 {configname}.{env}.config 匹配的任何配置。它将提取它们、替换它们,然后将它们放回去。为了进行实际替换,我们使用 MSDeploy 使用的相同 DLL。我还使用 Ionic.Zip 来进行压缩。
因此,请添加对以下内容的引用:
导入:
这是通过 zip 文件旋转的代码
最后但最重要的是我们实际替换 web.configs 的位置。
就像我说的……这很困难,但可以做到。
Short answer: Yes you can. But it's "difficult".
Long answer:
When we deploy sites to destinations we had the usual web.test.config, and web.prod.config. This worked fine until we introduced log4net.test.config and log4net.prod.config. MSBuild will not automatically go through and replace all of these. It will only do the web.config ones.
If you want the nitty gritty go to the last code snippet. It shows the functions to take one config and replace it with a replacement. But... it will make more sense if I describe the whole process.
The process:
MSbuild will not automatically replace all of the extra configs. What's interesting is MSBuild will remove any "extra" configs. So your log4net.test.config will be gone after it's build. So the first thing you have to do is tell msdbuild to keep those extra files in place.
You have to modify your vbProj file to include a new setting:
Open your vbProj file for the web application into your favorite text editor. Navigate to each deploy configuration you want this to apply too (release, prod, debug, etc.) and add that config into it. Here is an example of our "release" config.
So now msbduild will build the project and keep those extra files in place and not do the replacements. Now you have to manually do them.
We wrote a .net app that will watch for these new zip files. I wrote some code that will spin through the whole zip package and find any configs that match the {configname}.{env}.config. It will extract them, replace them, and put them back. To do the actual replacement we use the same DLL's that MSDeploy uses. I also use Ionic.Zip to do the zip stuff.
So add reference to:
Import:
Here is the code that spins through the zip file
Lastly but the most important is where we actually do the replacement of the web.configs.
Like I said... it's difficult but it can be done.
只是为了添加到此 awnser,为了修改使用 msdeploy (webdeploy) 发布的应用程序中除 web.config 之外的其他文件,您可以在根目录下的parameters.xml 文件中设置
scope
属性项目:scope
是一个正则表达式,用于查找要应用match
xpath 的文件。我还没有对此进行广泛的实验,但据我了解,它只是将 xpath 匹配的内容替换为稍后提供的值。还有其他可用于
kind
的值,它们的行为与 xpath 不同,请参阅 https://technet.microsoft.com/en-us/library/dd569084(v=ws.10).aspx 了解详细信息注意:这适用于使用parameters.xml 时,而不是使用web.config.Debug/Release 文件时
Just to add to this awnser, in order to modify other files than the web.config in an application published with msdeploy (webdeploy) you can set the
scope
attribute in the parameters.xml file in the root of the project:scope
is a regex that will be used to find files to apply thematch
xpath to. I havent experimented with this extensively but as far as I understand it simply replaces what ever the xpath matches with the value that is provided later.There are also other values that can be used for
kind
that will have different behaviors than an xpath, see https://technet.microsoft.com/en-us/library/dd569084(v=ws.10).aspx for detailsnote: this applies to when you're using a parameters.xml, not when using the web.config.Debug/Release files