获取版本netCore csproj file powershell

isr3a4wc  于 2023-10-18  发布在  Shell
关注(0)|答案(2)|浏览(170)

我有净核心项目文件。版本存储在project.csproj文件中。我使用下面的脚本来获取版本字符串。

$Project = Resolve-Path ".\\source\\Project\\Project.csproj"
$xml = [Xml] (Get-Content $Project)
$version = [Version] ($xml.Project.PropertyGroup.Version)[0]
Write-Host "##teamcity[buildNumber '$version']"

以下是在Windows Powershell伊势中执行的打印输出:

##teamcity[buildNumber '1.0.1']

但是,它在TeamCity中出现了以下错误:

Cannot index into a null array.
[07:48:01][Step 4/16] At C:\TeamCity\buildAgent\temp\buildTmp\powershell340377043469883282.ps1:3 char
[07:48:01][Step 4/16] :59
[07:48:01][Step 4/16] + $version = [Version] ($xml.Project.PropertyGroup.Version)[ <<<< 0]
[07:48:01][Step 4/16]     + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
[07:48:01][Step 4/16]     + FullyQualifiedErrorId : NullArray
[07:48:01][Step 4/16]

有什么问题吗?

zqry0prt

zqry0prt1#

我想我找到我的问题的原因了。在一个网络核心项目中,此csproj文件中有多个PropertyGroup元素。

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
  <TargetFramework>netcoreapp2.1</TargetFramework>
  PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
  <AssemblyName>CoreApi</AssemblyName>
  <OutputType>Exe</OutputType>
  <PackageId>Project</PackageId>
  <Version>1.0.1</Version>
</PropertyGroup>

 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
 <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
 <TreatSpecificWarningsAsErrors />
  <DocumentationFile>bin\Release\netcoreapp2.1\CoreApi.xml</DocumentationFile>
 <NoWarn>1701;1702;1705;NU1603</NoWarn>
</PropertyGroup>

为了适应这一点,我必须使用以下代码

$Project = Resolve-Path ".\\source\\Project\\Project.csproj"
$xml = [Xml] (Get-Content $Project)
$propertyGroup = $xml.Project.PropertyGroup
if ($propertyGroup -is [array]) {
  $version = [version] $xml.Project.PropertyGroup[0].Version
  Write-Host "##teamcity[buildNumber '$version']"
}else {
  $version = [version] $xml.Project.PropertyGroup.Version
  Write-Host "##teamcity[buildNumber '$version']"
}
jyztefdp

jyztefdp2#

您需要运行包含.csproj文件的文件夹中的命令,然后运行以下命令
$xml = [Xml](Get-Content .\DEPLOY.Cachorro.API.csproj)$version = [Version] $xml.Project.PropertyGroup.AssemblyVersion
您需要确定您的版本是否在AssemblyVersion或Version标记中。我正在使用汇编版本x1c 0d1x
然后,您可以执行该命令以了解存储在“version”变量中的值
写入主机“版本:$version”
看一看:

相关问题