I have a C# project that uses the Project Dependencies in a sln file to make sure that the build order is correct.
So I have in my sln file that ProjectB depends on ProjectA.
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectB", "ProjectB.csproj", "{E24EAC46-1563-4E73-9411-3F9D2645F77C}"
ProjectSection(ProjectDependencies) = postProject
{4A7D6720-4AA1-4F0B-A796-A0436DB3D7D7} = {4A7D6720-4AA1-4F0B-A796-A0436DB3D7D7}
EndProjectSection
EndProject
ProjectA has some content that is set to CopyIfNewer.
When I build this with Visual studio, ProjectA goes to its own bin folder and ProjectB goes to its own bin folder.
But when I build it with MSBuild, the content of ProjectA somehow appears in the output folder in ProjectB as well!
The build log shows that [ProjectB.csproj] _CopyOutOfDateSourceItemsToOutputDirectory copies the files over.
My question is: How can I tell MSBuild that the files do not belong to that project and don't have to be copied?
As a workaround I added ProjectA as a ProjectReference with <Private>False</Private> and that seems to work, but it is not my desired solution.
I think that the workaround you have described is currently the best solution you can find today.
As a workaround I added ProjectA as a ProjectReference with False and that seems to work, but it is not my desired solution.
I Solve this trouble overriding MSBuild Task in ProjectB .csproj file.
(I have only dependency on ProjectA in .sln file, not adding project as ProjectReference to ProjectB)
Add this targets to your .csproj file:
<Project>
.....
<Target Name="_CopyOutOfDateSourceItemsToOutputDirectory" Condition=" '#(_SourceItemsToCopyToOutputDirectory)' != '' " Inputs="#(_SourceItemsToCopyToOutputDirectory)" Outputs="#(_SourceItemsToCopyToOutputDirectory->'$(OutDir)%(TargetPath)')">
<Message Importance="Normal" Text="$(MSBuildProjectName) Skip copy _CopyOutOfDateSourceItemsToOutputDirectory" />
</Target>
<Target Name="_CopyOutOfDateSourceItemsToOutputDirectoryAlways" Condition=" '#(_SourceItemsToCopyToOutputDirectoryAlways)' != '' ">
<Message Importance="Normal" Text="$(MSBuildProjectName) Skip copy _CopyOutOfDateSourceItemsToOutputDirectoryAlways" />
</Target>
</Project>
For myself files, needed to copy to output directory i do it by XCOPY in postbuild like:
<PropertyGroup>
<PostBuildEvent>xcopy "$(ProjectDir)SomeSubFolder\SomeContentFile.cfs" "$(TargetDir)SomeSubFolder\" /Y /F</PostBuildEvent>
</PropertyGroup>
Maybe there are has smart variant of overriding _CopyOutOfDateSourceItemsToOutputDirectory* tasks like this. But my variant satisfied me now. Because i dont have dependency of other content files in my project.
Related
I have a Visual Studio solution which has several projects. One of them, which is called Extensions is supposed to build a Nuget package. Problem is that if I use MSBuild to build the solution (by executing msbuild from the command prompt), I get the following error for the Nuget package build task:
"C:\git\repo\dirs.proj" (default target) (1:7) ->
"C:\git\repo\sources\dirs.proj" (default target) (2:5) ->
"C:\git\repo\sources\dev\Sdk\CompanyName.ProductName.Extensions.csproj" (default target) (7:6) ->
(GenerateNuspec target) ->
C:\Program Files\dotnet\sdk\5.0.408\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(221,5): error : Could not find a part of the path 'C:\git\ess\target\distrib\Debug\Amd64\CompanyName.ProductName.Extensions'. [C:\git\ess\sources\dev\Sdk\CompanyName.ProductName.Extensions.csproj]
Here is the CSPROJ file I have for this project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>CompanyName.ProductName.Extensions</AssemblyName>
<RootNamespace>CompanyName.ProductName.Extensions</RootNamespace>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeCoreAssets</TargetsForTfmSpecificBuildOutput>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<NuspecFile>CompanyName.ProductName.Extensions.nuspec</NuspecFile>
<PackageOutputPath>$(DistribRoot)\$(Configuration)\$(Platform)\$(MSBuildProjectName)</PackageOutputPath>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SkipAssemblyComVisible>true</SkipAssemblyComVisible>
<IncludeBuildOutput>false</IncludeBuildOutput>
<OutputPath>$(DistribRoot)\$(Configuration)\$(Platform)\$(MSBuildProjectName)</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<NuspecBasePath>$(OutputPath)</NuspecBasePath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core\CompanyName.ProductName.Core.csproj" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<Target DependsOnTargets="ResolveReferences" Name="IncludeCoreAssets">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
</Project>
I personally think the issue is that for some reason when MSbuild is trying to create the Nuget package, it's trying to find the Extensions project DLLs in this path: \target\distrib\Debug\Amd64\CompanyName.ProductName.Extensions\, even though it actually built and stored the said binaries in this path earlier: \target\distrib\CompanyName.ProductName.Extensions\. <--- This is something I checked manually myself by examining the 'target' folder after running msbuild.
Visual Studio however doesn't have this issue. When I build this solution within Visual Studio, it stores the binaries for this Extensions project in \target\distrib\Debug\AnyCPU\CompanyName.ProductName.Extensions\ folder, which I think matches the pattern defined in the CSPROJ file:
<OutputPath>$(DistribRoot)\$(Configuration)\$(Platform)\$(MSBuildProjectName)</OutputPath>
Visual Studio also stored the Nuget package in this folder, which is also correct as per the <PackageOutputPath> spec mentioned in the CSPROJ file.
So can someone suggest why MSbuild is storing the built DLLs of this project in \target\distrib\CompanyName.ProductName.Extensions\, but is trying to find them in \target\distrib\Debug\Amd64\CompanyName.ProductName.Extensions\ ?
I think the issue is that for some reason, MSbuild isn't adhering to the <OutputPath> spec mentioned above when it's storing the built DLLs for this project.
BACKGROUND
I have created an ASP.NET webforms (4.6) website that has some 3rd party components in it. In order for this to work I have created a SharedResources folder that contains the DLLs needed. I've added a reference and those DLLs get copied to the BIN when publishing - GREAT. BUT there are some other DLLs needed that cannot be referenced as they are not valid assemblies or COM components.
MY PROBLEM
How can I add to the build, dll's that are in a folder (SharedResources\3rdParty) so that when I click 'Publish' it dumps all those dll's into the bin folder of the website.
WORKAROUND
(trying to avoid this)
I have set the dll Build action to "Content - always copy" and this gives me a folder of DLLs in ~\SharedResources\3rdParty\ and then the application complains of missing dll's so I copy them to the bin manually - that can't be right!
Thanks
Right click your project go to properties and, in the build events tab paste this into your Pre-build event command line: copy $(ProjectDir)SharedResources\3rdParty*.dll $(ProjectDir)$(OutDir)
This will not work when you publish the website. You will have to create your own targets in the project file or a separate targets file and call as follows -
Add following -
<ItemGroup>
<ThirdPartyDllDependencies Include="$(SolutionDir)..\..\Lib\ThirdPartyDll\*.dll" SkipUnchangedFiles="true" />
</ItemGroup>
<Target Name="PublishThirdPartyDllDependencies">
<Copy SourceFiles="#(ThirdPartyDllDependencies)" DestinationFolder="$(OutDir)\" />
<Copy SourceFiles="#(ThirdPartyDllDependencies)" DestinationFolder="$(WebProjectOutputDir)\bin\" />
</Target>
<!-- This target is responsible for adding the ThirdPartyDll and dependencies to the bin directory while Publishing the website. -->
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="$(SolutionDir)..\..\Lib\ThirdPartyDll\ThirdPartyDll11\64Bit\*.dll" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>$(WebProjectOutputDir)\bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
<DestinationRelativePath>$(OutDir)\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
I am a beginner with MSBuild. I have a project (A) that depends on another project (B) and it consumes it either from a NuGet server either from a local workspace (I use NuGetReferenceSwitcher for this purpose).
Shortly, here are my requirements:
When consumed from NuGet server, project B has to support both Debug and Release builds. In this way, if we switch to Debug in Visual Studio we would be able to debug also project B when called from A. Right now, the NuGet also gives the Release version.
When switching to local workspace, it should still build properly.
Here is how my local files are located:
\workspace\projectA\projectA.sln
\workspace\projectB\projectB.csproj
The solution I tried is the following:
Inside projectB I created a targets file (located in build\projectB.targets) with the following contents:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<Reference Include="lib1">
<HintPath>"$(MSBuildProjectDirectory)\bin\$platform$\Debug\lib1.dll"</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<Reference Include="lib1">
<HintPath>"$(MSBuildProjectDirectory)\bin\$platform$\Release\lib1.dll"</HintPath>
</Reference>
</ItemGroup>
</Project>
projectB.csproj contains this:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\some_package" Condition="Exists('packages\some_package)" />
projectB.nuspec contains:
<file src="build\**" target="build\net462" />
projectA.csproj (that depends on projectB) contains:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\projectB.*\build\projectB.targets" Condition="Exists('..\packages\projectB.*\build\projectB.targets')" />
<Import Project="..\..\..\..\projectB\projectB.csproj" Condition="!Exists('..\packages\projectB.*\build\projectB.targets')" />
Here are my questions:
With this configuration, when using local workspace for projectB, the compilation of projectA fails because of the relative path of "some_package" which is not find from projectA (that also uses relative path for projectB as you see in point 4). How else could I do reference projectB from projectA so it doesn't fail the compiling?
Why is this code not working to switch from release to debug when using nuget?
Thank you,
Why is this code not working to switch from release to debug when using nuget?
Quite simply, NuGet doesn't have a concept of "Debug" or "Release".
However, there is a way to create debug symbols for NuGet packages and then configure Visual Studio to step through the code of the installed packages.
I'm using the Nuget Package Explorer to create some nuget packages. I've managed to do so just building a project in Release mode in VS and adding both the dll and pdb files to the package.
So far so good, but when I add the package to another project and try to step into the code while debugging, it will step over it instead.
I understand that I need to build and add the Debug dll and pdb to my package if I want to step into the code while debugging. I'm not sure though how to add these to the package I've already create, which already contains the Release dll and pdb file, which are named the same.
Any thoughts?
My thoughts are, NuGet packaging is a lot about conventions.
There is no problem in packaging same namespaces and same names for different platforms (as in lib/net40/mydll.dll, lib/net35/mydll.dll etc in the same package), as NuGet will filter registered dependencies by platform.
Building several versions for the same platform seems unconventional, this discussion is biased towards making a package per build. That doesn't mean you can't do it, but you should first ask yourself if you should.
That said, if your debug and release builds are very different (conditional compiling etc) this might useful though. But how will end-users choose Release or Debug when installing your package?
An idea could be, one version per build configuration. Both can be installed into the project. To do that, either add a targets file to your package or build a powershell install script (unsupported since Nuget v3) that adds conditional references directly in the target project file, if you want something less basic than whatever MsBuild can do for you.
Example of the first tactic: Create a .target file (in your package, create a build folder and then create build\YourLib.targets with the following contents):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<Reference Include="YourLib">
<HintPath>..\packages\YourLib.1.0.0\lib\Debug\YourLib.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<Reference Include="YourLib">
<HintPath>..\packages\YourLib.1.0.0\lib\Release\YourLib.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Providing you created debug and release folders (platform folder is optional), the build output will effectively change depending on configuration - provided packet consumers have conventional configuration names, but you could always extend the condition logic a bit with $(Configuration).Contains etc or just put that in the package readme
Inspired by #Tewr I've found a cumbersome but a working solution.
Create a nuget with the following file structure:
lib\net\$(Configuration)\YourLib.1.0.0.dll <---- put here some dummy file named YourLib.1.0.0.dll
tools\release\YourLib.1.0.0.dll <--- put here the release version
tools\debug\YourLib.1.0.0.dll <--- put here the debug version
build\YourLib.targets
The targets file content:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CopyReferences" BeforeTargets="Build" Condition="Exists('..\packages\YourLib.1.0.0\lib\net\%24(Configuration)')">
<Exec Command="mkdir ..\packages\YourLib.1.0.0\lib\net\Release" />
<Exec Command="mkdir ..\packages\YourLib.1.0.0\lib\net\Debug" />
<Exec Command='copy "..\packages\YourLib.1.0.0\tools\Release\YourLib.1.0.0.dll" "..\packages\YourLib.1.0.0\lib\net\Release"' />
<Exec Command='copy "..\packages\YourLib.1.0.0\tools\Debug\YourLib.1.0.0.dll" "..\packages\YourLib.1.0.0\lib\net\Debug"' />
<Exec Command='rmdir /S /Q "..\packages\YourLib.1.0.0\lib\net\%24(Configuration)"' />
</Target>
The dlls in lib folder will be automatically added as references creating the following in the project file:
<Reference Include="YourLib>
<HintPath>..\packages\YourLib.1.0.0\lib\net\$(Configuration)\YourLib.1.0.0.dll</HintPath>
<Private>True</Private>
</Reference>
Once you build the project for the first time, the target will copy the release and debug version from tools\release and tools\debug folders to lib\net\release and lib\net\debug folders. In the end, it will delete the lib\net\$(Configuration) folder
Enjoy (or not - I personally don't like the solution).
Thanks #Tewr In new nuget format and sdk style csproj format, we can use some constant as $(MSBuildThisFileDirectory) to get the current file path.
The code that uses the version will increase maintenance difficulty. The sdk style csproj format use the new package format that will do not output the package file to package folder.
We can add the targets file to build folder and use $(MSBuildThisFileDirectory) to get the file path.
<ItemGroup Condition="'$(Configuration)' == 'DEBUG'">
<Reference Include="YourLib">
<HintPath>$(MSBuildThisFileDirectory)..\lib\debug\YourLib.dll</HintPath>
</Reference>
</ItemGroup>
See the file
I have a class library that is referenced by many other web application projects. It has many settings in its app.config that I wish to use in all of the referencing web projects. When the class library is built, it copies the app.config to its own bin folder as <assembly.name>.dll.config.
How do I ensure that <assembly.name>.dll.config is copied to the bin folder of each of my referencing web application projects?
Visual Studio / MSBuild does not seem to do this by default.
Changing Copy to Output Directory or Build Action (in any combination) does not work.
SlowCheetah VS extension appears to do what I want as an unintended side-effect of its config transform process, but I want to do this without any 3rd-party extensions.
As an aside: It's impractical to manually put all of this config in each of the web application projects. I can read the file by looking for <assembly.name>.dll.config in the bin folder, and extract the settings from there. I can already do this, so that's not an issue - I just need to ensure the file is going to be there for reading
This can be achieved without 3rd-party tools by adding the following to the class library's project file, assembly.name.csproj:
// Find this <Import> element for Microsoft.CSharp.targets
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
// Add this <ItemGroup> element immediately after
<ItemGroup>
<Content Include="app.config">
<Link>$(TargetName).dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
This causes the app.config to be copied to whichever project is referencing it as <assembly.name>.dll.config. It's good because you only need to configure the one .csproj file and the effect cascades out to all referencing projects.
Visual Studio 2015 and earlier
For Visual Studio 2015, I am using the solution of #theyetiman (see this answer):
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<None Include="app.config">
<Link>$(TargetFileName).config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
(The import of Microsoft.CSharp.targets was already present.)
Visual Studio 2017
During my experiments, Visual Studio 2017 seems to handle configuration files as expected out of the box. No manual modification of the project file is needed.