How to copy contentFiles of NuGet package references via MSBuild Copy task - msbuild

We have several repositories and each one has its own .editorconfig. Obviously, these are not synced, which is why I would like to distribute the .editorconfig from our framework solution (along with other files) via NuGet package to all our repositories/solutions and copy it via a simple Copy build Task to the solution directory.
I attempted to do the following:
Create a project "EditorConfigDistribution", which is supposed to contain the master .editorconfig file.
<Project Sdk="Microsoft.NET.Sdk">
...
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoDefaultExcludes>true</NoDefaultExcludes>
</PropertyGroup>
<ItemGroup>
<Content Include=".editorconfig">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackageCopyToOutput>false</PackageCopyToOutput>
<PackagePath>contentFiles\any\any\content</PackagePath>
</Content>
</ItemGroup>
</Project>
This all works as expected and I do get the desired .editorconfig file in my project from the other solutions and it is referenced as shortcut in a folder content/.editorconfig (see EditorConfigConsumer Project Structure).
The file is only a reference to C:\Users\<user>\.nuget\packages\editorconfigdistribution\1.0.0\contentFiles\any\any\content\.editorconfig.
Now, I want to copy that .editorconfig file via build task:
<Project Sdk="Microsoft.NET.Sdk">
...
<ItemGroup>
<PackageReference Include="EditorConfigDistribution" Version="1.0.0">
</PackageReference>
</ItemGroup>
<Target Name="CopyEditorConfig" BeforeTargets="BeforeBuild">
<ItemGroup>
<EditorConfigFileToCopy Include="$(MSBuildProjectDirectory)\content\.editorconfig" />
</ItemGroup>
<Copy SourceFiles="#(EditorConfigFileToCopy)" DestinationFolder="$(MSBuildProjectDirectory)\.." SkipUnchangedFiles="true" UseHardlinksIfPossible="false" />
</Target>
</Project>
However, I do get the following error:
Error MSB3030: Could not copy the file "C:\Users\weberma9\source\repos\<some_path>\EditorConfigConsumer\content\.editorconfig" because it was not found. (20, 5)
I can understand that the file (since it is a shortcut) cannot be found, but I just cannot figure out a way to reference that shortcut correctly in my build task.
What do I need to change in that line <EditorConfigFileToCopy Include="$(MSBuildProjectDirectory)\content\.editorconfig" />?
Of course, if you have better approaches to my general problem - I'm glad to hear about it.

I was able to find a solution for my problem:
I not only provide the .editorconfig, but also the Copy-Build task via my EditorConfigDistribution project, which looks like this now:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoDefaultExcludes>true</NoDefaultExcludes>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\.editorconfig">
<Link>Rules\.editorconfig</Link>
<Pack>true</Pack>
<PackageCopyToOutput>false</PackageCopyToOutput>
<PackagePath>Rules\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<None Include="build\**">
<Pack>true</Pack>
<PackageCopyToOutput>false</PackageCopyToOutput>
<PackagePath>build\</PackagePath>
</None>
</ItemGroup>
</Project>
The .props file is straightforward and due to the convention that <package_id>.props and <package_id>.target are added to projects that consume the package (see Include MSBuild props and targets in a package), it will always be executed before 'BeforeBuild'.
EditorConfigDistribution.props (placed in build folder):
<Project>
<Target Name="CopyEditorConfig" BeforeTargets="BeforeBuild">
<ItemGroup>
<EditorConfigFilesToCopy Include="$(MSBuildThisFileDirectory)..\Rules\.editorconfig" />
</ItemGroup>
<Copy SourceFiles="#(EditorConfigFilesToCopy)" DestinationFolder="$(SolutionDir).." SkipUnchangedFiles="true" UseHardlinksIfPossible="false" />
</Target>
</Project>

Related

MSBuild well-known item metadata and NuGet pack

I am trying to add custom files to a specific NuGet package (basically I need all output files included in the NuGet package since it serves as a tool for Chocolatey).
After some searching, I found this potential fix:
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetToolsPackageFiles</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="GetToolsPackageFiles">
<ItemGroup>
<BuildOutputInPackage Include="$(OutputPath)\**\*.dll" />
<BuildOutputInPackage Include="$(OutputPath)\**\*.exe" />
</ItemGroup>
</Target>
Unfortunately, this won't work correctly for subdirectories, so I tried this:
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetToolsPackageFiles</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="GetToolsPackageFiles">
<ItemGroup>
<BuildOutputInPackage Include="$(OutputPath)\**\*.dll">
<TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(FullPath)))</TargetPath>
</BuildOutputInPackage>
<BuildOutputInPackage Include="$(OutputPath)\**\*.exe">
<TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(FullPath)))</TargetPath>
</BuildOutputInPackage>
</ItemGroup>
</Target>
According to the docs, I should be able to use %(FullPath), but I am getting this error:
error MSB4184: The expression "[MSBuild]::MakeRelative(C:\Sour
ce\RepositoryCleaner\output\Release\RepositoryCleaner\netcoreapp3.1\, '')" cannot be evaluated. Parameter "path" cannot have zero length.
[C:\Source\RepositoryCleaner\src\RepositoryCleaner\RepositoryCleaner.csproj]
Any idea why the well-known items don't seem to work in this scenario?
Got a fix by specifying the item group outside the target and then using that instead.
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetToolsPackageFiles</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<ItemGroup>
<ToolDllFiles Include="$(OutputPath)\**\*.dll" />
<ToolExeFiles Include="$(OutputPath)\**\*.exe" />
</ItemGroup>
<Target Name="GetToolsPackageFiles">
<ItemGroup>
<BuildOutputInPackage Include="#(ToolDllFiles)">
<TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(ToolDllFiles.FullPath)))</TargetPath>
</BuildOutputInPackage>
<BuildOutputInPackage Include="#(ToolExeFiles)">
<TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(ToolExeFiles.FullPath)))</TargetPath>
</BuildOutputInPackage>
</ItemGroup>
</Target>

EmbeddedResource not working inside Target

I have some TypescriptCompile files which I then want to embed in my .dll. I had to migrate to the new csproj format and now I can't embed while inside a Target.
This works:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="testFile.ts" />
</ItemGroup>
</Project>
While this does not:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<Target Name="AddGeneratedToBuildOutput" BeforeTargets="CoreCompile">
<ItemGroup>
<PackageFiles Include="$(MSBuildProjectDirectory)\**\*.*;"/>
</ItemGroup>
<Message Text="The target is called: %(PackageFiles.FullPath)" Importance="high"/>
<ItemGroup>
<EmbeddedResource Include="testFile.ts" />
</ItemGroup>
</Target>
</Project>
Note that the message gets written and the referred file is in the project, hence we can be sure that the target gets called.
I already tried other targets instead of CoreCompile but since I'm using TypeScriptCompile, if I use BeforeBuild, Build or ResolveReferences I get compilation errors since the .js files are not generated yet.
I'm using JetBrains' DotPeek to inspect the resources and using msbuild 15.0.
Solution to this problem is here: https://github.com/microsoft/msbuild/issues/4778

Msbuild - build with output in diffrent folders

I have right now this in a script and want to have it in a msbuild instead.
msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder XXXXX\XXXX.XXX.xxx\XXXXX.XXXX.XXXXX1.csproj /p:Configuration="Release";_PackageTempDir=....\Deploy\XXXX1
msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder XXXXX\XXXX.XXX.xxx\XXXXX.XXXX.XXXXX2.csproj /p:Configuration="Release";_PackageTempDir=....\Deploy\XXXX2
How would this be written in a msbuild script? I only have managed to do it with one build but to create diffrent folders in the Deploy folder I havent been able to do.
Could someone teach me?
You can specify OutDir (new projects) or OutputPath (some old project types) as Properties when you call msbuild task to build your project. Or whatever property you want, like your "_PackageTempDir"
Something like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BatchMyProjects" ToolsVersion="4.0">
<ItemGroup>
<BuildMyProjects Include="XXXX-Project-1" />
<BuildMyProjects Include="XXXX-Project-2" />
</ItemGroup>
<Target Name="BatchMyProjects" >
<ItemGroup>
<ProjectsToBuild Condition="Exists('SomeSourcePath\%(BuildMyProjects.Identity)/%(BuildMyProjects.Identity).csproj')">
<ProjectName>SomeSourcePath/%(BuildMyProjects.Identity)/%(BuildMyProjects.Identity).csproj</ProjectName>
<PublishSubFolder>%(BuildMyProjects.Identity)</PublishSubFolder>
</ProjectsToBuild>
</ItemGroup>
<MSBuild Projects="%(ProjectsToBuild.ProjectName)" Targets="Build;PipelinePreDeployCopyAllFilesToOneFolder"
Properties="Configuration=Release;
OutDir=SomePathToDeploy/Deploy/%(ProjectsToBuild.PublishSubFolder)/;
OutputPath= SomePathToDeploy/Deploy/%(ProjectsToBuild.PublishSubFolder)/;
_PackageTempDir=SomePathToDeploy/Deploy/%(ProjectsToBuild.PublishSubFolder)/
" />
</Target>
</Project>
Also I created gist for this same example
This do work but that seems abit strange. I rateher have it to work as you mentioned
<Target Name="testar" >
<MSBuild Projects="..\xxxxxx\xxxxxx1.csproj" Targets="Build;PipelinePreDeployCopyAllFilesToOneFolder" Properties="Configuration=Release;_PackageTempDir=../../Deploy/xxx1/"/>
<MSBuild Projects="..\xxxx\xxxxxx2.csproj" Targets="Build;PipelinePreDeployCopyAllFilesToOneFolder" Properties="Configuration=Release;_PackageTempDir=../../Deploy/xxx2/"/>
</Target>

Additional paths in msbuild script

How to specify additional assembly reference paths for the MSBuild tasks?
I have following script so far, but can't figure out how to specify additional search paths.
<ItemGroup>
<ProjectsToBuild Include="..\Main\Main.sln" />
</ItemGroup>
<!-- The follwing paths should be added to reference search paths for the build tasks -->
<ItemGroup>
<MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib1" />
<MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib2" />
</ItemGroup>
<MSBuild
Projects="#(ProjectsToBuild)"
Properties="Configuration=Debug;OutputPath=$(BuildOutputPath)">
</MSBuild>
UPDATE:
Please show one complete working script which invokes original project, such as an SLN with multiple additional reference paths.
No suggestions on how to improve the project structure please.
I know how to build a good structure, but now it's the task of building an existing piece of crap.
I have finaly figured out how to do it:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectsToBuild Include="ConsoleApplication1\ConsoleApplication1.csproj" />
</ItemGroup>
<ItemGroup>
<AdditionalReferencePaths Include="..\Build\ClassLibrary1" />
<AdditionalReferencePaths Include="..\Build\ClassLibrary2" />
</ItemGroup>
<PropertyGroup>
<BuildOutputPath>..\Build\ConsoleApplication1</BuildOutputPath>
</PropertyGroup>
<Target Name="MainBuild">
<PropertyGroup>
<AdditionalReferencePathsProp>#(AdditionalReferencePaths)</AdditionalReferencePathsProp>
</PropertyGroup>
<MSBuild
Projects="ConsoleApplication1\ConsoleApplication1.csproj"
Properties="ReferencePath=$(AdditionalReferencePathsProp);OutputPath=$(BuildOutputPath)"
>
</MSBuild>
</Target>
The property you want to modify is AssemblySearchPaths. See the ResolveAssemblyReference task more information.
<Target Name="AddToSearchPaths">
<CreateProperty Value="x:\path\to\assemblies;$(AssemblySearchPaths)">
<Output PropertyName="AssemblySearchPaths" TaskParameter="Value" />
</CreateProperty>
</Target>
Making use of item groups, as in your example, it would look like:
<Target Name="AddToSearchPaths">
<CreateProperty Value="#(MyAddRefPath);$(AssemblySearchPaths)">
<Output PropertyName="AssemblySearchPaths" TaskParameter="Value" />
</CreateProperty>
</Target>
Looking in %WINDIR%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets, you can see that the ResolveAssemblyReference Task is executed as part of the ResolveAssemblyReferences target. Thus, you want the newly added target to modify the AssemblySearchPaths property before ResolveAssemblyReferences is executed.
You've stated that you want to be able to modify the assembly search paths without modifying the project files directly. In order to accomplish that requirement you need to set an environment variable that will override the AssemblySearchPaths. With this technique you will need to provide every assembly reference path used by all the projects in the solutions. (Modifying the projects or copies of the projects would be easier. See final comments.)
One technique is to create a batch file that runs your script at sets the environment variable:
set AssemblySearchPaths="C:\Tacos;C:\Burritos;C:\Chalupas"
msbuild whatever.msbuild
Another way is to define a PropertyGroup in your custom msbuild file (otherwise known as the "hook" needed to make this work):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectsToBuild Include="..\Main\Main.sln" />
</ItemGroup>
<PropertyGroup>
<AssemblySearchPaths>$(MSBuildProjectDirectory)\..\..\Build\Lib1;$(MSBuildProjectDirectory)\..\..\Build\Lib2</AssemblySearchPaths>
</PropertyGroup>
<Target Name="Build">
<MSBuild Projects="#(ProjectsToBuild)" Properties="AssemblySearchPaths=$(AssemblySearchPaths);Configuration=Debug;OutputPath=$(OutputPath)" />
</Target>
</Project>
Now if it were me, and for whatever unexplained reason I couldn't modify the project files to include the updated references that I am going to build with, I would make copies of the project files, load them into the IDE, and correct the references in my copies. Synching the projects becomes a simple diff/merge operation which is automatic with modern tools like mercurial (heck I'm sure clearcase could manage it too).
...and remember that you don't need to use a target for this, you can use project-scoped properties or items, as...
<ItemGroup>
<MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib1" />
<MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib2" />
</ItemGroup>
<PropertyGroup>
<MyAddRefPath>$(MSBuildProjectDirectory)\..\..\Build\Lib3</MyAddRefPath>
<!-- add in the property path -->
<AssemblySearchPaths>$(MyAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
<!-- add in the item paths -->
<AssemblySearchPaths>#(MyAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
</PropertyGroup>
...and if you do need to do this in a target to pick up paths from a dynamically populated item group, use inline properties, not the CreateProperty task (if you are not stuck in v2.0)
<Target Name="AddToSearchPaths">
<PropertyGroup>
<!-- add in the item paths -->
<AssemblySearchPaths>#(MyDynamicAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
</PropertyGroup>
</Target>

How do I execute tasks in MSBUILD?

I'm trying to wrap my head around MSBuild.
I have a very simple script that does the following so far:
Builds a solution and places it in my drop location.
I have created a <Target> and in it I would like to copy files and from my source control location and drop them in the drop location as well.
Eventually the script will have to create the folders etc. For now I am just trying to copy one file over to see how this works.
The solution builds and is placed in the drop location but no files are copied. The build log makes no mention of this Target ever being run.
What am I missing?
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<Target Name="Build">
<Message Text="Building msbuildintro" />
</Target>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
<ProjectExtensions>
<ProjectFileVersion>2</ProjectFileVersion>
<Description></Description>
<BuildMachine>hw-tfs-build02</BuildMachine>
</ProjectExtensions>
<PropertyGroup>
/* Properties here*/
</PropertyGroup>
<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/HostASPX/mySolution.sln">
<Targets></Targets>
<Properties></Properties>
</SolutionToBuild>
<CommonFiles Include="$(SolutionRoot)\trunk\folder\Common\Shared\js\somefile.js"></CommonFiles>
</ItemGroup>
<ItemGroup>
<ConfigurationToBuild Include="Release|Any CPU">
<FlavorToBuild>Release</FlavorToBuild>
<PlatformToBuild>Any CPU</PlatformToBuild>
</ConfigurationToBuild>
</ItemGroup>
<Target Name="CopyCommonData">
<Message Text="Copy Common Data" />
<Copy SourceFiles="#(CommonFiles)"
DestinationFiles="$(DropLocation)\Common\somefile.js" />
</Target>
</Project>
Thanks!
OH I get it.. Target Names are not 'made up'. They must be a specific Target Name found here:
http://msdn.microsoft.com/en-us/library/aa337604.aspx