通过指定默认构建配置,避免“未为项目设置OutputPath属性”

时间:2014-04-13 19:03:49

标签: .net visual-studio msbuild

我的情况类似于以下情况:

包含两个项目的解决方案, A B A 引用 B 。除了标准构建配置DebugRelease之外,我还想添加第三个Staging,这样我就可以为项目 A 执行一些配置转换只要。 Staging构建配置对 B 没有影响,因此我不会将其添加到项目中。

使用解决方案配置管理器,可以轻松设置此方案......当活动配置为Staging时, A 将使用Staging B 将使用Debug。解决方案按预期构建。但是,运行

msbuild A.csproj /p:Configuration=Staging

将失败The OutputPath property is not set for project B,因为B没有Staging构建配置。

我的问题是,在通过msbuild

构建项目时,是否有一种简单的方法可以避免这种情况

Staging配置添加到 B 会起作用,但仅将构建配置添加到它们影响的项目中似乎更加清晰,其余部分则保持不变。在一个包含许多项目的解决方案中,确保每个项目都具有所有可能的构建配置并不理想(尽管也不是很糟糕)。

1 个答案:

答案 0 :(得分:0)

在A.csproj文件中添加以下行

  <ItemGroup>
    <ConfigList Condition=" '@(ConfigList)' == '' and $(Config) != '' " Include="$(Config.Split('+'))" />
    <!-- parse all requested configurations into a list -->
    <ConfigList Condition=" '@(ConfigList)' == '' " Include="Debug" />
    <!-- if no configurations were specified, default to Debug -->
  </ItemGroup>

在A.csproj中添加引用项目的目标

  <Target Name="Build">
    <MSBuild Projects="$(MSBuildProjectDirectory)\..\B\B.csproj" Properties="Configuration=%(ConfigList.Identity);OutputPath=$(MSBuildProjectDirectory)\bin\%(ConfigList.Identity)" Targets="Build" />
  </Target>

使用/ p:Configuration = Staging构建项目A时,引用项目的目标将传递配置并创建所需的输出目录。

看看这个post,它清楚地描述了你想要做的事情。