Is it possible to set MSBuild properties from within Visual Studio? - visual-studio

I'm contributing to a fairly large project that uses MSBuild properties to control the build process. I am running some tests from the command line like this:
msbuild /p:Configuration=Release /p:OSGroup=Windows_NT /p:Performance=true
And everything is working fine. However, when I try to run the tests from Visual Studio, the OSGroup and Performance properties are not set, which causes things to not work correctly.
Any idea how I can set these properties before building my .csproj file within Visual Studio? It has to be without editing the .csproj file, since I don't to accidentally check in any changes I make there. Thanks in advance.

You could just add an external tool command which builds using the same command as shown. VS will parse the output just like from it's own builds, so warnings and errors are shown in the Error List etc. Your command would be something like
msbuild $(SolutionDir)$(SolutionFileName) /p:Configuration=Release /p:OSGroup=Windows_NT /p:Performance=true

You can implement a Visual Studio extension and access project properties trough DTE.Solution.Projects and Project.Properties. There is an example here:
https://msdn.microsoft.com/en-us/library/bb166172.aspx
VS DTE interface can also be used in a standalone application or Powershell script.

Related

Publish has different output when doing it from MSBuild

I am currently trying to set up a automated publish using MSBuild and am now realizing that it produces a different output when doing it from MSBuild instead of Visual Studio. I am not sure what I am missing here, but for some reason it is copying different project files into the route web project directory.
Is there a way to simulate a Visual Studio Publish using MSBuild? I am currently doing this with an Orchard Project, figured that would be worth mentioning.
Here is the command I am currently using to do this:
/p:PublishProfile="exampleprofile";DeployOnBuild=true;VisualStudioVersion=12.0;
FrameworkPathOverride="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v4.5";
PublishProfileRootFolder=%WORKSPACE%\src\Orchard.Web\Properties\PublishProfiles;
Password=ExamplePass;Configuration=Release
As far as I can understand, you're trying to simulate a ClickOnce publish using a manual msbuild routine. You can achieve that by calling msbuild with the correct parameters. To simulate the ClickOnce, the target publish is available for you.
msbuild MyProj.csproj /t:Publish
Given your specifications, you have to be able to run multiple publish configurations, each one having its own output settings. To be able to run multiple profiles, I would recommend that you abandon the PublishProfile attribute (I never understood how to get it to work) and switch to the BuildEnvironment as showed here :
https://wallism.wordpress.com/2009/12/21/msbuild-and-multiple-environments/
(Focus on the "Setting up the customizations" part)
You have to adapt your call to msbuild to include your build environment
msbuild MyProj.csproj /t:Publish /p:BuildEnvironment=MyConfig
Just for a little test, for you to know if this is useful, follow the tutorial, create your target, and add a
<PropertyGroup>
<PublishUrl> Add a custom path here </PublishUrl>
<InstallUrl> Add the same path here </InstallUrl>
</PropertyGroup>
to your target file.
Run then the msbuild and let me know if you solved your problem

What parameters does Visual Studio pass to MSBuild?

When Visual Studio runs a build, it runs MSBuild to do the majority of the work. If you pass the .sln file to MSBuild, with appropriate Configuration and Platform properties, it will build your solution similarly to how Visual Studio would.
msbuild mysolution.sln /p:Configuration=Release /p:Platform="Any CPU"
However, there are differences: sometimes a build will error through MSBuild and not through Visual Studio, or vice-versa.
What parameters does Visual Studio pass into MSBuild to influence the process? Is there some way to see the parameters it's passing as a build is is executed?
Visual Studio does not execute MSBuild.exe, but hosts the build engine itself (by means of the Microsoft.Build.* assemblies) - at least that is the case for the default C# project system. Other languages, addins, packages, etc. might do it differently.
Actually, there was a post series on the Microsoft blogs about this, I'm trying to find them and update this answer.
UPDATE: Found it again. Look for the "MSBuild in Visual Studio" posts here.
Concerning your original question, this page might help you further. Also you could go to "Tools", "Options", "Projects and Solutions", "Build and Run" and increase the MSBuild output verbosity. With "Diagnostic" you should basically see every property that is set upon starting the build.
First off, you can run msbuild with the /v:diag argument to get diagnostic-level logging. This can really help in figuring out why a build is failing.
Otherwise, yes, if you use Process Monitor, you can monitor for process start events where you can see the specific command-line sent to the process.
Specifically:
Run Process Monitor
Filter » Filter...
Operation is Process Create » Add
Operation is Process Start » Add
OK
Run your build through VS and through command-line msbuild
See the command-line arguments in the Detail column

Integrating MSBuild into Visual Studio

I'm a solo developer running Visual Studio 2008 and looking into MSBuild to improve my build process.
Almost all of the tutorials I've found so far have plenty of information about writing a build file. However I'm having a lot of trouble finding out how to integrate MSBuild into Visual Studio. Maybe MSBuild is only used with something like CruiseControl but that's overkill for me as a single developer.
Where should the build file live in a Visual Studio project and how can I run it from within the IDE?
Visual Studio executes MSBuild automatically for projects it supports.
If you right click on a project and unload it, you can then edit it in Visual Studio. Reload (right click on project again), force a (re)build to test your changes. An alternative is to edit the project file in an external editor and Visual Studio will detect saves and offer to reload the project for you.
Sounds like you're on the right track, and if you are considering writing Targets or custom MSBuild Tasks, take the time to separate them from your current project so that you can re-use them. Don't re-invent the wheel though, the two main complementary MSBuild projects are MSBuild Community Tasks and MSBuild Extension Pack.
Update: Judging from your comment on Mitch's answer, you might also want to consider adding a new Configuration element or custom properties to a project. A new MSBuild Configuration (something other than the default Debug/Release) could run unit tests, build documentation, or whatever you want automated. A custom MSBuild property would allow you to use normal Debug/Release Configuration and extend it to automate more of your build process, just depends on what you want. Either approach could also be driven from the command line.
As others have noted, MSBuild is already available when you install Visual Studio.
If you want to integrate into VS2008: Running MSBuild from Visual Studio
MSBuild is the build engine used by Visual Studio to process the files included in a project.The Visual Studio project files themselves (**.csproj* for C#, and .vbproj for VB, for example) are in fact MSBuild scripts that are run every time you build a project.
Your .csproj file is a MSBuild file. So you are actually using it already.
You may of course wish to create a separate build file to have more control, especially within a continuous integration or nightly build say.
If you simply wish to edit your project build file then you can use the IDE to edit some settings such as pre and post build actions or edit the Xml itself by unloading project and right click and editing.
You can use your current .vcproj files to build your project with MSBuild. However, as MSBuild is not directly supported (at least for vc++) vcbuild is used instead (internally).
In VS2010 all project files are MSBuild based...
This is an older article about some simple extension points from the msbuild team
How To: Insert Custom Process at Specific Points During Build
Also, don't forget you can use the MSBuild SideKick for developing and debugging your (local) msbuilds, available for free at http://www.attrice.info/msbuild/
I'd suggest you call msbuild as a post build step. Then you can put your build script somewhere in your solution and call it.
<windowsdir>\Microsoft.NET\Framework\v3.5\MSBuild.exe c:\temp\MyProject\mybuildfile.proj
The easiest way is probably to invoke your custom build script using a post-build step. Right click project, choose "Build Events" and call msbuild with your custom msbuild file from there.
I use the msbuild template to intergrate with visual studio
http://msbuildtemplate.codeplex.com/

Changing a Visual Studio project using command line switches

I know very little about visual studio so the answer to this one might be simple; but is it possible to change settings in the project using command line switches to avoid having different project files for minor changes ? As I want to trigger automatic builds changing settings through the ui is not possible.
For example something like /MyOwnSetting="something"
and then have the project file use that.
There is no command line switch that will modify the project file, If you want to have different setting the way to go is to use configurations.
Each project can have many configurations (Release/Debug for example) and when you compile a project file you can specify which configuration to build.
The last part of you question is a bit unclear.
Here are some Visual Studio Command Line Switches (for v2.0 ... see other links on that page for other versions of Visual Studio).

Problem building a Setup project

I'm working on a tool to simplify an application's deployment. Hence I'm aiming to automate the build of the setup project.
The Situation:
When I use Visual Studio to build the setup project this, creates the msi and exe files and concludes successfully. The problem occurs when I run a command in the command prompt, I keep getting this error "ERROR: Cannot find outputs of project output group '(unable to determine name)'"
The command for the command prompt is:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE>devenv "C:\Project's Directory\Project.Setup.vdproj" /Build
Can anyone help me with it.
I'm really stuck.
EDIT: The solution to my problem was to create a solution which contains the setup project and the project which is actually the output project of the setup project.
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE>devenv "C:\Project's Directory\Project.Setup.sln" /Build
Thanks to everyone.
You can't do this.
The issue is that you have defined your inputs to the setup project as outputs from the other projects in the solution. You have to build the entire solution to use this method.
You can do this but you have to manually link all the files you want into your setup project. It is a bit more tricky but just as doable.
I suspect that you have a setup project that is referencing the output of another project. in this case you need to use
devenv [solutionname] /build
the reason you are getting the error is because visual studio has not compiled the referenced project.
side note: if you are trying to automate a build in vs 2005 or later I would investigate wix as it is easier to automate using msbuild.

Resources