Max Polkovnik
Max Polkovnik

Reputation: 319

Nuget does not import .targets

I want to add my .targets file into project via nuget. I have next .nuspec file:

<package >
  <metadata>
  ...
  </metadata>
  <files>
    <file src="..\..\Rcs\Rcs\bin\Release\Rcs.targets" target="build\Rcs.targets" />
    <file src="..\..\Rcs\Rcs\bin\Release\Rcs.dll" target="lib\Xamarin.iOS10\Rcs.dll" />     


    <file src="..\..\Rcs\Rcs\bin\Release\*.dll" />
    <file src="..\..\Rcs\Rcs\bin\Release\*.config" />

  </files>
</package>

And .targets file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
        <RcsDirectory Condition="$(RcsDirectory) == '' Or $(RcsDirectory) == '*Undefined*'">$(MSBuildThisFileDirectory)..\..\</RcsDirectory>
  </PropertyGroup>

  <UsingTask
    TaskName="Rcs.RcsBuildTask"
    AssemblyFile="$(RcsDirectory)Rcs.dll" />

  <Target AfterTargets="Build" Name="RcsBuildTask">
    <RcsBuildTask
      ProjectPath="$(MSBuildProjectFullPath)"
      RootNamespace="$(RootNamespace)"
    />
  </Target>
</Project>

New doesn't added to project after installing this nuget. Any help will be appreciated

Upvotes: 1

Views: 3028

Answers (2)

Andrew Dennison
Andrew Dennison

Reputation: 1089

I found that by using NuGet Package Explorer that the targets file had to be named correctly. By default, NPE uses .targtets. It appears that other names may not work.

My Id is "Handy.TargetFrameworkVersionCumulativePreprocessorSymbols", but I had named my target file "HandyTargetFrameworkVersionCumulativePreprocessorSymbols.targets".

Once I used "Handy.TargetFrameworkVersionCumulativePreprocessorSymbols" everywhere it just worked as expected.

Upvotes: 0

J&#233;r&#244;me MEVEL
J&#233;r&#244;me MEVEL

Reputation: 7814

Are you sure the .targets file is really not imported into your Nuget package?

Along with a .nuspec file you should use the Nuget CLI to pack your files. Don't use the dotnet CLI

Once you created your .nupkg file using the nuget pack command, you can open your package using Nuget Package Explorer or simply WinRar (I believe this should work too with 7-zip).

At this point if you see your .targets file or your Rcs.dll are missing it means you have an issue with your .nuspec file. Ensure your .nuspec file has the name ProjectName.nuspec

OR

Specify the .nuspec file in your .csproj

<PropertyGroup>
  [...]
  <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  <NuspecFile>ProjectName.nuspec</NuspecFile>
  [...]
</PropertyGroup>

If your .targets file is in your .nupkg then it means its execution is simply doing not what you expect.

When you install a Nuget package, the files are stored to the Nuget cache. On Windows this is located at %userprofile%\.nuget\packages. Your .targets file and your Rcs.dll should be there too

First of all you can include this in your .targets file

<Target Name="TestMessage" AfterTargets="Build" >
    <Message Text="***********************************************************" Importance="high"/>
    <Message Text="$(MSBuildThisFileDirectory)" Importance="high"/>
    <Message Text="***********************************************************" Importance="high"/>
</Target>

When building your project, you will see the current folder is actually your cache (not your project's directory)

If I understand, what you want is to copy your Rcs.dll into your bin folder. To achieve that you can write a similar task in your .targets file

<ItemGroup>
    <Content Include="$(MSBuildThisFileDirectory)\..\lib\Xamarin.iOS10\Rcs.dll">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

This is a normal behavior that the .targets file is not copied to your project referencing your Nuget package. The .targets file should stay in the Nuget cache, but from there, execute a MsBuild command to copy the Rcs.dll file.

I hope you understand my answer and it solves your problem.

Upvotes: 4

Related Questions