简体   繁体   English

无法从MSBuild引用Nuget包的“tools”文件夹中的可执行文件

[英]Can't reference an executable in the “tools” folder of a Nuget package from MSBuild

I have a C# console app project which I am trying to turn into a Nuget package so that multiple projects within my team can call the executable from this project in an AfterResolveReferences step within the .csproj file. 我有一个C#控制台应用程序项目,我试图将其转换为Nuget包,以便我的团队中的多个项目可以在.csproj文件中的AfterResolveReferences步骤中调用此项目中的可执行文件。

I've created the Nuget package just fine with a .nuspec file that looks something like this: 我已经使用.nuspec文件创建了Nuget包,看起来像这样:

<package ...>
  <metadata>
    ...
  </metadata>
  <files>
    <file src="bin\Release\*.*" target="tools" />
  </files>
</package>

This works and creates a Nuget package which contains my executable in the "tools" directory. 这将工作并创建一个Nuget包,其中包含我在“tools”目录中的可执行文件。 The problem is the the containing folder includes the version number of the Nuget package which will change frequently. 问题是包含文件夹包含将经常更改的Nuget包的版本号。 I'm trying to reference it like this in the .csproj file: 我试图在.csproj文件中像这样引用它:

<Target Name="AfterResolveReferences">
  <Exec Command="$(SolutionDir)packages\PackageName.1.2.3\tools\AssemblyName.exe" />
</Target>

When I include PackageName.1.2.3 in the path, it works as expected but this is obviously a very brittle solution. 当我在路径中包含PackageName.1.2.3时,它按预期工作,但这显然是一个非常脆弱的解决方案。 When I just use "AssemblyName.exe" I get "The command AssemblyName.exe exited with code 9009". 当我只使用“AssemblyName.exe”时,我得到“命令AssemblyName.exe退出代码9009”。

There's obviously a simple standard for doing this kind of thing which I'm not familiar with - MSBuild and Nuget aren't my strongest suits, so I'd be very grateful for any advice. 显然有一个简单的标准来做这种我不熟悉的事情 - MSBuild和Nuget不是我最强的套装,所以我非常感谢任何建议。

What I'm actually trying to achieve here is to create a TypeScript file containing interfaces derived from C# classes defined in my model project using the TypeLite.Lib package. 我实际上想要实现的是创建一个TypeScript文件,其中包含使用TypeLite.Lib包在我的模型项目中定义的C#类派生的接口。 The TypeScript file must be created before the web project is built, as TypeScript code in the web project depends on the interfaces contained in this output. 必须在构建Web项目之前创建TypeScript文件,因为Web项目中的TypeScript代码取决于此输出中包含的接口。 I'm open to suggestions of more elegant ways to solve this problem but I would still like to know how to solve the referencing problem anyway. 我愿意接受更优雅的方法来解决这个问题,但我仍然想知道如何解决引用问题。

The idea is that the NuGet package should be self-contained. 我们的想法是NuGet包应该是自包含的。 That is, users should not need to add anything to the project file when using a nuget package. 也就是说,在使用nuget包时,用户不需要向项目文件添加任何内容。

NuGet packages can also contain build logic - if you put a PackageName.targets file into the a build directory, it will be automatically included into the project referencing the NuGet package. NuGet包还可以包含构建逻辑 - 如果将PackageName.targets文件放入build目录,它将自动包含在引用NuGet包的项目中。 From there, you can define targets and would typically reference a tool by using $(MSBuildThisFileDirectory)..\\tools\\MyTool.exe . 从那里,您可以定义目标,并通常使用$(MSBuildThisFileDirectory)..\\tools\\MyTool.exe

This is important because the packages directory is only used for "classic" NuGet references via packages.config and not for the new-style PackageReference way of referencing NuGet packages, where all projects/solution share a user-level global package cache (no solution-local copies). 这很重要,因为packages目录仅用于通过packages.config进行“经典”NuGet引用,而不是用于引用NuGet包的新式PackageReference方式,其中所有项目/解决方案共享用户级全局包缓存(无解决方案) - 本地副本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM