I am trying to set the version number to something I want (read they want me to do). They want a specific format, and I need to set the version of out programme.
I tried to use a UsingTask to set a PropertyGroup variable. My UsingTask is working.
All this is new to me, I managed to get the code below. Now, how can I set the local project variable? My initial value is used, next I want to set it programmatically in the UsingTask
This is all done in the *.csproj file
<Project Sdk="Microsoft.NET.Sdk">
<!-- my additional project variable, used below -->
<PropertyGroup Label="UserMacros">
<AssemblyVersion>1.2.3.5</AssemblyVersion>
</PropertyGroup>
<UsingTask TaskName="EnvVarSet"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
// here I want to set the version number to our format
Log.LogMessage(MessageImportance.High, "**** Setting variables");
System.Environment.SetEnvironmentVariable("AssemblyVersion", "1.5.6.7");
var a = Environment.GetEnvironmentVariable("AssemblyVersion");
Log.LogMessage(MessageImportance.High, "**** Value is: " a);
]]>
</Code>
</Task>
</UsingTask>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<!-- the version number I want to set -->
<Version>$(AssemblyVersion)</Version>
<!-- this does not work -->
<!-- <Version>$([System.Environment]::GetEnvironmentVariable('AssemblyVersion'))</Version>-->
other values....
</PropertyGroup>
//more project....
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<EnvVarSet />
<Message Text="***** AssemblyVersionis now: $([System.Environment]::GetEnvironmentVariable('AssemblyVersion'))"/>
</Target>
</Project>
CodePudding user response:
You can view the document https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build. You can do this by using the dotnct command. Use multiple doctnct commands to pass the version number to each command and build the project and all its dependencies via dotnet build-. You can use the following command to help you set the version number: dotnet build /p:AssemblyVersion=1.2.3.4
CodePudding user response:
I managed to get what I want. Here is my example of how to do the classic Build and Revision autoincrement numbers in the assembly version This is an example, I was asked to do another format, but one can create any format this way
First, add the AssemblyVersion
<PropertyGroup>
<!-- add this line -->
<AssemblyVersion>1.0.0.0</AssemblyVersion>
And add this code, which is similar to "1.2.*" in .Net Framework
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<SetBuildNumber>
<Output TaskParameter="AssemblyVersion" PropertyName="AssemblyVersion" />
</SetBuildNumber>
</Target>
<UsingTask TaskName="SetBuildNumber" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyVersion ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage(MessageImportance.High, "Setting version number");
var now = DateTime.Now;
var secondsSinceMidnightDivivedBy2 = (int)(now - now.Date).TotalSeconds / 2;
var daysSinceJan1st2000 = (int)(now - new DateTime(2000, 1, 1)).TotalDays;
AssemblyVersion = "1.2."
daysSinceJan1st2000.ToString() "."
secondsSinceMidnightDivivedBy2.ToString();
Log.LogMessage(MessageImportance.High, "Version number is: " AssemblyVersion);
]]>
</Code>
</Task>
</UsingTask>
In the code above, one can set the assembly version to what may be required
CodePudding user response:
The code above give a custom assembly version to the final exe or dll file, but for some reason it does not work, when the installer publishes the project to one file, in the "pre-build event command line":
dotnet publish $(SolutionDir)NCAutomatedReport\NCAutomatedReport.csproj
-p:PublishProfile=FolderProfile -r:win10-x64 -p:PublishSingleFile=true
-p:PublishReadyToRun=false -p:PublishTrimmed=false -c:Release
-o:$(TargetDir)published
This gives the error
The task factory "CodeTaskFactory" is not supported on the .NET Core version of MSBuild.
and
The task factory "CodeTaskFactory" could not be loaded from the assembly
"C:\Program Files\dotnet\sdk\6.0.400\Microsoft.Build.Tasks.Core.dll".
The task factory must return a value for the "TaskType" property.
Now I should look into this - any ideas anyone?