![](/img/trans.png)
[英]Web Deploy/Publish Web site project using MsBuild.exe and command line with VS2010
[英]Publish Web Deploy using VS Code
在 Visual Studio 中,我使用“發布 Web”功能進行一些 web.config 轉換,並將 WebAPI 項目發布到我們的服務器。 發布是使用 Web Deploy 完成的。
現在我在使用 Visual Studio Code,我已經失去了那個工具。 但是,我想繼續使用 Web Deploy 發布項目。 有什么方法可以編寫一個 VSCode 任務來發布我的項目嗎?
Visual Studio Publish 使用 [target].pubxml 文件。 我有“staging.pubxml”和“production.xml”。 這些看起來像 MSBuild 文件。 因此,也許這只是從 Code 執行 msbuild 任務的問題。 不過,不確定從哪里開始。
另一個想法是我可以啟動 Web Deploy 命令行工具。 我從來沒有用過,似乎第一個想法會更好。
假設您現在使用的是最新的 vscode (1.7.x)。 您可以使用 Visual Studio Code 的 Task Runner。
首先,您需要按<F1>
並輸入task
來配置task
。 選擇任務:配置任務運行器。
tasks.json
將創建一個新文件tasks.json
,其內容如下。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true"
],
"taskSelector": "/t:",
"showOutput": "silent",
"tasks": [
{
"taskName": "build",
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
其次,現在您需要添加新的發布任務。 使用@Rolo 給出的答案,您可以在tasks
數組中添加一個新任務:
{
"taskName": "publish",
// Always show errors from builds.
"showOutput": "always",
"args": [
"/p:DeployOnBuild=true",
"/p:PublishProfile=Test"
]
}
第三, tasks.json
完成后。 您可以通過按Ctrl
+ P
(或 Mac 上的Cmd
+ P
)來使用publish
任務,然后輸入task publish
。
Visual Studio Code 沒有像 Visual Studio 那樣的集成構建系統(Web 發布)。 但它確實運行了命令行任務並內置了 Git。
所以你有幾個選擇:
1) 使用任務運行器從命令面板 (ctrl+p) 開始構建/發布。 Grunt在預覽中可用*。 這需要您手動編寫腳本,但是一旦完成,就很容易從那時開始執行任務。
(更新:文檔提到了其他兼容的任務運行器,包括:Make、Ant、Gulp、Jake、Rake 或 MSBuild——並且 .settings tasks.json 有如何讓你的 MSBuild 文件工作的示例。按 ctrl+p 類型:“運行任務”,然后單擊“配置任務”)
2) 為持續集成設置源代碼控制系統,以便當您將更新推送到特定分支時,它將運行 MSBuild(或其他構建系統)腳本並為您發布到服務器。 我們使用 Team Foundation Server (TFS) 和 Git。 我們有一個特定的“發布/主”分支,設置為在收到推送時構建和發布。 它也需要一些初始配置,但一旦完成,它是自動的。 如果您沒有 TFS,請在線試用 TFS。 還有很多其他選擇,但這就是我們使用的。
我和你試圖弄清楚這個問題的立場是一樣的。 我很想知道你發現了什么。
*根據Build 2015的Deep Dive 會議。 盡管查看 tasks.json 文件,但看起來 Gulp 和 MSBuild 示例在預覽版中可用。
為了使用 MSBuild 發布,您需要使用以下命令:
msbuild <Project or Solution Path> /p:DeployOnBuild=true /p:PublishProfile=<Publish Profile Name>
您可以指向一個解決方案,這將發布所有包含有效發布配置文件的項目:
msbuild <FullPath>\\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=Test
你可以像這樣指向一個特定的項目:
msbuild <FullPath>\\Project1\\MyProj.csproj /p:DeployOnBuild=true /p:PublishProfile=Test
在這兩種情況下,您還可以指定使用 .pubxml 文件的完整路徑:
msbuild <FullPath>\\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=<FullPath>\\PublishProfiles\\Test.pubxml
實際上 *.pubxml 文件是 MSBuild 腳本,因此您可以像處理任何其他 MSBuild 腳本一樣與其交互,例如,您可以像這樣從命令行替換屬性:
測試.pubxml
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>C:\Deploy\MyProject\</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
</PropertyGroup>
</Project>
msbuild <FullPath>\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=<FullPath>\PublishProfiles\Test.pubxml /p:publishUrl:"D:\DifferentPath\DifferentFolder\"
您可以從持續集成服務器或任何其他構建腳本中使用這些命令。
附加信息:
您可以使用dotnet publish
命令將其部署到本地或遠程目錄。
對於本地目錄
dotnet publish -c Release -o ./publish
對於遠程目錄
dotnet publish -c release -p:PublishDir=//serverName/d$/inetpub/wwwroot/appName
dotnet publish
在幕后調用 MSBuild。
-c configName
=> 定義構建配置。
-o dirName
=> 指定輸出目錄。
-p:PublishDir
=> 指定復制輸出目錄的目錄
要了解dotnet publish
命令,請訪問鏈接。
您還可以使用命令行在 VS Code 中使用發布配置文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.