72

因此,我安装了 VS 2010,并且正在修改我的 MSBuild 脚本以进行 TeamCity 构建集成。除了一个例外,一切都很好。

我如何告诉 MSBuild 我想应用我在发布构建时创建的 Web.conifg 转换文件...

我有以下生成已编译网站的内容,但是它将 Web.config、Web.Debug.config 和 Web.Release.config 文件(全部 3 个)输出到已编译的输出目录。在工作室中,当我执行发布到文件系统时,它会进行转换并仅输出具有适当更改的 Web.config ...

<Target Name="CompileWeb">
    <MSBuild Projects="myproj.csproj" Properties="Configuration=Release;" />
</Target>

<Target Name="PublishWeb" DependsOnTargets="CompileWeb">
    <MSBuild Projects="myproj.csproj"
    Targets="ResolveReferences;_CopyWebApplication"
    Properties="WebProjectOutputDir=$(OutputFolder)$(WebOutputFolder);
                OutDir=$(TempOutputFolder)$(WebOutputFolder)\;Configuration=Release;" />
</Target>

任何帮助都会很棒..!

我知道这可以通过其他方式完成,但如果可能的话,我想使用新的 VS 2010 方式来完成

4

6 回答 6

63

我一直在寻找类似的信息,但没有找到,所以我在 Visual Studio 2010 和 MSBuild 4.0 附带的 .targets 文件中进行了一些挖掘。我认为这是寻找执行转换的 MSBuild 任务的最佳位置。

据我所知,使用了以下 MSBuild 任务:

<Project ToolsVersion="4.0"
         DefaultTargets="Deploy"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask TaskName="TransformXml"
               AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

    <PropertyGroup>
        <ProjectPath>C:\Path to Project\Here</ProjectPath>
        <DeployPath>C:\Path to Deploy\There</DeployPath>
        <TransformInputFile>$(ProjectPath)\Web.config</TransformInputFile>
        <TransformFile>$(ProjectPath)\Web.$(Configuration).config</TransformFile>
        <TransformOutputFile>$(DeployPath)\Web.config</TransformOutputFile>
        <StackTraceEnabled>False</StackTraceEnabled>
    </PropertyGroup>


    <Target Name="Transform">
        <TransformXml Source="$(TransformInputFile)"
                      Transform="$(TransformFile)"
                      Destination="$(TransformOutputFile)"
                      Condition="some condition here"
                      StackTrace="$(StackTraceEnabled)" />
    </Target>
</Project>

我已经测试了上述内容,并且可以确认它有效。您可能需要稍微调整结构以更好地适应您的构建脚本。

于 2010-06-03T22:07:51.267 回答
14

您应该能够通过使用 Package 目标并指定临时目录来完成此操作。

msbuild solution.sln /p:Configuration=Release;DeployOnBuild=true;DeployTarget=Package;_PackageTempDir=..\publish

http://pattersonc.com/blog/index.php/2010/07/15/visual-studio-2010-publish-command-from-msbuild-command-line/

于 2010-07-15T19:47:31.097 回答
8

或者,您尝试使用XDT 转换工具

http://ctt.codeplex.com

我正在使用它而不是弄乱晦涩的 msbuild 目标。与app.config一起工作,而不仅仅是web.config

于 2011-05-23T08:33:47.757 回答
5

通过以下更改对我有用

<MSBuild Projects="$(ProjectFile)"
         Targets="ResolveReferences;_WPPCopyWebApplication"
     Properties="WebProjectOutputDir=TempOutputFolder;OutDir=$(WebProjectOutputDir);Configuration=$(Configuration);" />

来自 MsBuild 文件夹下的 Microsoft.WebApplication.targets 文件

_CopyWebApplication

This target will copy the build outputs along with the 
content files into a _PublishedWebsites folder.

This Task is only necessary when $(OutDir) has been redirected
to a folder other than ~\bin such as is the case with Team Build.

The original _CopyWebApplication is now a Legacy, you can still use it by 
 setting $(UseWPP_CopyWebApplication) to true.
By default, it now change to use _WPPCopyWebApplication target in
 Microsoft.Web.Publish.targets.   
It allow to leverage the web.config trsnaformation.
于 2012-05-08T00:47:19.283 回答
0

我不是 MSBuild 专家,但我能够使用此链接中的信息来完成相同的任务:

http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

在文章底部附近有一个与 MSBuild 相关的部分。希望这可以帮助。

于 2010-05-25T13:43:43.823 回答
0

在我解决这个问题之前我已经搜索了几天之后,这个主题的另一个答案:

您的发布配置文件和配置名称应该匹配。

在我的情况下,我没有。通过发布配置文件手动发布给了我想要的结果,因为我的配置是在发布配置文件中设置的。然而,MSBuild 试图变得智能,并根据名称神奇地连接发布配置文件和配置。(在命令中添加 /p:Configuration 会导致有关引用项目的输出路径的其他奇怪错误)。

只是为了明确我的意思:

命令行中的 MSBuild 语句

msbuild myproject.csproj -t:Clean -t:Rebuild /p:DeployOnBuild=true /p:PublishProfile="Development"

作品

  • 发布配置文件名称:开发
  • 解决方案配置名称:开发

不工作

  • 发布配置文件名称:开发
  • 解决方案配置名称:Dev

希望这可以帮助!

于 2019-05-02T06:24:35.193 回答