2017-07-26 117 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;你只需要用一天的时间就可以为我节省时间。 –