Comanche

Hi,

I'm a complete newbie in MSBuild, so maybe it's a silly question.

I wanna make a simple thing: build C# solutions (created in VS2005) from command-line in either Debug or Release configuration. This can be done in 2 ways: via csc.exe and via msbuild.exe. Which way to choose What must my choice depend on I mean which aspects should I take into account

Thank you in advance!



Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

Simon Dahlbacka

you're comparing apples to oranges here..

csc.exe is the actual compiler, and doesn't know anything about solutions, projects nor configurations.

So while you *can* do it manually with csc.exe, you're in for a lot of extra unnecessary work. You will want to use msbuild.exe (which directly can consume visual studio project files)






Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

Comanche

Yes, you're absolutely right - it was a silly question! but before I always used to build my projects and solutions from under Visual Studio 2005, so I got very little idea about the above mentioned two executables. I thought that csc.exe also accepts csproj- and sln- files as command line arguments, so that's where my question comes from.

I wonder also, whether I can change AssemblyVersion from command line when building via MSBuild.exe. I need to change it for every project inside a solution when building that solution. Of course I can temporarily change contents of the Assembly.cs files, build solution and then restore original Assembly.cs files, but it's a bad way I guess. Is there any other, more elegant way to solve this problem





Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

sluggy

Of course I can temporarily change contents of the Assembly.cs files, build solution and then restore original Assembly.cs files, but it's a bad way I guess. Is there any other, more elegant way to solve this problem

Why would you want to change the version only temporarily A build is a build, regardless of whether it succeeds or not, if you do a build then you should change your build number permanently. Don't get me wrong, you can do as you please with your build, but what you are proposing would be unusual.

To specify a version number from the command line, you need to set up a property to hold the version passed in, let's call it newVersion. Then as one of your tasks, you can have a task to retrieve the version from the assembly.cs unless the newVersion property has already been set. So, in pseudo code, you want this:

Code Snippet

<PropertyGroup>

<newVersion></newVersion>

</PropertyGroup>

<Target Name="getNewVersioNumber" Condition="'$newVersion' = ''">

<!-- do whatever to get the old version and increment it -->

</Target>

With this, the getNewVersionNumber target will only be executed if newVersion is empty, i.e. hasn't been set from the command line. To set newVersion from the command line, use the /property: switch, like this:

MSBuild mySolution.sln /tTongue TiedtartTarget /p:newVersion=1.2.3.456

The usual reason for injecting the version number in this way is if you have a nested build of a component that you want to have the same version number as the main build - but you would normally still leave it stamped in the asembly.cs file.





Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

sluggy

Bleh, that command line should be:

Code Snippet

MSBuild mySolution.sln /t:startTarget /p:newVersion=1.2.3.456

In the previous post the ": s" was changed to an emoticon...





Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

Comanche

Thank you for the answer!

So, if I understand properly, the only way is to manually edit the csproj-files !

One thing more:

For me it doesn't make big difference whether to start MSBuild.exe with Process.Start("...\msbuild.exe", "[arguments]") or to use the Microsoft.Build.BuildEngine namespace. So maybe it's better to choose the 2nd way I just wanna find a solution where it's not necessary to change the csproj-files manually!





Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

sluggy

No, you can do this without having to change the csproj files - just make your own simple proj file with about five targets in it which:

- gets the solution from the source repository

- creates an item list by doing a search for all assembly.cs files

- batch over that item list, opening each assembly.cs and modifying the version

- the final task builds the solution

If you obtain the files from a source repository you achieve your mission of not permanently altering the assembly.cs files because you won't be checking them back in. The task to create the item list will look something like this:

Code Snippet

<Target Name="buildAssemblyInfoList">

<CreateItem Include="$(buildLocation)\**\assembly.cs" >

<Output ItemName="AssemblyList" TaskParameter="Include" />

</CreateItem>

</Target>

and the task to batch over that list to open the files and replace the version number will look something like this:

Code Snippet

<Target Name="setVersionNumbers" Condition="'$(newVersion)' = ''">

<MyCustomVersionTask FilePath="%(AssemblyList.Identity)" Version="$(newVersion)" />

</< FONT>Target>

I left out the code to actually replace the version as i tend to use a custom task for this (it loads the file, and uses a regex to find and replace the version, then saves it again). This is a pretty simple proj file, an experienced person might whack it together in half an hour, a newbie might take a couple of hours.





Re: Visual Studio MSBuild Command-line building: use csc or msbuild?!

Comanche

Thank You!

In the long run I choose the following way:

1). Parse SLN-file with RegExps and retrieve list of projects.

2). For each project in the list:

2.1). Search for AssemblyInfo.cs file under the project's folder.

2.2). Create its backup copy.

2.3). Change "AssemblyVersion" and "AssemblyFileVersion" params inside this file to the value I need.

3). Run MSBuild.exe passing SLN-file to it.

4). For each project in the list:

4.1). Restore backup copies of the AssemblyInfo.cs files.