ʃʈɑɲ
ʃʈɑɲ

Reputation: 2684

Custom nuget package help needed

Hi I would like to know how I would go about this scenario:

I followed this answer but I never got the targets file integrated into the consuming csproj file.
The version is set to a fixed number there?

This is my nuspec file contents:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>My.Assembly</id>
    <version>1.0.0.0</version>
    <title>A Library</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <!-- <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl> -->
    <!-- <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl> -->
    <!-- <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl> -->
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>A Library</description>
    <releaseNotes>Some release notes.</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <!-- <tags>Tag1 Tag2</tags> -->
  </metadata>
      <files>
        <file src="bin\**" target="lib" />
        <file src="*.targets" target="build" />
    </files>
</package>

What is advised to do here?

(using NuGet Version: 4.4.1.4656)

Upvotes: 2

Views: 226

Answers (1)

Leo Liu
Leo Liu

Reputation: 76760

but I never got the targets file integrated into the consuming csproj file.

Not sure why you could not got the targets file integrated into the consuming csproj file, if .targets file is under your build folder in your nuget package, then when you add this nuget package to the project, the .targets file will be integrated into the consuming csproj file.

The first thing is make sure the nuget package is correct:

This is my .nuspec file:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>My.Assembly</id>
    <version>1.0.0</version>
    <authors>Admin</authors>
    <owners>Admin</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
  <files>
    <file src="bin\**" target="lib" />
    <file src="*.targets" target="build" />
  </files>
</package>

You should make sure that .targets file is in the same folder as your .nuspec file, in this case, this command src="*.targets" can include the .targets file into the nuget package.

Note: The name of the .targets file must match the ID of your package, for your situation, the name of .targets must be My.Assembly.targets.

After pack the .nuspec, you will get following structure nuget package:

enter image description here

Then install this package to your project, after installation, unload the project, edit the project file, in the .csproj file, following code would be imported:

<Import Project="..\packages\My.Assembly.1.0.0\build\My.Assembly.targets" Condition="Exists('..\packages\My.Assembly.1.0.0\build\My.Assembly.targets')" />

Now, the .targets file integrate into the consuming csproj file.

In addition, there is an issue for this solution, because the dlls in lib folder will be automatically added as references creating the following in the project file:

<Reference Include="My.Assembly, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
  <HintPath>..\packages\My.Assembly.1.0.0\lib\Release\My.Assembly.dll</HintPath>
</Reference>

So we could not set dll file in the lib folder, we could set the dll file in to the tools folder, then add a target in the .targets file to copy the dll file from tools folder to lib folder before build the project:

  <files>
    <file src="bin\Debug\My.Assembly.dll" target="lib\net\$(Configuration)\My.Assembly.dll" />  
    <file src="bin\**" target="tools" />
    <file src="*.targets" target="build" />
  </files>

For the detailed info you can refer to the Arie R`s answer in the thread which you are referencing:

How to create a nuget package with both release and debug dll's using nuget package explorer?

Hope this helps.

Upvotes: 1

Related Questions