Reputation: 51
im looking for a way to do the following things:
CommandLine
msbuild myProject.csproj /p:MyInfoText
And in my C# Project in the AssemblyInfo.cs I would do:
[assembly: AssemblyInformationalVersion($MyInfoText)]
I tried targets with BeforeBuild option, but nothing worked. Anybody can and want to help?
Greetings
Upvotes: 3
Views: 5739
Reputation: 51
First of all thx for your comments and answers!
I was looking for a way to set my AssemblyInformationalVersion with a property which I can set over the msbuild cli tools.
I dont want to use any nuggets or third party libaries. So the Community.Tasks are not an option. Obivously I found a solution with this nugget for my problem.
Now to my final solution:
I was looking hows the Community.Task.Nugget is working and built my own solution You can declare inline Tasks in your csproj where you can write C# Code in it.
<UsingTask TaskName="MyCustomTask" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> //When using .NETCORE use here ...Tasks.Core.dll and change TaskFactory to RoslynTaskFacotry
<ParameterGroup>
<MyParam ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Code Type="Fragment" Language="cs">
<![CDATA[
// My C# Code comes here!
// You can use your params here with Name like Console.WriteLine(MyParam)
]]>
</Code>
</Task>
</UsingTask>
Now you can use it like a normal Task in your .csproj
<Target Name="foo">
<MyCustomTask MyParamter="Test" />
</Target>
I solved my problem with coding a custom Task which can add/replace the AssemblyInformationalVersion inside the AssemblyInfo.cs before the build.
Upvotes: 1
Reputation: 23818
Set AssemblyInformationalVersion in AssemblyInfo.cs over msbuild Parameters
First, you should note that MSBuild properties like $MyInfoText
can only be used in xxxx.xxproj
file and in your situation, it can only be used in myProject.csproj
file rather than the cs file.
To realize your requirements, you can try to use a target which is equivalent to redeploying the parameters in the assembly.cs file in MSBuild. Based on it, you can override its properties from the command line in MSBuild. Only the properties in the XML file can be overridden by the values specified on the command line of msbuild.
Solution
1) install a nuget package called MSBuildTasks into your project.
2) please import MSBuild.Community.Tasks.Targets
which exists under the lib folder of th nuget package. For me, l add it in the xxxx.csproj
file like this on the top of the file:
<Import Project="C:\Users\Admin\source\repos\ConsoleApp\packages\MSBuildTasks.1.5.0.235\tools\MSBuild.Community.Tasks.Targets" />
3) Besides, add these:
<PropertyGroup>
<ProductName>$(AssemblyName)</ProductName>
<CompanyName></CompanyName>
<Version>1.0.0.0</Version>
<MyInfoText>xxx</MyInfoText>
</PropertyGroup>
<ItemGroup>
<AssemblyVersionFiles Include="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs" />
</ItemGroup>
<Target Name="AssemblyVersion" Inputs="@(AssemblyVersionFiles)" Outputs="UpdatedAssemblyVersionFiles" BeforeTargets="Build">
<Attrib Files="%(AssemblyVersionFiles.FullPath)" Normal="true" />
<AssemblyInfo
CodeLanguage="CS"
OutputFile="%(AssemblyVersionFiles.FullPath)"
AssemblyProduct="$(ProductName)"
AssemblyTitle="$(ProductName)"
AssemblyCompany="$(CompanyName)"
AssemblyCopyright="Copyright $(CompanyName), All rights reserved."
AssemblyVersion="$(Version)"
AssemblyFileVersion="$(Version)"
AssemblyInformationalVersion="$(MyInfoText)">
<Output TaskParameter="OutputFile" ItemName="UpdatedAssemblyVersionFiles" />
</AssemblyInfo>
</Target>
4) After that, you can use msbuild myProject.csproj /p:MyInfoText=xxxxxx
to overwrite it.
Update 1
Since you do not want to install this nuget package, I suggest you could move its content from the tool folder into your project so that it will be a part of your project.
1) create a folder called newtool under your project
2) copy the content of the tool folder from the nuget package into the newtool folder in your project.
3) change to use this import tag in your xxx.csproj
file
<Import Project="$(ProjectDir)newtool\MSBuild.Community.Tasks.Targets" />
Then you can realize it and be much more convenient.
Hope it could help you.
Upvotes: 3