Copy assemblies to build directory using MSBuild task - msbuild

I'm new to MSBuild and was wondering how could you copy all files in your lib directory to your build directory? if you could point to some online resource that would be fine.

You can use a copy task.
Refer to this URL(definition and example): http://msdn.microsoft.com/en-us/library/3e54c37h.aspx

visual studio uses ItemGroup/Reference and ItemGroup/Content for this
<ItemGroup>
<Reference Include="nunit.framework,...">
<HintPath>..\..\..\lib\nunit.framework.dll</HintPath>
</Reference>
<Content Include="TestData\nwind.mdb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

Related

What can I do about my build error in Visual Studio 2019 .net core? cannot find part of path \obj\Debug\net48\Package\PackageTmp

I am so confused, this is my error:
1>obj\Debug\net48\Package\PackageTmp.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0\Web\Microsoft.Web.Publishing.targets(3000,5): error : Copying file obj\Debug\net48\NuGet\DD3A9BE7C0EDA549CD5B7B690E0621F0E2C932EC\Glass.Mapper.Sc.100\5.8.173\App_Config/Include/Glass/Glass.Mapper.Sc.Start.config to obj\Debug\net48\Package\PackageTmp\obj\Debug\net48\NuGet\DD3A9BE7C0EDA549CD5B7B690E0621F0E2C932EC\Glass.Mapper.Sc.100\5.8.173\App_Config/Include/Glass/Glass.Mapper.Sc.Start.config failed. Could not find a part of the path 'obj\Debug\net48\Package\PackageTmp\obj\Debug\net48\NuGet\DD3A9BE7C0EDA549CD5B7B690E0621F0E2C932EC\Glass.Mapper.Sc.100\5.8.173\App_Config/Include/Glass/Glass.Mapper.Sc.Start.config'.
1>Done building project "some.project.ORM.csproj" -- FAILED.
I have tried to delete the obj\Debug folder in the file system as well as the solution, as some have suggested. I have unloaded the project to review the .csproj file, I didn't see anything strange. I tried removing the Nuget Package and Reinstalling it. I just am not certain.
any help would be welcomed.
Note: I checked the directories do exist and they do. I even deleted the /obj/Debug folders from the solution as well as the source, as suggested, nothing seems to be working.
here is the .csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<RootNamespace>some.project.ORM</RootNamespace>
<AssemblyName>some.project.ORM</AssemblyName>
<PublishTargetType>website</PublishTargetType>
</PropertyGroup>
<Import Project="$(SolutionDir)\_Build\props\_PublishTargetType.props" />
<ItemGroup>
<PackageReference Include="Glass.Mapper.Sc.100" Version="5.8.173" />
</ItemGroup>
<ItemGroup>
<Content Update="App_Config\Include\Foundation\Glass\Glass.Mapper.Sc.Start.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
this issue was because the file that I was copying to was over the 260 limit in VS. My path length was 261. We copied the solution to a shorter path and the build worked fine. thanks for your help.

NuGet Package Packing - Is it possible to copy files to a custom directory?

I'm trying to package a few files into a NuGet package, but the issue is that all of the files are sent to the "content" folder within the NuGet package by default when packaged. Normally this is okay, but for the JSON files I have in "ABCJsons" I'd like them to be sent to "content/NewFolderName".
In my example below, the first block is my AbcToolTester, which has all of its project files files being successfully sent to the content directory in the NuGet package. The second block, is where I attempted to copy all the json files with ABCLibrary (ABCLibrary has subfolders where the actual Jsons are located) to the destination folder "ABCJsons". I thought this would do the trick, but unfortunately the ABCJson files just get sent to the content folder along with all the other files.
<ItemGroup>
<Content Include="..\AbcToolTester\bin\Debug\netcoreapp3.1\**" Exclude="..\AbcToolTester\bin\Debug\netcoreapp3.1\*.pdb">
<IncludeInPackage>true</IncludeInPackage>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<ItemGroup>
<Target Name="CopyABCLibrary" AfterTargets="AfterBuild">
<ItemGroup>
<ABCJsonsInclude="..\..\tests\ABCLibrary\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(ABCJsons)" DestinationFolder="$(TargetDir)\ABCJsons" SkipUnchangedFiles="true" />
</Target>
It's hard to tell if the NuGet package you are creating is actually dependent on AbcToolTester project because there are easier ways to package that. That's another question though.
For your actual issue, you can simplify the copying process while also telling it where to pack the files. Replace your CopyABCLibrary target with this:
<ItemGroup>
<Content Include="..\..\tests\ABCLibrary\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>ABCJsons\%(RecursiveDir)</PackagePath>
<!-- This line hides the items from showing in the solution explorer -->
<Visible>false</Visible>
</Content>
</ItemGroup>
This will put all those files into the root of the nuget package into the ABCJsons folder and preserve the directory structure. Change the path accordingly to put it somewhere else.

Linked files location on .Net core in Debug vs Publish

I have a shared.{Environment}.json file that is added as linked files in several .Net core 2.1 projects. When project is build or published file gets copied to output directory, in case of release its fine but on debug it doesn't work as when project run it looks up for that file in project directory not in output directory.
Whats the proper way to solve this issue for both debug and publish?
For linked files, it will not exist under the project directory.
For a workaround, you could try to copy the file with task in csproj like below:
<ItemGroup>
<Content Include="..\MVCPro\shared.{Environment}.json">
<Link>shared.{Environment}.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
<Copy SourceFiles="..\MVCPro\shared.{Environment}.json" DestinationFolder=".\" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
</Target>

How to include a local DLL reference in to a nuget package when calling msbuild pack?

We have several projects that need to include a few static DLL. Therefore the project files include code like this:
<ItemGroup>
<Reference Include="..\_Solutions\dependencies\abc123.dll" />
<Reference Include="..\_Solutions\dependencies\def456.dll" />
<Reference Include="System.Web" />
</ItemGroup>
Expected:
We expected that the two dlls; abc123.dll and def456.dll would befound in the nupkg file.
Actual:
However, the nupkg doesn't include the abc123.dll nor the def456.dll files.
One can always include custom content in the nuget-package. Like this:
<ItemGroup>
<Content Include="$(OutputPath)\ReferencedLib.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
If you target multiple frameworks:
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.6</TargetFrameworks>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);IncludeReferencedProjectInPackage</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>
<Target Name="IncludeReferencedProjectInPackage" Condition="'$(IncludeBuildOutput)' != 'false'">
<ItemGroup>
<TfmSpecificPackageFile Include="$(OutputPath)\ReferencedLib.dll" PackagePath="lib/$(TargetFramework)" />
</ItemGroup>
</Target>
How to include a local DLL reference in to a nuget package when calling msbuild pack?
According the issue on the GitHub, this is currently not directly supported by NuGet.
The workaround I suggest is using the .nuspec file:
NuGet allows you to disable the automatic generation of the resulting
.nuspec file and automatic collection of files by setting the
property in your project, along with a
property that allows you to pass replacement tokens for parsing the
.nuspec file.
See Martin`s answer for details.

Add folder from solution to C# .NET project Content output

I want to include files from the solution directory, regardless of where the files are. I added this to my .csproj file:
<ItemGroup>
<Content Include="$(SolutionDir)\web\**\*" />
</ItemGroup>
But the files do not appear in the solution explorer. And they do not appear in the publish output either.
How to do this correctly?
You still need to tell MSBuild to copy the content, for example
<ItemGroup>
<Content Include="$(SolutionDir)\web\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>