How to pass parameter to MSBuild from Visual Studio? - visual-studio

My issue is simple (or it is supposed to be so)
I want to pass some parameter to MSBuild From Visual Studio, Not from the command line
Put it in another way, How can I execute the following command when I click Build project from Visual Studio
msbuild myproject.csproj -p:CustomParameter=ItsValue
Thank you

On Visual Studio, you only have to modify the proj file of the project. And this special and simple assignment is only a unique way of msbuild command line.
So you have to right-click on your project on the Solution Explorer-->Unload Project-->then right-click on the unload project-->Edit Project File,
add this at the bottom of the proj file:
<PropertyGroup>
<CustomParameter>ItsValue</CustomParameter>
</PropertyGroup>
Every time you want to change the custom value, you have to modify the property on the proj file.
This is the only way and actually, it is indeed not as convenient as the command line.
============================================
Update 1
1) add a new file called test.props file on the project folder
then add these content on the on the test.props file:
<Project>
<PropertyGroup>
<CustomParameter>ItsValue</CustomParameter>
</PropertyGroup>
</Project>
2) modify your proj file with this:
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="test.props"/>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<Target Name="test123" AfterTargets="Build">
<Message Importance="high" Text="$(CustomParameter)"></Message>
</Target>
</Project>
3) every time you should open test.props file, change the value of CustomParameter,save, click Build button, you will get what you want.
So far, this is by far the easiest way.

Related

Limit custom MSBuild target to the file(s) selected in Visual Studio

I have a small custom MSBuild target:
<ItemGroup>
<Foo Include="file1.foo"/>
<Foo Include="file2.foo"/>
<Foo Include="file3.foo"/>
...
</ItemGroup>
<Target Name="Foo2Bar" Inputs="foo.config;#(Foo)" Outputs="%(Foo.Filename).bar">
<Exec Command="path\to\script #(Foo) -o %(Foo.Filename).bar"/>
</Target>
Now I want to be able to to select one of the *.foo-files in Visual Studio and trigger processing just this one (e.g. by pressing CtrlF7 or right clicking in the Solution Explorer -> Compile). It seems that this sets a property $(SelectedFiles), but I don't find any usable tutorial or example how to make this work...
Limit custom MSBuild target to the file(s) selected in Visual Studio
If I understand you correctly, you want to select one of *.foo file to the target, if yes, you can pass the list around as a property, so we need to convert item into a property:
<Target Name="BuildMigrationZip">
<PropertyGroup>
<FooProperty>#(Foo)</FooProperty>
</PropertyGroup>
<MSBuild Projects="$(MSBuildThisFile)" Targets="Foo2Bar"
Properties="FilesToFoo=$(FooProperty)" />
</Target>
Then when we build the this file with MSBuild command line, we could pass the property FooProperty:
msbuild.exe "YourCustomTargetFile" /p:FooProperty=file1.foo
You can check this thread for some more details info.
If I understand you incorrect, please let me know for free, I will keep follow ASAP.
Hope this helps.

MSBuild project in Visual Studio

I Have a question about msbuild integration with Visual Studio 2017 Preview.
I have a custom *.csproj:
<Project>
<Target Name="Build">
<Message Text="Hello World" Importance="High"/>
</Target>
</Project>
I want to add this project to Visual Studio, but i have an error:
Project file is incomplete. Expected imports are missing
What i need to add to my custom *.csproj to get my project working in Visual Studio?
First, I suggest you to create a project from Visual Studio or from dotnet cli, and then add your custom target to csproj file.
Anyway, I think that the minimal csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
</Project>
This is the template from dotnet cli, of course, it depends on your project type (Console, ClassLibrary)

Create proj file that loads csproj files into solution explorer when opened with visual studio

I am building a project file for our application that I am going to execute from our build machine. I was wondering if it is possible for me to open the project file and get the same view visual studio gives me of the solution when I open a solution file.
So here is my Contosa.proj file so far.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)'=='' ">Debug</Configuration>
<RootNamespace>Contosa</RootNamespace>
<AssemblyName>Contosa</AssemblyName>
</PropertyGroup>
<ItemGroup>
<Projects Include="C:\Users\localuser\Documents\Perforce\Contosa\Branches\Working23\UI\Desktop\ContosaClient\ContosaClient.csproj" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="Build">
<PropertyGroup>
<Contosa>$(MSBuildProjectDirectory)\UI\Desktop\ContosaClient\ContosaClient.csproj</Contosa>
</PropertyGroup>
<MSBuild Projects="$(Contosa)"
Properties="Configuration=QA;
VisualStudioVersion=12.0;
DevEnvDir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\;
SolutionDir=$(MSBuildProjectDirectory)\"/>
</Target>
</Project>
Currently double clicking this file with visual studio as its associated application will open the Contosa.proj for text editing. I would really like it to be possible to associate my Contosa.proj file with visual studio like our Contosa.sln file is. So when developers open it with visual studio they get the same view that you get from the Contosa.sln. I don't understand what parts of a csproj or a sln file make them open as projects or solutions in visual studio.
UPDATE 1
I am looking to do what this user Replace .sln with MSBuild and wrap contained projects into targets did but I want the project file to be able to opened by the user like a solution file. I want the Projects I include to be loaded into the solution explorer.
When visual studio installs, it configures explorer to launch visual studio when a .csproj is doubleclicked (How can I set a file association from a custom file type to a program). Your windows, and your developers windows, don't know what a .proj is. So rename it .csproj or distribute a .sln which references .proj

How do I use an MSBuild file from Visual Studio 2012?

I have a simple MSBuild file that I'm learning with.
Here it is:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Clean" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>{D5A16164-962E-4A6D-9382-240F31AB6C50}</ProjectGuid>
</PropertyGroup>
<Target Name="Clean">
<ItemGroup>
<BinFiles Include="bin\*.*" />
<fff Include="f\*.*" />
</ItemGroup>
<Delete Files="#(BinFiles)" />
<Delete Files="#(fff)" />
</Target>
</Project>
Now I want to include this in a Visual Studio solution and be able to run the "clean" target from Visual Studio 2012. I tried naming it testproject.msbuildproj like the internet seems to suggest "works", but it doesn't work. When I run the clean command I just get "unexpected error".
If I rename the project to testproject.csproj, it does some unintuitive things like creating compilation directories, but it does actually run my clean command properly. However, this is undesireable because it creates obj and bin/x86/debug type directories. It also looks goofy in Visual Studio because it still gives the References drop down.
How can I use just a plain vanilla MSBuild project from Visual Studio without random errors or false assumptions?
Note I only am having a problem with this from Visual Studio. Using msbuild from the command line it works perfectly
Visual Studio creates bin / obj folders when it opens csproj file. When you click Build / Rebuild / Clean it just uses appropriate targets from the project file.
You cannot stop VS from creating these folders, but you can ask it to create them in say temp folder by setting appropriate properties - refer this MSDN article for details.
So the steps are to rename your project to csproj, and add the following lines into project:
<PropertyGroup>
<OutputPath>$(Temp)\bin</OutputPath>
<IntermediateOutputPath>$(Temp)\obj</IntermediateOutputPath>
</PropertyGroup>
I usually use a bit different approach to work with MSBUILD files from VS:
I use regular csproj file with removed Import ... CSharp.targets part as pure container for my Build projects.
I add actual build files with targets and logic, and all properties, necessary artifacts like XSLT etc using "Include into project", so I can manage hierarchy and change any file from within VS.Net.
I redefine Build / Rebuild targets in csproj file for whatever I need, for example Build may contain minimum output, and while rebuild diagnostic one.
Like this:
<Target Name="Build">
<Exec Command="%windir%\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe Builds\build.proj /t:Build /v:m" />
</Target>
<Target Name="Rebuild">
<Exec Command="%windir%\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe Builds\build.proj /t:Build /v:d" />
</Target>

Calling TransformWebConfig on a solution file

I have a solution with multiple projects in it one of these projects is a web application and I would like to be able to transform the web.config using web.release.config when building the solution using MSBuild.
When I call
MSBuild "WebProject.csproj" /t:TransformWebConfig
/p:Configuration=Release
on the web project I get the transformed web.config output to ...\obj\Release\TransformWebConfig\transformed\ Web.config
but when I try to call
MSBuild "Solution.sln" /t:TransformWebConfig
/p:Configuration=Release
I get an error that "TransformWebConfig does not exist in the project". Is there a you to get this to work so it will output a transformed web.config if possible and ignore the TransformWebConfig part of the command otherwise?
The easiest way to do this seems to be to change the web projects .csproj file to
<Project ToolsVersion="4.0" DefaultTargets="BuildWithConfig" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
And then add a new target
<Target Name="BuildWithConfig">
<CallTarget Targets="Build"/>
<CallTarget Targets="TransformWebConfig"/>
</Target>
And then run
MSBuild "Solution.sln" /p:Configuration=Release
And the file will be output to ...\WebProject\obj\Release\TransformWebConfig\transformed

Resources