关于msbuild:将.dll引用添加到生成之前生成的nuget

Add .dll reference to nuget that is generated before build

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

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

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

中部署以下目标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    <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文件中运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  <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>

也许有人对此有暗示吗?


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

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