MSBuild copy dlls to bin folder, not following the path - c#

I am using csproj file to bundle Chrome windows edition into our ASP.NET Core app. I place all the needed files into $(ProjectDir)\chrome-win and use below XML to copy the files in csproj
<None Update="chrome-win\**" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest" LinkBase="chrome-win\" />
What is strange is that when I publish the project using the built in folder publish profile, all the *.dll files gets copied to bin\chrome-win\ and other files are in chrome-win\. This is so frustrating, how can I tell stupid MSBuild / Visual Studio to not to do this? When I build it, the behavior is even stranger, the *.dll files gets copied twice, once to chrome-win folder, also gets copied to bin folder.
I am using the latest VS 2019 and MSBuild

I believe you would want to use the $(BaseOutputPath) property for your copy so that everything writes into the same folder. So it would look like this.
<None Update="chrome-win\**" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest" LinkBase="$(BaseOutputPath)\chrome-win\" />
EDIT1:
If you want all the contents of the bin folder moved to \bin\chrome-win\ you could use the CopyTask
<Copy SourceFiles="$(BaseOutputPath)\*" DestinationFiles="$(BaseOutputPath)\" AfterTargets="AfterBuild" />
You might need to tweak this a bit but it should do what you need.

Related

How do I include .deps and .runtimeconfig files as project dependencies?

Visual Studio creates two files along with the .exe for my project that are required to run the exe: a deps.json and a runtimeconfig.json. A second project in my solution has the first project as a project reference, but those two files aren't being copied to my second project's output directory. How can I tell Visual Studio that it should copy these files into the output directory of the second project, because the referenced project depends on them?
Output directory of my first project:
Foo.exe
Foo.deps.json
Foo.runtimeconfig.json
Output directory of my second project:
Bar.exe
Foo.exe
Should contain deps and runtimeconfig files, but does not
The solution I found is to manually edit my .csproj file to add the following target:
<Target Name="AddRuntimeDependenciesToContent"
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'"
BeforeTargets="GetCopyToOutputDirectoryItems"
DependsOnTargets="GenerateBuildDependencyFile;
GenerateBuildRuntimeConfigurationFiles">
<ItemGroup>
<ContentWithTargetPath Include="$(ProjectDepsFilePath)"
Condition="'$(GenerateDependencyFile)' == 'true'"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(ProjectDepsFileName)" />
<ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)"
Condition="'$(GenerateRuntimeConfigurationFiles)' == 'true'"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(ProjectRuntimeConfigFileName)" />
</ItemGroup>
</Target>
This solution came from https://github.com/dotnet/sdk/issues/1675#issuecomment-658779827.
There were other somewhat similar solutions posted in that thread, but this is the only one that worked for me. The others would either not consistently copy the files to my second project, or cause the first project to fail to build due to attempting to access a file that didn't yet exist. The key difference with this one is the inclusion of the correct "BeforeTargets" property (and possibly also "DependsOnTargets"), controlling at which point in the build process the files are included.

How to have an optional file that gets copied to the output directory in VS 2017?

On a large (team and code) project, we have set up the various App.configs and Web.configs to reference an (optional) local.config file so that developers can override some things when running locally. local.config is listed in .getignore so it's ignored and not included in commits. This works very well to support local configuration overrides.
However, for console apps, local.config isn't copied to the output directory by default. I can set its properties in VS so that it's copied to the output - it gets listed in the .csproj file like this:
<None Include="local.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
This works fine in VS, even when a particular developer does not have a local.config file at all. However, it fails in the VSTS build jobs because apparently msbuild doesn't like it when a listed file is absent.
Is there any way to configure the project or msbuild so that it will tolerate/ignore the missing (optional) file instead of failing the builds?
This can be done by using an msbuild condition:
<None Include="local.config" Condition="Exists('local.config')">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

VS 2015 project not rebuilt when I delete a file

I have a config file in my C# project that is excluded from source control (it's local to each developer). However, if the file doesn't exist I want it to be copied from the corresponding ".template" file (which is in source control). To do this, I added an MSBuild item type and target, like this:
<ItemGroup>
<AvailableItemName Include="LocalConfigTemplate" />
</ItemGroup>
<Target Name="CopyFromTemplateIfNeeded" BeforeTargets="BeforeBuild" Inputs="#(LocalConfigTemplate)" Outputs="#(LocalConfigTemplate->'%(FileName)')">
<Copy Condition="!Exists(%(LocalConfigTemplate.Filename))" SourceFiles="#(LocalConfigTemplate)" DestinationFiles="#(LocalConfigTemplate->'%(FileName)')" />
</Target>
(Based on How to hide files generated by custom tool in Visual Studio) This is in a separate file, included from the project file (before Microsoft.CSharp.targets, if that matters).
It works, but when I delete the target file (local.config) and build the project VS thinks it's "up to date" and does not build. How do I get it to detect that the output file is missing and build in that case?
Try doing a Clean before the rebuild, maybe Visual Studio is picking the file up from the bin directory.

ensuring c# project dependencies are copied to output directory in Visual Studio and TFS

I am working on a project which has many dependencies which are developed on a separate team from me. We use TFS 2010. Many of my applications depend on libraries and xml files which are under active development, so I want to keep them up to date. I also don't want to create separate copies of the dll's and xml files for each application/project, but rather source them from their respective locations within the same source control repository. This should be possible using a relative path.
I tried putting the following in my .csproj file
<ItemGroup>
<Dependencies Include="..\..\Driver\Driver.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Dependencies>
</ItemGroup>
this doesn't work, either on my workstation or on the build server, however, the files show up as dependencies in the Solution Explorer, and it allows me to change the copy to output property and shows the full path the to files, which is valid.
Another thing I tried was just running xcopy as a pre-build event, which works on my local machine but does NOT copy the files to the output/TFS drop folder, so it isn't picking it up as a dependency.
Try using the Private Element instead, set to True.
<ItemGroup>
<Dependencies Include="..\..\Driver\Driver.dll">
<Private>True</Private>
</Dependencies>
</ItemGroup>
See http://msdn.microsoft.com/en-us/library/bb629388.aspx

Remove folder from MSBuild

I'm trying to remove a folder (well, actually I thought it was easier to remove the files inside it) from a build using MSBuild scripts.
I thought the way is removing them from the copy task itself, but what I was thinking it was going to see quite straightforward it's not working (I'm sure because I don't have much idea of this stuff, just read documentation yesterday and today). Here is how I'm trying to remove the folder (or the files inside it) ..App_Data/Email Templates with this space (does the space something to do?).
<ItemGroup>
<SourceRootFiles Include="$(BuildFolder)/**/*.*" Exclude="$(BuildFolder)/**/App_Data/Email Templates/*.*">
</SourceRootFiles>
</ItemGroup>
<Target Name="PrepareBuild" DependsOnTargets="CleanUp">
<Message Text="Preparing the build directory : $(LocalBuild)"></Message>
<MakeDir Directories="$(LocalBuild)" />
<Copy SourceFiles="#(SourceRootFiles)" DestinationFolder="$(LocalBuild)\%(RecursiveDir)">
</Copy>
<Exec Command="FOR /r "$(LocalBuild)" %%f IN (.svn) DO RD /s /q "%%f"" IgnoreExitCode="true" />
</Target>
<Target Name="Build" DependsOnTargets="PrepareBuild">
<MSBuild Projects="$(LocalBuild)\Getting.sln" />
</Target>
Update.
Jenkins is raising this error
:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error : Copying file App_Data\Email Templates\BuyerRegistrationComplete.htm to obj\Latest\Package\PackageTmp\App_Data\Email Templates\BuyerRegistrationComplete.htm failed. Could not find a part of the path 'App_Data\Email Templates\BuyerRegistrationComplete.htm'. [C:\Builds\Getting\Latest\Build\Web\UI\UI.csproj]
Dont' really know if it's exluding it or not
On githup is a project named MsBuildTasks that contains all kind of custom-actions that you can easily integrate in your project
https://github.com/loresoft/msbuildtasks
From your update tells a "new" story.
In your project-file you reference files in the App_Data folder which WebDeployment wants to copy to deployment. Removing App_Data results in missing files and thus failure.
Either move those files to another location in your project or remove the references to those files.
My suggestion would be to make a separate folder for the templates, App_Data has a different purpose.

Categories