首页 > 解决方案 > 将 .dll 引用添加到构建之前生成的 nuget

问题描述

我正在使用 nuget 部署一个 MsBuild 任务,它会在每次构建之前生成一个 .dll。

我无法在References消费者的 Visual Studio 项目中获取节点中引用的生成的 dll。

我也在使用 MSBuild 来构建.nupkg文件。build/生成和编译工作正常,我在目录中部署以下目标

    <Project> 
        <!-- this will automatically run before the 'Build' target -->
        <Target Name="GenerateAndBuild"  BeforeTargets="Build">
             <!--the deployed task that generates the code-->
             <Utils.CreateUtilResourceTask/>

             <ItemGroup>
               <CompileGeneratedSources Include="GeneratedClass.cs" />
             </ItemGroup>
             <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />

            <Csc Sources="@(CompileGeneratedSources )" OutputAssembly="$(OutputPath)Util.dll" TargetType="library" EmitDebugInformation="true" />
            <Delete Files="@(CompileGeneratedSources )" />
        </Target>
    </Project>

这会util.dll在项目输出文件夹中生成,但我无法在使用项目中引用它。我认为这将在.csproj文件中起作用:

  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;net46</TargetFrameworks>
    <BuildOutputTargetFolder>tasks</BuildOutputTargetFolder>
    <VersionPrefix>0.1.1</VersionPrefix>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageOutputPath>$(MSBuildThisFileDirectory)</PackageOutputPath>

    <PackageId>BuildUtil</PackageId>
    <!-- that does not add a refenrece in consuming projects -->
    <references>
      <reference file="$(OutputPath)Util.dll"/>
    </references>

    <files>
      <file src="$(OutputPath)Util.dll"/>
    </files>
  </PropertyGroup>

也许有人对此有暗示?

标签: msbuildnuget

解决方案


您需要在ItemGroup而不是 PropertyGroup 中声明程序集引用。例如:

<ItemGroup>
   <Reference Include="$(OutputPath)Util.dll" />
</ItemGroup>

推荐阅读