我有净核心项目文件。版本存储在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 ISE中执行时的打印输出:
##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]
出什么问题了?
答案 0 :(得分:1)
我认为我找到了问题的原因。在一个网络核心项目中,此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']"
}