使用 MSBuild 属性 函数时无法将 nuget 文件复制到目标位置

Can't copy nuget file into destination when I use MSBuild Property Functions

我当前的任务有两个目标;自动增加一个Nuget包版本的版本号,并且复制生成的.nupkg文件到一个构建后的新文件夹。

在我的 .csproj 文件中使用 <GeneratePackageOnBuild>True</GeneratePackageOnBuild> 我正在尝试使用动态方法 (MSBuild 属性 Functions) 来增加我的汇编版本的版本号。而不是静态定义 <version>1.0.0</version> 我可以创建这样的动态内容; <version>$([System.DateTime]::Now.ToString("yyyy.MM.dd"))</version>。这将在 bin/debug 文件夹中创建一个名为 projectName.2017.7.26.nupkg 的文件。

此过程会生成一个文件,但它会导致 msbuild 构建过程中出现同步问题。在第二步,当我尝试将文件复制到目的地时,出现错误:Could not copy the file "bin\Debug\projectName.2017.07.26.nupkg" because it was not found

此错误出现半秒后,Nuget 包文件出现在 bin\debug 文件夹中,同名。

如果我不使用 $([System.DateTime]::Now.ToString("yyyy.MM.dd")),而是使用像 1.0.101 这样的静态版本号,一切正常,文件会被复制到目标文件夹。

这是我的 csproj 文件中的相关部分:

<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>

我是否需要在 CopyPackage 任务的 AfterTargets 中添加其他内容,目前它已设置为 Pack 的预定义目标?

您的问题似乎是前导零将从版本号中删除。因此,例如,如果 PackageVersion2017.07.27,则生成的文件将被称为 projectName.2017.7.27.nupkg,但您专门寻找其中有一个额外 0 的文件 - 实际上不是存在。

如果你使用

<NewVersionNumber>$([System.DateTime]::Now.ToString("yyyy.M.d"))</NewVersionNumber>

您的代码应该可以工作。