Azure Pipeline中NuGet Pack命令-Exclude参数不生效问题
问题:Azure Pipeline打包NuGet时无法排除EF的TT模板文件
问题描述
使用Azure Pipeline打包私有NuGet包时,EF的*.tt模板文件被自动打包到包的content目录下,导致引用该包的项目出现构建问题。尝试用nuget pack的-Exclude *.tt参数排除这些文件,但未生效。
项目目录结构:
- ROOT - DataLayer - EntityFramework - Common_Model.context.vb - Common_Model.edmx - Common_Model.vb
打包后NuGet包中多余的文件:
- ROOT - DataLayer - EntityFramework - Common_Model.context.tt - Common_Model.tt
使用的打包命令:
nuget.exe pack Company_PR_WS\Company_PR.vbproj -version 1.0.26 -Verbosity detailed -Exclude *.tt -OutputDirectory D:\a\8\a -Properties Configuration=release -NonInteractive
构建日志显示TT文件仍被添加到content目录:
Add file 'D:\a\8\s\Company_PR_WS\DataLayer\EntityFramework\Common_Model.Context.tt' to package as 'content\DataLayer\EntityFramework\Common_Model.Context.tt' Add file 'D:\a\8\s\Company_PR_WS\DataLayer\EntityFramework\Common_Model.tt' to package as 'content\DataLayer\EntityFramework\Common_Model.tt'
解决方案
方法1:修改项目文件(最可靠)
直接在Company_PR.vbproj中配置,让TT文件不参与NuGet打包:
找到项目中TT文件对应的节点,将其标记为<None>并添加<Pack>False</Pack>属性:
<ItemGroup> <None Include="DataLayer\EntityFramework\Common_Model.Context.tt"> <Generator>TextTemplatingFileGenerator</Generator> <LastGenOutput>Common_Model.Context.vb</LastGenOutput> <Pack>False</Pack> </None> <None Include="DataLayer\EntityFramework\Common_Model.tt"> <Generator>TextTemplatingFileGenerator</Generator> <LastGenOutput>Common_Model.vb</LastGenOutput> <Pack>False</Pack> </None> </ItemGroup>
此方法从源头上控制文件是否被打包,完全避免NuGet自动拾取TT文件。
方法2:使用精确的通配符排除路径
修改-Exclude参数,用递归通配符匹配所有子目录下的TT文件,并添加双引号避免shell解析问题:
nuget.exe pack Company_PR_WS\Company_PR.vbproj -version 1.0.26 -Verbosity detailed -Exclude "**\*.tt" -OutputDirectory D:\a\8\a -Properties Configuration=release -NonInteractive
**\*.tt表示匹配项目目录下所有层级的TT文件,比单独的*.tt更精准。
方法3:使用Nuspec文件(灵活可控)
- 生成项目对应的nuspec文件:
nuget spec Company_PR_WS\Company_PR.vbproj
- 在生成的
Company_PR.nuspec中添加files节点,明确包含需要的文件并排除TT文件:
<files> <!-- 包含编译后的DLL --> <file src="bin\release\*.dll" target="lib\net481" /> <!-- 排除所有TT文件 --> <exclude src="**\*.tt" /> </files>
- 使用nuspec文件进行打包:
nuget.exe pack Company_PR_WS\Company_PR.nuspec -version 1.0.26 -Verbosity detailed -OutputDirectory D:\a\8\a -Properties Configuration=release -NonInteractive
验证方法
打包完成后,用NuGet Package Explorer打开生成的.nupkg文件,确认content目录下已无*.tt文件,再将包引用到测试项目中验证构建是否正常。
内容的提问来源于stack exchange,提问作者B-M
相关产品推荐
相关产品推荐

