.NET Framework Bookmark and Share   
 index > MSBuild > CSProj self-modifying ApplicationVersion
 

CSProj self-modifying ApplicationVersion

I'm a newbie to MSBuild and am using a mostly Visual Studio built .csproj file as I have a very simple build process. I've created a custom task to get the AssemblyFileVersion from the AssemblyInfo.cs. I want to then use that as the publish version (ApplicationVersion ). I thought the following would work which I added to my .csproj at the end of Project :
<UsingTask AssemblyFile="GetAssemblyFileVersion.dll" TaskName="GetAssemblyFileVersion.GetAssemblyFileVersion" />
<Target Name="AfterCompile">
    <GetAssemblyFileVersion strFilePathAssemblyInfo="$(SolutionDir)\AssemblyInfo.cs">
        <Output TaskParameter="strAssemblyFileVersion" PropertyName="strAssemblyFileVersion" />
    </GetAssemblyFileVersion>
    <PropertyGroup>
        <ApplicationVersion>$(strAssemblyFileVersion)</ApplicationVersion>
    </PropertyGroup>
    <Message Text="ApplicationVersion = $(ApplicationVersion)" />
</Target>


This works insofar as it puts the proper version into the ApplicationVersion property within the Target , which I verified using Message . However, that property only occasionally gets used by the Publish step as the publish version. And I've been unable to figure out why it uses it on those rare occasions at all. It seems like it should be always (if I'm doing this right) or never (if I'm doing it wrong).

Perhaps I'm going about this improperly. I'd appreciate any assistance you can provide. Thank you.
  • Edited byGregory_G Tuesday, September 01, 2009 12:31 AMprevious edit destroyed formatting
  • Edited byGregory_G Tuesday, September 01, 2009 12:33 AMstill fixing formatting from previous edit
  • Edited byGregory_G Tuesday, September 01, 2009 12:31 AMremoved extra spaces caused by italics
  •  
Gregory_G

Hi, Gregory_G

Finally, I understand your problem and I got the reason for the failure of your updating the ApplicationVersion luckily. This actually has no relationship with the
GetAssemblyFileVersion.dll.
I guess in your <project> element you must write like this, Am I right ?

<Project ToolsVersion="3.5" DefaultTargets="Build;AfterCompile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
... ...
</Project>

Now I will explain the reason for you.

1. One point we should know firstis that the publish step use the *.application file which is generated by the MSBuild to publish our application, and the *.application is usuallyin yourobj\Debug or obj\Release folder.

2. When you put your target in the DefaultTargets. The MsBuilddoes not know which to be builtfirst, Build or AfterCompile.Sothis result in two conditons.

1) The Build target is built first, and then your aferCompile is built. Because the MsBuild has already set the ApplicationVersion and generate the *.application file. So your publish step use the old ApplicationVersion , this will cause your publish ApplicationVersion not updated by your aferCompile target.

2) The second condition is your aferCompile target is built first and your ApplicationVersion is updated by your afterCompile successfully.

To resolve this, We can change the project element to this

<Project ToolsVersion="3.5" InitialTargets="<span style="color:blue">AfterCompile</span>" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

This will make your target be built before the Build
Hope this could help you!
Thanks

Chao

  • Marked As Answer byGregory_G Thursday, September 03, 2009 1:43 PM
  •  
Chao Kuo

Hi,Gregory

I am sorry Istill unclear about your problem, you mean your ApplicationVersion not always getthe proper version, sometimes it gets the version, but sometimes it doesn't. Am I right?

Would you mind if yourposting the Execute() method here? Maybe there is something wrong in the GetAssemblyFileVersion.dll.

Thanks,
Chao

Chao Kuo
Hi Chao,

Thanks for replying. What happens is that if I build the project, the above MSBuild code only occasionally sets the ApplicationVersion properly. So if I, for example, look at the Publish Version in the Project Properties, it usually shows the old one. Or if I do a Build & Publish it doesn't publish using the adjusted ApplicationVersion. This is in Visual Studio 2008. Also, I have auto-increment revision turned off.

I do know that my custom task is working properly, however. If you see in the code above I have a <Message.../> line outputting the string that my custom task returns. And that's always correct. However, only on occasion does the <PropertyGroup...> section do what I believe it should be doing.

I did link in the original post to the entire custom task (you have to scroll down to the blue answer that starts "I've managed to solve this using a custom task..."), but I'll include it here as well. I'm fairly sure the custom task itself is working fine though for the reasons I stated above.


public bool Execute()
{
    StreamReader streamreaderAssemblyInfo = null;
    Match matchVersion;
    Group groupVersion;
    string strLine;
    strAssemblyFileVersion = String.Empty;
    try
    {
        streamreaderAssemblyInfo = new StreamReader(strFilePathAssemblyInfo);
        while ((strLine = streamreaderAssemblyInfo.ReadLine()) != null)
        {
            matchVersion = Regex.Match(strLine, @"(?:AssemblyFileVersion\("")(?<ver>(\d*)\.(\d*)(\.(\d*)(\.(\d*))?)?)(?:""\))", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
            if (matchVersion.Success)
            {
                groupVersion = matchVersion.Groups["ver"];
                if ((groupVersion.Success) && (!String.IsNullOrEmpty(groupVersion.Value)))
                {
                    strAssemblyFileVersion = groupVersion.Value;
                    break;
                }
            }
        }
    }
    catch (Exception e)
    {
        BuildMessageEventArgs args = new BuildMessageEventArgs(e.Message, string.Empty, "GetAssemblyFileVersion", MessageImportance.High);
        BuildEngine.LogMessageEvent(args);
    }
    finally { if (streamreaderAssemblyInfo != null) streamreaderAssemblyInfo.Close(); } 
    return (true);
}


So, to summarize, the <Message...> always shows the correct string, but <ApplicationVersion /> rarely gets set to it (but does occasionally, with no rhyme or reason).

Thank you,
Greg
  • Edited byGregory_G Thursday, September 03, 2009 4:15 AMtrying to fix code section
  •  
Gregory_G

Hi, Gregory_G

Finally, I understand your problem and I got the reason for the failure of your updating the ApplicationVersion luckily. This actually has no relationship with the
GetAssemblyFileVersion.dll.
I guess in your <project> element you must write like this, Am I right ?

<Project ToolsVersion="3.5" DefaultTargets="Build;AfterCompile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
... ...
</Project>

Now I will explain the reason for you.

1. One point we should know firstis that the publish step use the *.application file which is generated by the MSBuild to publish our application, and the *.application is usuallyin yourobj\Debug or obj\Release folder.

2. When you put your target in the DefaultTargets. The MsBuilddoes not know which to be builtfirst, Build or AfterCompile.Sothis result in two conditons.

1) The Build target is built first, and then your aferCompile is built. Because the MsBuild has already set the ApplicationVersion and generate the *.application file. So your publish step use the old ApplicationVersion , this will cause your publish ApplicationVersion not updated by your aferCompile target.

2) The second condition is your aferCompile target is built first and your ApplicationVersion is updated by your afterCompile successfully.

To resolve this, We can change the project element to this

<Project ToolsVersion="3.5" InitialTargets="<span style="color:blue">AfterCompile</span>" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

This will make your target be built before the Build
Hope this could help you!
Thanks

Chao

  • Marked As Answer byGregory_G Thursday, September 03, 2009 1:43 PM
  •  
Chao Kuo
Dear Chao,

That did it. Thank you both for the solution and the explanation. The intermittent successes even make sense thanks to the latter.

Best regards,
Greg
Gregory_G

You can use google to search for other answers

Custom Search

More Threads

• Daisy Chaining builds
• Tracing and logging with a custom tool
• conditionally add generated source files
• HELP! Unable to Read MyProject.vbproj(247,11).
• Problems with VSHOST.EXE
• VS inproc compiler, MSBUILD and the obj subdir
• MS Build doesn'r support to Setup Project and Merge module projects
• Auto Inc of Setup Project version and change product code
• Securing Passwords in MSBuild
• Package 'Visual Studio Source Control Integration Package' failed to load