I have two goals on my current task; increment the version number of a Nuget package version automatically, and copy the generated .nupkg file into a new folder after the build.
With <GeneratePackageOnBuild>True</GeneratePackageOnBuild> inside my .csproj file I am trying to use a dynamic method (MSBuild Property Functions) to increase the version number of my assembly version. Instead of static definition <version>1.0.0</version> I am able to create something dynamic like this; <version>$([System.DateTime]::Now.ToString("yyyy.MM.dd"))</version>. This creates a file named projectName.2017.7.26.nupkg inside the bin/debug folder.
This process generates a file but it causes an synchronization issue in msbuild build process. On the second step, when I try to copy the file to the destination, I get the error: Could not copy the file "bin\Debug\projectName.2017.07.26.nupkg" because it was not found
Half a second after this error shows up, the Nuget package file appears in the bin\debug folder with the same name.
If I don't use $([System.DateTime]::Now.ToString("yyyy.MM.dd")) but use a static version number like 1.0.101, everything works fine and the file gets copied to the destination folder.
This is the relevant parts from my csproj file:
<PropertyGroup>
<NewVersionNumber>$([System.DateTime]::Now.ToString("yyyy.MM.dd"))</NewVersionNumber>
<CopyDestionationPath>..\Nuget</CopyDestionationPath>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netcoreapp1.0;net46;net40</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>$(AssemblyName)</PackageId>
<version>$(NewVersionNumber)</version>
<title>A Package</title>
.....
</PropertyGroup>
.....
<Target Name="CopyPackage" AfterTargets="Pack">
<ItemGroup>
<OldNugetFiles Include="$(CopyDestionationPath)\*nupkg" />
</ItemGroup>
<ItemGroup>
<NewNugetFile Include="$(OutputPath)$(PackageId).$(PackageVersion).nupkg" />
</ItemGroup>
<Delete Files="@(OldNugetFiles)" />
<Copy SourceFiles="@(NewNugetFile)" DestinationFolder="$(CopyDestionationPath)"/>
</Target>
Do I need something else in AfterTargets of CopyPackage task, currently it's set to pre-defined target of Pack?