visual-studiomsbuild.net-assemblyautomatic-differentiationassemblyversions

Automatically change the version of only assembly which has code changes in Dot Net


I am creating patch for my application to deploy small changes to customer. In my application I have 100 .CSProject. Out of 100 library I made code changes in class library A, B, C and Library D is calling to A,B and C library. So Is there any way when I build my application then It should change the version of only A,B,C and D library which have changes. Manually I can change but I need any automatic way.


Solution

  • So Is there any way when I build my application then It should change the version of only A,B,C and D library which have changes. Manually I can change but I need any automatic way.

    Actually, Directory Build.targets file can probably realize it. When you create a file called Directory.Build.targets, it can search and act on all projects in the current folder and subordinates. See this document about its search scope. Directory Build.targets is pretty much like Directory Build.props. And Directory Build.targets executes after build while Directory Build.props executes before the build starts.

    In your situation, you want to overwrite property Version, you should use Directory Build.targets.

    Then when you're building a project, it automatically puts content in the scope of the project.

    Solution

    1) create a file called Directory Build.targets file under your solution or upper level directory of 100 projects.

    2) add these into Directory Build.targets file:

    <Target Name="change_property" AfterTargets="CoreCompile">
    <PropertyGroup>
    <Version>xxxxx</Version>
    </PropertyGroup>
    </Target>
    

    3) Then you can set all changed projects' version to the same version that you wish.

    And this function makes the same property changes for all the changed projects.

    You should Note that it has a flaw

    But you should not modify other related projects(code changes or add new item, picture or anything else) If you change unrelated projects, this feature also changes the version of unrelated projects. This is a special feature of CoreCompile target.

    Suggestion

    Therefore, you should pay attention not to modifying other projects or you could use Alt+A to select all projects-->unload project and then reload required projects and modify them in case any other unrelated projects are modified.

    Or try to put these required projects into a new folder called Modified Projects under the solution folder(which exists xxx.sln file) and then put Directory Build.targets into Modified Projects folder.

    -----------------Use other methods like this to avoid this situation.

    Update 1

    1) If your projects are in the same solution folder, then you can create a file called Directory.Build.targets.

    enter image description here

    It will work on all items in the current folder and its secondary directory.

    And add my sample code in this file:

    <Project>
    <Target Name="change_property" AfterTargets="CoreCompile">
        <PropertyGroup>
        <Version>xxxxx</Version>
        </PropertyGroup>
        </Target>
    </Project>
    

    And then you can modify your nuget projects, when you finishing your related projects, you can build your solution or use MSBuild.exe to build xxx.sln and this file will be executed automatically and will change the version of modified nuget projects.

    Hope it could help you and any other feedback will be expected.

    Update 2

    Please try to change to use in Directory.Build.targets and it will work under the whole solution to apply to every verison number.

     <Project>
        <Target Name="change_property" AfterTargets="CoreCompile">
            <PropertyGroup>
            <Version>xxxxx</Version>
            </PropertyGroup>
            </Target>
        </Project>
    <PropertyGroup>
            <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        </PropertyGroup>
    

    And make sure that <GeneratePackageOnBuild>true</GeneratePackageOnBuild> in Directory.Build.targets. Otherwise it will fail. So please remove this node in xxx.csorj file and add <GeneratePackageOnBuild>true</GeneratePackageOnBuild> in Directory.Build.targets.