相关
我的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 项目选择哪种生成配置,调试配置始终用于域项目。
但是,在我的持续集成服务器上使用 MSBuild 进行构建时,出现问题。我在我的rollout.msbuild文件中使用它:
<MSBuild Projects="BookApp.WebBookApp.Web.csproj" Properties="Configuration=Prod-us" />
当我运行它时,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'.
相关问题的答案建议为每个生成配置创建单独的.sln解决方案,并使用 MSBuild 运行该解决方案。对我来说,这听起来不是一个好主意。
将所有生成配置复制到域项目也不理想。
有没有更好的方法来告诉 MSBuild 使用不同的生成配置?
看看这个答案,它解释了配置如何通过 MSBuild 任务从一个项目传递到另一个项目,并使用配置的元数据传递目标项目的所需配置
这里
更新
我创建了一个包含类库(Sample.Domain)和ConsoleApplication(SampleApp.Console)的解决方案。 我在 SamplApp.Console 中添加了另外两个配置:prod-us;prod-eu,Sample.Domain 保留在 debug;release 中。
然后我更改了控制台应用程序的 csproj 文件,如下所示:
项目参考
<!--<ItemGroup>
<ProjectReference Include="..Sample.DomainSample.Domain.csproj">
<Project>{73e8a7fd-0a24-47c5-a527-7601550d4b92}</Project>
<Name>Sample.Domain</Name>
</ProjectReference>
</ItemGroup>-->
<ItemGroup>
<ProjectReference Include="..Sample.DomainSample.Domain.csproj" >
<Targets>Build</Targets>
</ProjectReference>
</ItemGroup>
在传递给 MSBuild 的配置上添加了开关案例,以配置输出文件和引用文件的某些属性:
<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>
创建了一个目标来切换传递给 MSBuild 的配置,以便调试将调试传递给 Sample.Domain,其他所有内容都将传递发布
<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>
生成目标使用我们添加的属性,因此引用文件的输出和副本将根据配置值具有正确的值
<!--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>
调用调试配置是这样完成的msbuild SampleApp.Console.csproj
调用 (Release;prod-us;prod-eu;...) 是这样完成的msbuild SampleApp.Console.csproj/p:Configuration="prod-us"/p:OutputPath="bin"
我相信它可以优化,并且可能会更容易,但它有效。