asp.net MSBuild脚本和VS2010发布应用Web.config转换

vktxenjb  于 2023-02-17  发布在  .NET
关注(0)|答案(6)|浏览(147)

所以,我已经安装了VS 2010,并且正在为TeamCity构建集成修改我的MSBuild脚本。除了一个例外,一切都运行得很好。
我如何告诉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方式完成

k7fdbhmy

k7fdbhmy1#

我正在寻找类似的信息,但没有找到,所以我在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>

我已经测试了上面的代码,可以确认它是有效的。您可能需要稍微调整一下结构,以便更好地适应您的构建脚本。

bq3bfh9z

bq3bfh9z2#

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

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/

yvt65v4c

yvt65v4c3#

或者,您可以尝试使用XDT转换工具
https://github.com/greenfinch/ctt
我用这个来代替模糊的msbuild目标。可以和*app.config*一起使用,而不仅仅是web.config

nfzehxib

nfzehxib4#

它对我起作用,但有以下更改

<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.
woobm2wo

woobm2wo5#

我不是MSBuildMaven,但我能够使用此链接中的信息完成相同的任务:
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
在文章的底部附近有一个与MSBuild相关的部分。希望这能有所帮助。

6yt4nkrj

6yt4nkrj6#

另一个答案这个主题后,我一直在寻找几天前,我处理这个问题:

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

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

    • 命令行中的MSBuild语句**

msbuild我的项目. csproj-t:清理-t:重新生成/p:生成时部署= true/p:发布配置文件="开发"

    • 工程**
    • 发布配置文件名称 *:发展
    • 解决方案配置名称 *:发展
    • 不起作用**
    • 发布配置文件名称 *:发展
    • 解决方案配置名称 *:开发

希望这有帮助!

相关问题