las*_*hou 10 msbuild visual-studio
我的VS解决方案中有两个项目,BookApp.Web并且BookApp.Domain.
BookApp.Web参考BookApp.Domain.
BookApp.Web具有以下生成配置:debug,staging,prod-eu,prod-us和prod-as.我们有三个数据中心用于生产和临时环境.
BookApp.Domain到目前为止只有两个构建配置,debug.
从Visual Studio中构建解决方案时,我可以使用构建配置程序来确保无论为Web项目选择什么构建配置,调试配置始终用于Domain项目.
但是,在我的持续集成服务器上使用MSBuild构建时,事情就出错了.我在rollout.msbuild文件中使用它:
<MSBuild Projects="BookApp.Web\BookApp.Web.csproj" Properties="Configuration=Prod-us" />
Run Code Online (Sandbox Code Playgroud)
当我运行它时,MSBuild期望所有依赖项目具有相同的构建配置.由于情况并非如此(并且不应该是IMO),因此失败并显示以下错误消息:
The OutputPath property is not set for project 'BookApp.Domain.csproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Prod-us' Platform='AnyCPU'.
Run Code Online (Sandbox Code Playgroud)
对相关问题的回答建议为每个构建配置创建单独的.sln解决方案,并使用MSBuild运行该解决方案.对我来说听起来不是一个好主意.
将所有构建配置复制到域项目也不理想.
有没有更好的方法告诉MSBuild使用不同的构建配置?
看看这个答案,它解释了如何通过 MSBuild 任务将配置从一个项目传递到另一个项目,并使用配置的元数据传递目标项目所需的配置
更新
我使用类库(Sample.Domain)和ConsoleApplication(SampleApp.Console)创建了一个解决方案。我向 SamplApp.Console 添加了另外两个配置:prod-us;prod-eu,Sample.Domain 保留为 debug;release。
然后我更改了 ConsoleApplication 的 csproj 文件,如下所示:
项目参考
<!--<ItemGroup>
<ProjectReference Include="..\Sample.Domain\Sample.Domain.csproj">
<Project>{73e8a7fd-0a24-47c5-a527-7601550d4b92}</Project>
<Name>Sample.Domain</Name>
</ProjectReference>
</ItemGroup>-->
<ItemGroup>
<ProjectReference Include="..\Sample.Domain\Sample.Domain.csproj" >
<Targets>Build</Targets>
</ProjectReference>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
在传递给 MSBuild 的配置上添加了一个 switch case,以配置输出文件和参考文件的一些属性:
<Choose>
<When Condition="'$(Configuration)' != 'Debug'">
<PropertyGroup>
<OutputProperty>$(OutputPath)\$(Configuration)</OutputProperty>
<FileCopy>$(OutputProperty)</FileCopy>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<OutputProperty>$(OutputPath)</OutputProperty>
<FileCopy>$(OutputProperty)</FileCopy>
</PropertyGroup>
</Otherwise>
</Choose>
Run Code Online (Sandbox Code Playgroud)
创建了一个 Target 来切换传递给 MSBuild 的 Configuration,以便 Debug 将 Debug 传递给 Sample.Domain,其他所有内容都将传递给 Release
<Target Name="MultiConfiguration" >
<CreateProperty Value="Debug">
<Output TaskParameter="Value" PropertyName="LibConfiguration" Condition="'$(Configuration)' == 'Debug'"/>
</CreateProperty>
<CreateProperty Value="Release">
<Output TaskParameter="Value" PropertyName="LibConfiguration" Condition="'$(Configuration)' != 'Debug' "/>
</CreateProperty>
</Target>
Run Code Online (Sandbox Code Playgroud)
构建目标使用我们添加的属性,因此引用文件的输出和副本将根据配置值具有正确的值
<!--Build Process-->
<Target Name="Build" DependsOnTargets="Clean;MultiConfiguration;ComputeProjectReference" >
<Csc Sources="@(Compile)" References="@(NewAssemblies)" TargetType="exe" OutputAssembly="$(OutputProperty)\$(AssemblyName).exe"/>
</Target>
<Target Name="ComputeProjectReference" Inputs="@(ProjectReference)" Outputs="%(ProjectReference.Identity)__Forced">
<MSBuild Projects="@(ProjectReference)" Targets="%(ProjectReference.Targets)" Properties="Configuration=$(LibConfiguration);Platform=AnyCPU;OutputPath=bin\$(LibConfiguration)">
<Output TaskParameter="TargetOutputs" ItemName="ResolvedProjectReferences"/>
</MSBuild>
</Target>
<Target Name="AfterProjectReference" AfterTargets="ComputeProjectReference">
<CreateItem Include="@(ResolvedProjectReferences)">
<Output TaskParameter="Include" ItemName="CopyFiles" />
</CreateItem>
<Copy SourceFiles="@(CopyFiles)" DestinationFolder="$(FileCopy)" SkipUnchangedFiles="false" />
<ItemGroup>
<NewAssemblies Include="$(OutputProperty)\%(CopyFiles.FileName)%(CopyFiles.Extension)" />
</ItemGroup>
</Target>
Run Code Online (Sandbox Code Playgroud)
调用调试配置是这样完成的 msbuild SampleApp.Console.csproj
调用 (Release;prod-us;prod-eu;...) 是这样完成的 msbuild SampleApp.Console.csproj /p:Configuration="prod-us" /p:OutputPath="bin"
我确信它可以优化,并且可能更容易一些,但它确实有效。
| 归档时间: |
|
| 查看次数: |
1462 次 |
| 最近记录: |