在MSBuild之外是否可以使用Microsoft的XML文檔轉換來準備web.configs?我想使用PowerShell來完成這些轉換,而不必通過MSBuild引擎運行。如果微軟使用標準XSLT,在PowerShell中很容易做到。從我能告訴我必須使用他們的C:\ Program Files文件(x86)\ MSBuild \ Microsoft \ VisualStudio \ v10.0 \ Web \ Microsoft.Web.Publishing.Tasks.dll需要一個構建引擎。 謝謝Web.Config在Microsoft MSBuild之外轉換嗎?
回答
我創建了一個小函數來處理Microsoft的PowerShell中的XML文檔轉換。
我將Visual Studio生成文件夾中的Microsoft.Web.XmlTransform.dll文件複製到我的腳本路徑中,但是如果您願意,可以從源文件夾引用它。
function XmlDocTransform($xml, $xdt)
{
if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
throw "File not found. $xml";
}
if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
throw "File not found. $xdt";
}
$scriptPath = (Get-Variable MyInvocation -Scope 1).Value.InvocationName | split-path -parent
Add-Type -LiteralPath "$scriptPath\Microsoft.Web.XmlTransform.dll"
$xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
$xmldoc.PreserveWhitespace = $true
$xmldoc.Load($xml);
$transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
if ($transf.Apply($xmldoc) -eq $false)
{
throw "Transformation failed."
}
$xmldoc.Save($xml);
}
要使用web.release.config變換的web.config:
XmlDocTransform("Web.config", "Web.Release.config")
轉換的邏輯包含在TransformXml任務本身內部。如果你想從代碼中調用它,你將不得不使用帶有模擬引擎的MSBuild API並執行它。如果你想的話,我有一些代碼。
在你的情況,因爲你提到了PowerShell,最好的辦法就是創建一個包裝器MSBuild文件來調用TransformXml任務。我這樣說是因爲PowerShell配置爲在.NET 2.0下運行,但TransformXml任務需要.NET 4.0。爲了從一個虛擬的MSBuild文件中調用它,你可以在http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx查看我的博客,但我也粘貼了下面這個鏈接的示例。
<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<Target Name="Demo">
<TransformXml Source="app.config"
Transform="Transform.xml"
Destination="app.prod.config"/>
</Target>
</Project>
感謝賽義德,其實我去你的解決方案。我做了一些修改以包含3個項目的屬性組,所以我可以在PowerShell的命令行中覆蓋這些項目。據悉,從你的書:) '<的PropertyGroup>
採取看着使用MSDeploy,因爲它有PowerShell腳本API,允許您轉換和部署包。
你也可以看看XML-Document-Transform,如果你想要你可以編寫你自己的代碼來執行轉換。
這是一個codeplex項目,做了類似的事情。 XDT Transformation Tool
根據米歇爾的答案我寫了一個C#函數,將完成相同的。
當然,你可以調用與PowerShell中產生的DLL,但我實際上是尋找一個完全程序化的版本,所以在這裏它是,如果其他人正在尋找類似的解決方案:
using Microsoft.Web.XmlTransform;
...
public static void TransformConfig(string configFileName, string transformFileName)
{
var document = new XmlTransformableDocument();
document.PreserveWhitespace = true;
document.Load(configFileName);
var transformation = new XmlTransformation(transformFileName);
if (!transformation.Apply(document))
{
throw new Exception("Transformation Failed");
}
document.Save(configFileName);
}
你會只需要包含到以下參考:
C:\ Program Files文件 (86)\的MSBuild \微軟\ VisualStudio的\ 11.0 \網絡\ Microsoft.Web.XmlTransform.dll
很好的答案,這需要更多的投票! – Matt
微軟已將XDT發佈到codeplex http://xdt.codeplex.com並作爲NuGet包https://www.nuget.org/packages/Microsoft.Web.Xdt/。我也用MSBuild任務創建了一個NuGet豬,TransformXml和一個.exe來調用它們https://www.nuget.org/packages/SlowCheetah.Xdt/1.1.6-beta。
對於PowerShell,我創建了一個自引導腳本,您可以使用https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b。
有關自引導腳本的更多信息,請訪問http://sedodream.com/2014/07/22/StopCheckinginBinariesInsteadCreateSelfbootstrappingScripts.aspx。
我更新了腳本,使它能夠與最新版本的PowerShell配合使用,並且使它更容易一些。
function XmlDocTransform($xml, $xdt)
{
$scriptpath = $PSScriptRoot + "\"
$xmlpath = $scriptpath + $xml
$xdtpath = $scriptpath + $xdt
if (!($xmlpath) -or !(Test-Path -path ($xmlpath) -PathType Leaf)) {
throw "Base file not found. $xmlpath";
}
if (!($xdtpath) -or !(Test-Path -path ($xdtpath) -PathType Leaf)) {
throw "Transform file not found. $xdtpath";
}
Add-Type -LiteralPath "$PSScriptRoot\Microsoft.Web.XmlTransform.dll"
$xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
$xmldoc.PreserveWhitespace = $true
$xmldoc.Load($xmlpath);
$transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdtpath);
if ($transf.Apply($xmldoc) -eq $false)
{
throw "Transformation failed."
}
$xmldoc.Save($xmlpath);
Write-Host "Transformation succeeded" -ForegroundColor Green
}
並調用該函數使用
XmlDocTransform "App.config" "App.acc.config"
- 1. MSBuild - 轉換web.config
- 2. 在使用MSBuild發佈之前執行web.config轉換
- 3. Application_Start在Web.config文件轉換之前執行嗎?
- 4. Msbuild在轉換時未複製web.config文件
- 5. 爲什麼msbuild在web.config轉換中重複條目?
- 6. Web.Config轉換XPath
- 7. web.config的轉換
- 8. 轉換web.config
- 9. MSBuild腳本和VS2010發佈應用Web.config轉換
- 10. 將MSBuild參數傳遞給Web.config XDT轉換
- 11. Teamcity - 使用MSBuild構建時轉換web.config和password.config SlowCheetah 2.5.48
- 12. TFS/MSBuild構建/發佈web部署不會轉換web.config文件
- 13. 使用Asp.Net web.config轉換web.config以外的文件?
- 14. ASP.NET MVC 2和額外的Web.config文件中的Web.config轉換
- 15. Web.config文件轉換在VS2008
- 16. Web.config轉換變量
- 17. web.config文件轉換
- 18. VS 2010 web.config轉換
- 19. 無法轉換web.config
- 20. _CopyWebApplication與web.config轉換
- 21. Visual Studio Web.Config轉換
- 22. web.config轉換 - appSettings節點不轉換
- 23. 將Makefiles轉換爲MsBuild
- 24. ASP.NET Web.Config轉換問題
- 25. web.config轉換xml元素
- 26. 如何轉換web.config值?
- 27. 將htaccess轉換爲web.config
- 28. Web.Config轉換xml註釋
- 29. 轉換web.config文件爲.htaccess
- 30. VS.中的Web.Config轉換
神奇的是,這正是我需要編寫一個C#版本 –
我有一個自引導PowerShell腳本,你可以使用在https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b –
非常好!我必須在$ xml和$ xdt上調用Resolve-Path,但除此之外,它非常有用!,thx – PierrOz