C# 如何获取VisualStudio用于发布应用程序的clickonce构建脚本

C# 如何获取VisualStudio用于发布应用程序的clickonce构建脚本,c#,visual-studio,msbuild,mage,C#,Visual Studio,Msbuild,Mage,我有一个相当大的多项目解决方案。我目前在VisualStudio中构建它,然后使用右键单击=>publish选项进行发布。这很有效 我现在的位置是,我至少需要自动化clickonce发布,这超出了我在VisualStudio中所能做的。我已成功创建了一个publish.xml文件,可与msbuild和mage一起使用该文件创建类似于clickonce的内容。但是,我希望能够将visual studio界面中的配置复制到publish.xml文件中,这样的配置非常复杂 是否可以让visual st

我有一个相当大的多项目解决方案。我目前在VisualStudio中构建它,然后使用右键单击=>publish选项进行发布。这很有效

我现在的位置是,我至少需要自动化clickonce发布,这超出了我在VisualStudio中所能做的。我已成功创建了一个publish.xml文件,可与msbuild和mage一起使用该文件创建类似于clickonce的内容。但是,我希望能够将visual studio界面中的配置复制到publish.xml文件中,这样的配置非常复杂

是否可以让visual studio生成一个文件,该文件提供visual studio通过右键单击=>publish过程发布时MSBuild和/或Mage使用的属性

基本上,我想要一个配置文件,可以使用它执行类似于msbuild publish.xml(或类似文件)的操作,并准确地获得visual studio生成的发布结果

我目前使用VS2017。该应用程序是使用c编写的winforms#

这是我的publish.xml。它不应该是测试之外的任何东西

<Project> 
    <PropertyGroup>
        <OutputPathBuild>C:\Users\d_000\Documents\Visual Studio 2017\Projects\CP\CP.UI.Winforms\bin\x86\Debug</OutputPathBuild>
        <Version>1.0.0.0</Version>
        <OutputPathDeployment>C:\Users\d_000\Documents\CP Deploy\test</OutputPathDeployment>
        <Mage>C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\mage.EXE</Mage>
    </PropertyGroup>
  <Target Name="Deployment">
    <Error Condition="'$(Version)'==''" Text="The Version property must be defined in order to build the Deployment task."/>

    <ItemGroup>
      <DeploySource Include="$(OutputPathBuild)\**"/>
    </ItemGroup>
    <Copy SourceFiles="@(DeploySource)" DestinationFolder="$(OutputPathDeployment)\v$(Version)\%(RecursiveDir)"/>
    <Exec Command='"$(Mage)" -New Application -ToFile "$(OutputPathDeployment)\v$(Version)\MyApp.manifest" -Name MyApp -Version $(Version) -Processor X86 -FromDirectory "$(OutputPathDeployment)\v$(Version)" -IconFile Resources\CPIcon.ico'/>
    <Exec Command='"$(Mage)" -New Deployment -ToFile "$(OutputPathDeployment)\MyApp.application" -Name MyApp -Version $(Version) -Processor X86 -AppManifest "$(OutputPathDeployment)\v$(Version)\MyApp.manifest" -IncludeProviderURL true -ProviderURL "http://example.com/download/MyApp/MyApp.application" -Install true'/>
    <!-- Grr... Mage tool has a bug whereby -MinVersion only works in Update mode... -->
    <Exec Command='"$(Mage)" -Update "$(OutputPathDeployment)\MyApp.application" -MinVersion $(Version)'/>
    <Copy SourceFiles="$(OutputPathDeployment)\MyApp.application" DestinationFiles="$(OutputPathDeployment)\MyApp.v$(Version).application"/>
  </Target>
  </Project>

C:\Users\d\u 000\Documents\Visual Studio 2017\Projects\CP\CP.UI.Winforms\bin\x86\Debug
1.0.0.0
C:\Users\d\u 000\Documents\CP Deploy\test
C:\Program Files(x86)\Microsoft SDK\Windows\v10.0A\bin\NETFX 4.6.1 Tools\mage.EXE
编辑:


我需要自定义发布过程中的内容,因此我希望了解VisualStudio正在做什么。基本上,我希望在visual studio中运行命令,复制并调整它们,以便使用不同的delpoy URL自动执行多个不同的生成,并更新URL

如果要为不同的阶段创建ClickOnce,则必须在调用MSBuild之前操作proj文件

1) 读取你的.proj文件

$projFilePath = Get-ChildItem $sourceRootDirectory -Filter $ProjectFileName -Recurse 
$XmlDocument = [xml] (Get-Content -Path $projFilePath.FullName -Encoding UTF8) 
2) 您必须在项目文件中更新此属性:

$ClickOnceForStages.Split("Dev", "Test", "UAT", "Prod") | Foreach { 

  $XmlDocument.project.PropertyGroup[0].ApplicationRevision = $BuildRevision
  $XmlDocument.project.PropertyGroup[0].ApplicationVersion = $BuildVersion
  $XmlDocument.project.PropertyGroup[0].ProductName = "MyProductName.$_" 
  $XmlDocument.project.PropertyGroup[0].AssemblyName = "MyAssemblyName.$_"
  $XmlDocument.project.PropertyGroup[0].PublishUrl = "MyClickOnceDropUncPath_$_"
}      
您必须确保:

$BuildRevision and $BuildVersion 
为每个版本增加一个版本

3) 保存文档而不操纵原始项目文件

$currentProjFilePath = $projFilePath.FullName +"_" + $_ + ".proj"
$XmlDocument.Save($clickOnceProjFilePath)
4) 调用MSBuild创建ClickOnce安装程序

"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
项目文件与ClickOnce配置和MSBuild参数的组合:

"/target:publish"
创建ClickOnce安装程序

这是我用来创建ClickOnce安装程序的完整MSBuild语句:

"$clickOnceProjFilePath", 
"/target:publish", 
"/p:PublishDir=$outputDirectory", 
"/p:Configuration=Release"

k7的工作帮助我找到了我的解决方案——所以我把它标记为答案,但这给出了我最终如何解决它的细节

因此,事实证明,运行发布的所有信息都包含在项目文件中,您几乎可以通过msbuild(为了清晰起见,添加了换行符)来运行它。对于那些不熟悉csProj文件体系结构的人来说,它包含了用于构建、发布和配置的项目的许多属性,以及一组目标,它们类似于您可以运行的不同作业。您可以通过使用msbuild指定/t:选项来运行它们。未指定的目标不会执行

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\msbuild.EXE"
    "C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\myProj" /t:publish
问题是

  • 如果在build命令中引用项目而不是解决方案,则还需要指定解决方案文件夹
  • 除非指定“发布前重新生成”,否则对项目配置所做的任何更新都不会反映在输出中-如果在多个配置下发布,这是一个问题
  • 指定要部署的位置
  • 您可能还需要指定平台和配置
  • 在这种情况下,我的命令

    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\msbuild.EXE"
       "C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\myProj" 
       /:SolutionDir="C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\\" 
       /t:rebuild /t:publish
       /p:PlatformTarget=x86 /p:Configuration=Release
       /p:PublishDir="C:\Users\me\Documents\Deploy\web\\
    
    此时,我正在重建和发布我的项目,就像在VisualStudio中一样

    当我想要自定义时,我只是将附加信息添加到命令行中以覆盖

    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\msbuild.EXE"
       "C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\myProj" 
       /:SolutionDir="C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\\" 
       /t:rebuild /t:publish
       /p:PlatformTarget=x86 /p:Configuration=Release
       /p:PublishDir="C:\Users\me\Documents\Deploy\web\\ 
       /p:UpdateUrl=http://example.com/
       /p:ExcludeDeploymentUrl=false
    
    我想更进一步,减少一些命令,所以我修改了myProj.csProj,但我想把代码分开。为此,我在项目文件中的发布信息之后直接添加了一个导入(这是一个属性组,其成员为publishURL和UpdateEnabled)。这直接跟在相关标签后面

    <Import Project="CustomBuild.targets" />
    

    我的代码没有指定生成的自动递增,因为这与我无关,但通过对msbuild工具和目标进行一些研究,或者使用上面k7中的信息,这相对容易做到。

    如果我正确理解了您的问题,您的
    属性
    文件夹中应该已经有了
    .pubxml
    文件?没有-似乎所有的发布设置都包含在项目文件中谢谢k7,这是有用的信息,但并不能完全满足我的需要。我需要自定义发布过程中的内容,因此我希望了解VisualStudio正在做什么。基本上,我想让命令在VisualStudio中运行,复制并调整它们,这样我就可以使用不同的delpoy URL自动执行多个不同的构建并更新URL。这也是可能的。您必须在调用MSBuild之前操作proj文件。谢谢k7,这看起来很有希望。我最终得到了一个不同的解决方案,它允许我通过导入项目文件来完成几乎所有的事情。当我有时间的时候,我也会发布这个,但是你的解决方案看起来会让我到达我想去的地方,但是通过一条不同的路线,所以会将它标记为答案。感谢您的帮助别担心,我们有同样的遗留项目,它们都在使用“ClickOnce遗留废话”,一年前一位同事说,完全自动化ClickOnce部署是不可能的。我研究了VS createsCheers-我下面为您提供的信息的解决方案,我认为当您不增加版本时,ClickOnce更新机制无法正常工作。不一定如此k7。在我的应用程序中,我使用版本号来反映数据层和客户机,因为这两者都包含在构建中。我的版本号是9.x.1.y,其中x是数据层(对于c
        <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <PublishType>none</PublishType>
      </PropertyGroup>
    
      <Choose>
        <When Condition="'$(PublishType)' == 'web'">
          <PropertyGroup>
            <PublishDir>C:\Users\me\Documents\Deploy\Web\</PublishDir>
            <PublishUrl>C:\Users\me\Documents\Deploy\Web\</PublishUrl>
            <MapFileExtensions>false</MapFileExtensions>
            <UpdateUrl>http://www.example.com/myAppUpdate</UpdateUrl>
            <ExcludeDeploymentUrl>false</ExcludeDeploymentUrl>
            <CreateWebPageOnPublish>true</CreateWebPageOnPublish>     
        </PropertyGroup>
    
        </When>
        <When Condition=" '$(PublishType)' == 'network' ">
          <PropertyGroup>
            <PublishDir>C:\Users\me\Documents\Deploy\Network\</PublishDir>
            <PublishUrl>C:\Users\me\Documents\Deploy\Network\</PublishUrl>
            <MapFileExtensions>false</MapFileExtensions>
            <UpdateUrl>\\DummyServer\Share\</UpdateUrl>
            <ExcludeDeploymentUrl>true</ExcludeDeploymentUrl>
          </PropertyGroup>
        </When>
      </Choose>
    
    
    </Project>
    
    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\msbuild.EXE"
       "C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\myProj" 
       /:SolutionDir="C:\Users\me\Documents\Visual Studio 2017\Projects\mySoln\\" 
       /t:rebuild /t:publish
       /p:PlatformTarget=x86 /p:Configuration=Release /p:PublishType=web