2017-07-26 119 views
2

我一直試圖更新ApplicationVersion屬性在我的csproj file.witch工作正常;我添加了一個運行自定義任務的目標,以從我的assemblyinfo.cs中提取AssemblyFileVersion;這是毫無疑問的。 但是,當我想使用我更新的ApplicationVersion來確定將我的新構建文件放在哪裏時,我得到屬性中設置的默認值。csproj MSBuild更新屬性

<PropertyGroup> 
     ... 
     <ApplicationVersion>1.0.0.0</ApplicationVersion> 
     ... 
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
     <PlatformTarget>AnyCPU</PlatformTarget> 
     <DebugSymbols>true</DebugSymbols> 
     <DebugType>full</DebugType> 
     <Optimize>false</Optimize> 
     <OutputPath>..\media-converter-BUILD\debug\$(ApplicationVersion)\</OutputPath> 
     <DefineConstants>DEBUG;TRACE</DefineConstants> 
     <ErrorReport>prompt</ErrorReport> 
     <WarningLevel>4</WarningLevel> 
     <DocumentationFile>..\media-converter-BUILD\debug\$(ApplicationVersion)\MediaConverter.XML</DocumentationFile> 
    </PropertyGroup> 

我的目標

<UsingTask AssemblyFile="GetAssemblyFileVersion.dll" TaskName="GetAssemblyFileVersion.GetAssemblyFileVersion" /> 
    <Target Name="MainAfterCompile"> 
     <CallTarget Targets="AfterCompile" /> 
     <CallTarget Targets="VerifyParam" /> 
    </Target> 
    <Target Name="AfterCompile"> 
     <GetAssemblyFileVersion strFilePathAssemblyInfo="Properties\AssemblyInfo.cs"> 
      <Output TaskParameter="strAssemblyFileVersion" PropertyName="ApplicationVersionModded" /> 
     </GetAssemblyFileVersion> 
     <PropertyGroup> 
      <ApplicationVersion>$(ApplicationVersionModded)</ApplicationVersion> 
     </PropertyGroup> 
    </Target> 

    <Target Name="VerifyParam"> 
     <Message Text="New $(ApplicationVersionModded)" Importance="high"/> 
     <Message Text="Old Updated $(ApplicationVersion)" Importance="high"/> 
    </Target> 

GetAssemblyFileVersion.dll我或多或少從一些後我發現在互聯網上偷了,就不能再次找到它,這樣我就可以」添加一個鏈接,對不起。

我爲什麼不起作用的理論是PropertyGroups中的變換和參數在InitailTagets和DefaultTargets都運行之前被渲染。還有的將我的計劃行不通

但如果有人知道的一種方式,使其工作,我會感激這裏吧

回答

0

我爲什麼它不工作的理論是,轉換及參數在PropertyGroups中渲染之前,InitailTagets和DefaultTargets確實運行了,這就是evaluation order的工作方式:msbuild在該文件的第一遍中評估全局屬性,您定義了由Microsoft.Common.CurrentVersion.targets文件使用的OutputPath派生OutDir/BaseIntermediateOutputPath/....然後在另一個階段中,您的目標運行並更新版本號,但是沒有另一個階段再次評估全局OutputPath屬性。

但是,您可以覆蓋Target中OutputPath和派生路徑的值,並且它會生效,您只需在構建中儘早運行它,以便其他目標使用更新後的版本。這確實的伎倆:

<Target Name="GetApplicationVersion"> 
    <GetAssemblyFileVersion strFilePathAssemblyInfo="Properties\AssemblyInfo.cs"> 
    <Output TaskParameter="strAssemblyFileVersion" PropertyName="ApplicationVersion" /> 
    </GetAssemblyFileVersion> 
</Target> 
<Target Name="SetOutputPaths" DependsOnTargets="GetApplicationVersion" 
     BeforeTargets="PrepareForBuild"> 
    <PropertyGroup> 
    <OutputPath>bin\$(Configuration)\$(ApplicationVersion)\</OutputPath> 
    <OutDir>$(OutputPath)</OutDir> 
    </PropertyGroup> 
    <Message Text="Set OutDir to $(OutDir)" Importance="high" /> 
</Target> 

另一種方式來處理,這是周圍做事情的另一種方式:定義應用程序的版本作爲一個全球性的MSBuild屬性,然後用它來定義OutputPath和更新的AssemblyVersion的數量。編譯之前的cs。

+0

Thanks mate;你只需要用一天的時間就可以爲我節省時間。 –