[Visual Studio 2017,.csproj属性]
若要自动更新PackageVersion / Version / AssemblyVersion属性(或任何其他属性),首先,创建一个新Microsoft.Build.Utilities.Task
类,该类将获取您的当前内部版本号并发送回更新的数字(我建议为该类创建一个单独的项目)。
我手动更新MAJOR.MINOR数字,但让MSBuild的自动更新版本号(1.1。1,1.1。2,1.1。3,等:)
using Microsoft.Build.Framework;
using System;
using System.Collections.Generic;
using System.Text;
public class RefreshVersion : Microsoft.Build.Utilities.Task
{
[Output]
public string NewVersionString { get; set; }
public string CurrentVersionString { get; set; }
public override bool Execute()
{
Version currentVersion = new Version(CurrentVersionString ?? "1.0.0");
DateTime d = DateTime.Now;
NewVersionString = new Version(currentVersion.Major,
currentVersion.Minor, currentVersion.Build+1).ToString();
return true;
}
}
然后调用您最近在MSBuild上创建的任务,在.csproj文件中添加下一个代码:
<Project Sdk="Microsoft.NET.Sdk">
...
<UsingTask TaskName="RefreshVersion" AssemblyFile="$(MSBuildThisFileFullPath)\..\..\<dll path>\BuildTasks.dll" />
<Target Name="RefreshVersionBuildTask" BeforeTargets="Pack" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<RefreshVersion CurrentVersionString="$(PackageVersion)">
<Output TaskParameter="NewVersionString" PropertyName="NewVersionString" />
</RefreshVersion>
<Message Text="Updating package version number to $(NewVersionString)..." Importance="high" />
<XmlPoke XmlInputPath="$(MSBuildProjectDirectory)\mustache.website.sdk.dotNET.csproj" Query="/Project/PropertyGroup/PackageVersion" Value="$(NewVersionString)" />
</Target>
...
<PropertyGroup>
..
<PackageVersion>1.1.4</PackageVersion>
..
选择Visual Studio Pack项目选项(更改BeforeTargets="Build"
为Build,以便在构建之前执行任务)时,将触发RefreshVersion代码以计算新版本号,并且XmlPoke
任务将相应地更新.csproj属性(是的,它将修改文件)。
在使用NuGet库时,我还通过将下一个构建任务添加到上一个示例中,将包发送到NuGet存储库。
<Message Text="Uploading package to NuGet..." Importance="high" />
<Exec WorkingDirectory="$(MSBuildProjectDirectory)\bin\release" Command="c:\nuget\nuget push *.nupkg -Source https://www.nuget.org/api/v2/package" IgnoreExitCode="true" />
c:\nuget\nuget
是我拥有NuGet客户端的位置(请记住通过调用保存您的NuGet API密钥,nuget SetApiKey <my-api-key>
或将密钥包含在NuGet推调用中)。
以防万一它可以帮助某人^ _ ^。