在Nuget的post-build中获取版本

时间:2012-07-30 13:43:00

标签: c# nuget

代码:

if $(ConfigurationName) == Release (
    cd $(ProjectDir)
    nuget spec Entities -f
    nuget pack DeusPak.Entities.csproj -Prop Configuration=Release
    nuget push DeusPak.Entities.$(version).nupkg  $(MYGET_API_KEY) -Source     http://www.myget.org/F/lojaali/api/v2/package
)

我刚刚开始使用NuGet,想知道如何在我的NuGet包中包含版本号。我目前正在将其编码到后期构建事件中,这显然不是我想要继续做的事情。有人可以帮忙吗?

这是我目前的后期制作活动:

if $(ConfigurationName) == Release (
    cd $(ProjectDir)
    nuget spec Dev-f
    nuget pack Dev.csproj -Prop Configuration=Release
    nuget push Dev.1.0.0.0.nupkg  $(MYGET_API_KEY) -Source     http://www.myget.org/F/api/v2/package
)

更新

好的,我已设法使用正确的自动递增版本号构建DLL:

if $(ConfigurationName) == Release (
    cd $(ProjectDir)
    nuget spec Dev -f
    nuget pack Dev.csproj -Prop Configuration=Release
    nuget push Dev.$(version).nupkg  $(MYGET_API_KEY) -Source     http://www.myget.org/F/api/v2/package
)

但是这个版本没有显示在我的MyGet包列表中。如何让它显示在那里以便可以下载?或者只能通过点击“添加包”手动完成?

3 个答案:

答案 0 :(得分:7)

只是扩展了CarlosJLópez提供的解决方案,我使用AfterBuild脚本实际调用NuGet并提供版本参数。

在我的情况下,我还在post build事件中添加了“cd $(ProjectDir)”,它实际上发生在AfterBuild脚本之前。

干杯,

    <Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">
      <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
          <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
      </GetAssemblyIdentity>
      <Exec Command="$(SolutionDir)\.nuget\nuget pack $(ProjectName).nuspec -Version %(AssemblyVersion.Version)" />
      <Message Text="$(SolutionDir)\.nuget\nuget pack $(ProjectName).nuspec -Version %(AssemblyVersion.Version)" Importance="high" />
    </Target>  

答案 1 :(得分:6)

在你的问题中不清楚,但假设你想要将软件包的版本与程序集的版本同步,你可以简单地管理项目的AssemblyInfo.cs文件中的AssemblyVersion属性

[assembly: AssemblyVersion("1.0.0")]

或者如果您想使用自动生成的内部版本号

[assembly: AssemblyVersion("1.0.0.*")]

如果要偏离程序集的版本,并且只指定程序包版本,可以使用AssemblyInfo.cs文件中的AssemblyInformationalVersion属性。

[assembly: AssemblyInformationalVersion("1.0.0")]

从问题中您使用的版本控制策略也不清楚,但我假设您想要应用Semantic Versioning(其中前3个版本号最相关)。通常在自动创建NuGet包时,我建议您在csproj目录中创建tokenized nuspec file,这样您就可以更轻松地操作包元数据。仅供参考,甚至还有一个NuGet套餐可以帮助您:

Install-Package NuSpec 

当定位MyProject.csproj时,NuGet会查找此nuspec(确保它名为MyProject.nuspec)。

<package>
  <version>$version$</version>
  ...
</package>

我还在此帖子的MyGet博客中解释了这一点:http://blog.myget.org/post/2012/04/27/NuGet-version-token-explained.aspx

调用nuget pack的post构建应该足够好了,假设你只需在构建之前更改程序集版本。

nuget pack MyProject.csproj

答案 2 :(得分:4)

这是你在after build事件中获得$(version)变量的方法,它应该和post build一样好。

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
        <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
    </GetAssemblyIdentity>
    <Exec Command="echo %(AssemblyVersion.Version)" />
    <Message Text="Released %(AssemblyVersion.Version)" Importance="high" />
</Target>

所以你必须修改你的.csproj文件,因为VS没有为它提供UI。