首页 > 解决方案 > 根据项目名称设置 msbuild OutputPath

问题描述

我正在尝试改进 .NET 解决方案的构建服务器设置。构建是使用完成msbuild solution.sln /p:OutputPath="$pwd/build"的,另外还有一个Directory.Solution.targets解决 msbuild 的怪癖(来自相关问题,请参阅下面的内容)。现在我们决定将测试转移到单独的项目中,这样测试 dll 及其依赖项就不会出现在 OutputPath 中。通常这不是问题,但由于历史原因,解决方案中的每个项目都将其 OutputPath 设置为..\..\. 这就是为什么我在调用时覆盖它的原因msbuild。但现在我只需要为一些项目覆盖它——我不知道怎么做。

现在的项目结构如下所示。

app/
    sources/
            Complete.sln
            Proj1/
                  proj1.csproj
            Proj1.Test/
                  proj1.test.csproj

开发人员从 Visual Studio 构建解决方案会导致此结果(仅显示新文件),这很好。

app/
    proj1.exe
    sources/Proj1.Tests/bin/Release/
                                    proj1.exe
                                    proj1.tests.dll

但是构建它会msbuild Complete.sln /p:OutputPath="$pwd/build" /t:BuildAll导致以下结果。

app/build/
    proj1.exe
    proj1.tests.dll

有没有办法在不更改解决方案或项目文件的情况下获得以下内容?

app/build/
    proj1.exe
app/tests/
    proj1.exe
    proj1.tests.dll

即我想将OutputPathfor each 设置*.Tests.csproj/tests,并将 for each other project 设置为/build

或者,有没有办法以msbuild Complete.sln这样的方式调用它从该解决方案构建所有项目,其名称不以“测试”结尾,或者只构建那些名称?

上面提到的Directory.Solution.targets(使用它的原因)如下所示。

<Project>
  <Target Name="SetSkip">
    <ItemGroup>
      <ProjectReference Update="*">
        <SkipNonexistentProjects>Build</SkipNonexistentProjects>
      </ProjectReference>
    </ItemGroup>
  </Target>
  <Target Name="BuildAll" DependsOnTargets="SetSkip">
    <CallTarget Targets="Build"/>
  </Target>
</Project>

UPDATE因为文件数量增加,为了更容易测试我创建了一个存储库

标签: msbuild

解决方案


您可以尝试将以下内容添加到Directory.Build.targets(我尚未对其进行测试,但应该可以):

   <PropertyGroup>
      <OutputPath Condition="'$(BuildOutputPath)' != ''
          and !$(MSBuildProjectName.EndsWith('.Test'))">$(BuildOutputPath)</OutputPath>
      <OutputPath Condition="'$(TestOutputPath)' == ''
         and $(MSBuildProjectName.EndsWith('.Test'))">$(TestOutputPath)</OutputPath>
   </PropertyGroup>

然后像这样调用 msbuild:

msbuild Complete.sln /p:TestOutputPath="$pwd\tests" /p:BuildOutputPath="$pwd\build"

还有其他选项,例如将所需的buildtests路径直接放在属性中。


推荐阅读