Azure devops 在Azure DevOps CI管道中替换$(SolutionDir)

Azure devops 在Azure DevOps CI管道中替换$(SolutionDir),azure-devops,msbuild,Azure Devops,Msbuild,我有以下情况: 一个SQL项目,其中安装了nuget包。这个包(仅仅是一个PS脚本)负责使用相对于解决方案文件夹的路径(查找包/和DACPAC/文件夹,并浏览从.sln文件中提取的项目)的引用来解包DB所需的DACPAC。这称为预构建事件 构建整个解决方案时,将按预期(本地和ADO)定义$(SolutionDir) 生成测试项目时,$(SolutionDir)为“”或“*未定义*”。同样,正如所料,因为msbuild在构建单个项目时不了解解决方案。我可以在当地接受这个警告,没问题 问题是:有没有

我有以下情况:

一个SQL项目,其中安装了nuget包。这个包(仅仅是一个PS脚本)负责使用相对于解决方案文件夹的路径(查找包/和DACPAC/文件夹,并浏览从.sln文件中提取的项目)的引用来解包DB所需的DACPAC。这称为预构建事件

构建整个解决方案时,将按预期(本地和ADO)定义$(SolutionDir)

生成测试项目时,$(SolutionDir)为“”或“*未定义*”。同样,正如所料,因为msbuild在构建单个项目时不了解解决方案。我可以在当地接受这个警告,没问题

问题是:有没有什么“神奇”的东西可以让我在Azure DevOps中工作

如果有人知道这些方法,我可以尝试各种破解方法,尽管我想要一个干净的解决方案

迄今为止:

1) 添加以下PropertyGroup:

<PropertyGroup>
  <SolutionDir Condition="'$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*'">.\</SolutionDir>
</PropertyGroup>

.\
到测试项目

2) 以下建议:


无效。

如果您的文件夹结构类似于:

1.Azure Devops Repos包含解决方案文件夹(其中存在xx.sln文件)

2.这些项目位于同一解决方案文件夹下

您可以尝试我的脚本:

  <PropertyGroup>
    <ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
    <MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
  </PropertyGroup>
Edit1:

您还可以添加以下条件:

  <PropertyGroup>
    <ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
    <MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
    <SolutionDir Condition="xxxx">$(MySolutionDir)</SolutionDir>
    It's recommended to add my script and PreBuildEvent in same propertyGroup, and mine should be in the first.
    <PreBuildEvent>echo $(MySolutionDir)</PreBuildEvent> 
  </PropertyGroup>
现在将其更改为:

  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
    <!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
    <SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
    <VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
  </PropertyGroup>
  <Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
  <Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
  <ItemGroup>
    <Folder Include="Properties" />
  </ItemGroup>
  <ItemGroup>
    <Build Include="test.sql" />
  </ItemGroup>
  <PropertyGroup>
    <ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
    <ParentFolder>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</ParentFolder>
    <SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
    <PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
  </PropertyGroup>

11
真的
11
$([System.IO.Directory]::GetParent($(ProjectDir)))
$([System.IO.Directory]::GetParent($(ProjectFolder)))\
$(父文件夹)
echo$(SolutionDir)
另外,删除
$(ProjectDir)
中的额外
'
。它应该是
$(ProjectDir)
,而不是
$(ProjectDir)
$(ProjectFolder)
。我还看到您有两个
PreBuildEvent
属性,只需将其中一个保留在自定义脚本中即可。在完成上述步骤后,您的项目现在在我这边运行良好:


如果有帮助,请随时告诉我。如果它不适用于您的场景,我想您可能有一个与我不同的文件夹结构。在此处共享有关文件夹结构的详细信息,我们可以一起修改脚本以使其适用于您的文件夹结构也~结构是这样的:D:\VSTSBuildAgents\\u work\791\s\不是真正的解决方案,现在Visual Studio抱怨ProjectDir为“”,无法保存项目。这不是我第一次遇到ProjectDir问题,在我以前的一些尝试中,这已经发生过。尤其是带有build.props文件的文件,但您最初对文件夹结构的假设是正确的。Root包含.sln和2个文件夹,我们称它们为MyDatabase\和MyDatabase.Tests\Update:我尝试在ProjectDir周围加上单引号,现在VS不再抱怨了。但是,现在,即使在我构建解决方案时,该值也会被评估为D:\VSTSAgents\\\\ U work\\ U tasks\VSBUILD71A9A2D3-a98a-4caa-96ab-affca411ecda\1.166.0\…更新2:通过引入条件对上述问题进行排序。但是,我现在在构建单个项目时得到了以下路径:“D:\VSTSAgents\\\ u work\3320\s\bin\Debug\”,这是2个级别2高。几乎感觉GetParent什么都不做…build.props文件的内容是什么?我想关于ProjectDir的问题与该文件有关。如果您有一个包含多个项目的干净解决方案,您可能会发现我的答案很有效,因为我们有相似的文件结构。尝试禁用该文件并再次进行测试。。。
  ....
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
    <!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
    <SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
    <VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
    <ProjectFolder>$([System.IO.Directory]::GetParent($'(ProjectDir)'))</ProjectFolder>
    <ParentFolder>$([System.IO.Directory]::GetParent($'(ProjectFolder)'))\</ParentFolder>
    <SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
    <PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
  </PropertyGroup>
  <Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
  <Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
  <ItemGroup>
    <Folder Include="Properties" />
  </ItemGroup>
  <ItemGroup>
    <Build Include="test.sql" />
  </ItemGroup>
  <PropertyGroup>
    <PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
  </PropertyGroup>
</Project>
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
    <!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
    <SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
    <VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
  </PropertyGroup>
  <Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
  <Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
  <ItemGroup>
    <Folder Include="Properties" />
  </ItemGroup>
  <ItemGroup>
    <Build Include="test.sql" />
  </ItemGroup>
  <PropertyGroup>
    <ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
    <ParentFolder>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</ParentFolder>
    <SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
    <PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
  </PropertyGroup>