简体   繁体   English

Visual Studio用于通过msbuild命令行进行增量生成的生成目标

[英]Build target used by Visual Studio to do Incremental Build via msbuild command line

I created a task in our C# Projects to auto-version projects when they are built (changes are made) in release mode. 我在C#项目中创建了一个任务,以便在发布模式下构建(更改)项目时自动对其进行版本控制。 The versioning part works perfectly. 版本控制部分工作完美。 However, all the projects are being built regardless if the project actually changed when done from command line. 但是,所有项目都在构建中,无论从命令行完成时项目是否实际更改。 This causes projects to be versioned unnecessarily. 这导致不必要地对项目进行版本控制。 Building in Visual Studio works, unchanged projects are not built, however we made a tool to do automated build using msbuild.exe and are using this as a temporary fix while we work on Bamboo and that method always does a blind build, even if there are no changes to the project. 在Visual Studio中工作时,不会生成未更改的项目,但是我们制作了一个工具,使用msbuild.exe进行自动生成,并且在使用Bamboo时将其用作临时修补程序,并且该方法始终会进行盲目编译,即使存在对项目没有任何更改。 I need to be able to identify if changes were made to the project. 我需要能够确定是否对该项目进行了更改。

Something like 就像是

'$(wasSourceUpdated)' == 'true' or some kind of target condition to use on my custom versioning target. '$(wasSourceUpdated)'=='true'或在自定义版本控制目标上使用的某种目标条件。

Here is a sample of my versioning task in our projects 这是我在项目中的版本控制任务的示例

<Import Project="..\..\DXT.BuildTasks\Targets\DXTAutoIncrementVersion.targets" Condition="Exists('..\..\DXT.BuildTasks\Targets\DXTAutoIncrementVersion.targets') And '$(Configuration)|$(Platform)' == 'Release|AnyCPU' And '$(DeployOnBuild)' != 'true'" />

I also checked this and this articles to no avail. 我也检查了这篇文章也无济于事。

EDIT 编辑

I need the task to run before the build is actually executed in order to stamp the generated assemblies with the new versions 我需要在实际执行构建之前运行任务,以便使用新版本标记生成的程序集

EDIT 2 编辑2

What I'm really looking for is the condition to run CoreCompile or to run CoreCompile again when I detect that the assembly was updated 我真正追求的是运行CoreCompile或再次运行CoreCompile 条件时,我发现,大会进行了更新

What I've tried so far: 到目前为止,我已经尝试过:

<Project>
<PropertyGroup>
    <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
<PropertyGroup>
    <_AssemblyTimestampBeforeCompile>%(IntermediateAssembly.ModifiedTime)</_AssemblyTimestampBeforeCompile>
</PropertyGroup>
<PropertyGroup>
    <_AssemblyTimestampAfterCompile>%(IntermediateAssembly.ModifiedTime)</_AssemblyTimestampAfterCompile>
</PropertyGroup>
<PropertyGroup>
    <_ProjectVersioned Condition="'$(_ProjectVersioned)'==''">false</_ProjectVersioned>
</PropertyGroup>
<Target Name="IncrementVersionBeforeBuild" AfterTargets="CoreCompile" Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)' and '$(_ProjectVersioned)' == 'false'">
 <Message Text="Before $(_AssemblyTimestampBeforeCompile) After $(_AssemblyTimestampAfterCompile)" Importance="High"/>
    <IncrementVersion
  ProjectPath="$(MSBuildProjectFullPath)"
    VersionRule="3.3.0.+"
    FileName="Properties\AssemblyInfo.cs">
    </IncrementVersion>
</Target>
<PropertyGroup>
    <TaskPath>$(MSBuildThisFileDirectory)..\Tasks\AutoVersionTask\AutoVersionTask\bin\Debug</TaskPath>
</PropertyGroup>
<!-- Sample import for projects
<Import Project="..\..\DXT.BuildTasks\Targets\DXTAutoIncrementVersion.targets" Condition="Exists('..\..\DXT.BuildTasks\Targets\DXTAutoIncrementVersion.targets') And '$(Configuration)|$(Platform)' == 'Release|AnyCPU' And '$(DeployOnBuild)' != 'true'" />
-->
<UsingTask AssemblyFile="$(TaskPath)\AutoVersionTask.dll" TaskName="AutoVersionTask.IncrementVersion" />

<PropertyGroup>
    <_ProjectVersioned>true</_ProjectVersioned>
</PropertyGroup>

Thanks in advance 提前致谢

So Thanks to Lance for getting me to understand MSBuild to the point that I understand the issue way better. 因此,感谢Lance使我理解MSBuild,以至于我更好地理解了问题。

After a long time researching the default task, I ran upon this question that had the perfect solution to my issue. 经过漫长的时间来研究默认任务,我跑在这个疑问,有完美的解决了我的问题。 After applying the fix the versioning task now only runs when changes are made to the msbuild code. 应用此修复程序后,版本控制任务现在仅在对msbuild代码进行更改时才运行。

The inputs and outputs are the same as the CoreCompile target and ensures that the task is only run if there were changes to the source 输入和输出与CoreCompile目标相同,并确保仅在源发生更改时才运行任务

Here is the target I ran that works: 这是我运行的有效目标:

<?xml version="1.0" encoding="utf-8"?>
<Project>
    <PropertyGroup>
        <CoreCompileDependsOn>
        $(CoreCompileDependsOn);
        IncrementVersionBeforeBuild
        </CoreCompileDependsOn>
    </PropertyGroup>
    <Target Name="IncrementVersionBeforeBuild"
            Inputs="$(MSBuildAllProjects);
            @(Compile);                               
            @(_CoreCompileResourceInputs);
            $(ApplicationIcon);
            $(AssemblyOriginatorKeyFile);
            @(ReferencePath);
            @(CompiledLicenseFile);
            @(EmbeddedDocumentation); 
            $(Win32Resource);
            $(Win32Manifest);
            @(CustomAdditionalCompileInputs)"
            Outputs="@(DocFileItem);
             @(IntermediateAssembly);
             @(_DebugSymbolsIntermediatePath);                 
             $(NonExistentFile);
             @(CustomAdditionalCompileOutputs)"
    >
        <Message Text="Version Task running" Importance="High"/>
        <IncrementVersion
      ProjectPath="$(MSBuildProjectFullPath)"
        VersionRule="3.3.0.+"
        FileName="Properties\AssemblyInfo.cs">
        </IncrementVersion>
    </Target>
    <PropertyGroup>
        <TaskPath>$(MSBuildThisFileDirectory)..\Tasks\AutoVersionTask\AutoVersionTask\bin\Debug</TaskPath>
    </PropertyGroup>
    <UsingTask AssemblyFile="$(TaskPath)\AutoVersionTask.dll" TaskName="AutoVersionTask.IncrementVersion" />

    <PropertyGroup>
        <_ProjectVersioned>true</_ProjectVersioned>
    </PropertyGroup>
</Project>

Normaly, we can add the script below into .csproj file: 通常,我们可以将以下脚本添加到.csproj文件中:

  <PropertyGroup>
    <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
  </PropertyGroup>
  <Target Name="AutoVersionWhenBuild" AfterTargets="CoreBuild"
          Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
    <Message Importance="high" Text="Auto-version begins when changes are made!"/>
    <!--<AutoVersionTask>Do your auto-version task here.</AutoVersionTask>-->
  </Target>

It will be called during the build when changes are really made to the project. 当真正对项目进行更改时,将在构建过程中调用它。 See this similar issue . 看到这个类似的问题

As for your situation: 至于你的情况:

It seems your tasks and target comes from the targets file DXTAutoIncrementVersion.targets ,you can open that file and change the target in it to the format above. 看来您的任务和目标来自目标文件DXTAutoIncrementVersion.targets ,您可以打开该文件并将其中的目标更改为上面的格式。

In addition: Please check the relationship between tasks , targets and .targets file. 另外:请检查任务目标.targets文件之间的关系。

1.MSBuild uses tasks to perform these actions. 1.MSBuild使用任务执行这些操作。

2.Targets group tasks together. 2.Targets将任务分组在一起。

3.MSBuild includes several .targets files that contain items, properties, targets, and tasks for common scenarios. 3.MSBuild包含几个.targets文件,其中包含常见方案的项目,属性,目标和任务。

So you can either modify your auto-version target in the xx.targets file , or use the script above, and call the auto-version task in the AutoVersionWhenBuild target . 因此,您可以modify your auto-version target in the xx.targets file ,也可以use the script above, and call the auto-version task in the AutoVersionWhenBuild target Hope it helps:) 希望能帮助到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM