从 msbuild 中的 nugets 导入道具或目标
Importing props or targets from nugets in msbuild
我正在使用 PackageReference 从 nuget 导入道具文件。使用命令行处理此问题不是问题,因为 msbuild /t:restore
可以解析项目文件并忽略丢失的文件。 msbuild /t:build
然后有了有效的路径来处理恢复。
Visual Studio 但是无法加载包含此类导入的任何项目,因为它确定这是一个无效的 csproj。如何让 Visual Studio 忽略此加载导入?
请注意,如果我首先从命令行使用 msbuild /t:restore
(以便生成 XXX.csproj.nuget.g.props
和 XXX.csproj.nuget.g.targets
),Visual Studio 可以很好地加载项目。我想避免这种依赖。
导入片段:
<ItemGroup>
<PackageReference Include="ScopedManifest.Task" GeneratePathProperty="true" />
</ItemGroup>
<Import Project="$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props" />
在 VS 中加载项目时出错:
error : The imported project "D:\include\ScopedManifestTask.Props"
was not found. Confirm that the expression in the Import declaration
"\include\ScopedManifestTask.Props" is correct, and that the file
exists on disk. D:\work\test-build\repo\src\Manifest\Manifest.csproj
使用条件。当您在 Visual Studio.
中将包含道具或目标的 NuGet 包添加到您的项目时,也是这样做的。
<Import Project="$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props" Condition="Exists('$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props')" />
Nuget 还添加了这个,因此您会在缺少导入时遇到构建错误:
<Project>
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props')" Text="$([System.String]::Format('$(ErrorText)', '$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props'))" />
</Target>
...
</Project>
首先,我想知道这个nuget包是不是你自己创建的,而且你似乎想在nuget包中导入一个props文件。但是我发现你使用了Include节点,这不是正确的实现方式。
请尝试使用 Build 节点而不是 Include 节点并将自定义目标命名为 <package_id>.targets
,然后打包您的nuget 项目。
事实上,当你的nuget包包含这样的<package_id>.targets
文件时,它会自动嵌入到新项目中当nuget包是安装。更多信息可以参考this document.
说明该文件对nuget包有重要作用,而且在nuget包安装过程中还有额外的辅助操作,所以不能轻易删除该文件。那就是 MSBuild 道具和目标工作机制。参见 this link。
所以你不能避免在项目中使用这个文件,你应该把它加回来。
建议
1)对于新的sdk格式项目,此节点导入在project folder\obj\xxx.csproj.nuget.g.props
.
首先,确保nuget包中存在xxx.targets
文件
然后在xxx.csporj
和xxx.csproj.nuget.g.props
.
上检查那个路径下的import路径是否正确
我认为您的导入路径在构建时自动生成的 xxx.csproj.nuget.g.props
中有所不同。那就是nuget机制自动抓取的地址。您需要检查并保持一致。您可以删除 bin
和 obj
文件夹,然后重建您的项目以再次测试它。
如果不一致,可能需要用官方的方式重新创建这个nuget包
2)如果你的nuget包坏了,你应该在全局nuget缓存下删除它,然后重新恢复。
-- clean nuget caches先或删除C:\Users\xxx(current user)\.nuget\packages
下的所有文件
-- 删除bin
、obj
文件夹和xxx.csporj
文件
中的任何自定义导入节点
-- 进行还原操作,然后重建您的项目以再次测试。
我正在使用 PackageReference 从 nuget 导入道具文件。使用命令行处理此问题不是问题,因为 msbuild /t:restore
可以解析项目文件并忽略丢失的文件。 msbuild /t:build
然后有了有效的路径来处理恢复。
Visual Studio 但是无法加载包含此类导入的任何项目,因为它确定这是一个无效的 csproj。如何让 Visual Studio 忽略此加载导入?
请注意,如果我首先从命令行使用 msbuild /t:restore
(以便生成 XXX.csproj.nuget.g.props
和 XXX.csproj.nuget.g.targets
),Visual Studio 可以很好地加载项目。我想避免这种依赖。
导入片段:
<ItemGroup>
<PackageReference Include="ScopedManifest.Task" GeneratePathProperty="true" />
</ItemGroup>
<Import Project="$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props" />
在 VS 中加载项目时出错:
error : The imported project "D:\include\ScopedManifestTask.Props" was not found. Confirm that the expression in the Import declaration "\include\ScopedManifestTask.Props" is correct, and that the file exists on disk. D:\work\test-build\repo\src\Manifest\Manifest.csproj
使用条件。当您在 Visual Studio.
中将包含道具或目标的 NuGet 包添加到您的项目时,也是这样做的。<Import Project="$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props" Condition="Exists('$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props')" />
Nuget 还添加了这个,因此您会在缺少导入时遇到构建错误:
<Project>
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props')" Text="$([System.String]::Format('$(ErrorText)', '$(Pkg_ScopedManifest_Task)\include\ScopedManifestTask.Props'))" />
</Target>
...
</Project>
首先,我想知道这个nuget包是不是你自己创建的,而且你似乎想在nuget包中导入一个props文件。但是我发现你使用了Include节点,这不是正确的实现方式。
请尝试使用 Build 节点而不是 Include 节点并将自定义目标命名为 <package_id>.targets
,然后打包您的nuget 项目。
事实上,当你的nuget包包含这样的<package_id>.targets
文件时,它会自动嵌入到新项目中当nuget包是安装。更多信息可以参考this document.
说明该文件对nuget包有重要作用,而且在nuget包安装过程中还有额外的辅助操作,所以不能轻易删除该文件。那就是 MSBuild 道具和目标工作机制。参见 this link。
所以你不能避免在项目中使用这个文件,你应该把它加回来。
建议
1)对于新的sdk格式项目,此节点导入在project folder\obj\xxx.csproj.nuget.g.props
.
首先,确保nuget包中存在xxx.targets
文件
然后在xxx.csporj
和xxx.csproj.nuget.g.props
.
我认为您的导入路径在构建时自动生成的 xxx.csproj.nuget.g.props
中有所不同。那就是nuget机制自动抓取的地址。您需要检查并保持一致。您可以删除 bin
和 obj
文件夹,然后重建您的项目以再次测试它。
如果不一致,可能需要用官方的方式重新创建这个nuget包
2)如果你的nuget包坏了,你应该在全局nuget缓存下删除它,然后重新恢复。
-- clean nuget caches先或删除C:\Users\xxx(current user)\.nuget\packages
-- 删除bin
、obj
文件夹和xxx.csporj
文件
-- 进行还原操作,然后重建您的项目以再次测试。