How can I run a bat file after project has been published to folder in Visual Studio 2022 - visual-studio-2022

I need to run a bat file after I have published my project using Visual Studio 2022. After searching the web I found that this should be able to be done by modifying the csproj file. So I added the following line to this file and run th publish command. I dont get any errors but the bat file I try to execute is not run. Is there anyone that know if this is actually supported?
<Target Name="ExecuteBatAfterPublish" AfterTargets="AfterPublish">
<Exec Command="D:\postbuild.bat"/>
</Target>

Related

How to restore only a specific solution folder with an MSBuild command

I have more than hundred projects in two solution folders.
How can I build a specific solution folder with the msbuild command in CLI, like the same thing in Visual Studio with right click on a specific solution folder and click on Build?
I use Visual Studio 2019 16.8.3
Update:
I found the Build a solution folder with MSBuild Stack Overflow question, but as you can see in this question, anyone does not provide a built-in solution for restore or build or any other target run in a specific solution folder. I hope to find a way to run the target on a specific solution folder with a built-in way using MSBuild.
Just open Developer Command Prompt for Visual Studio and then type these to build the specific .sln file like this:
msbuild xxx\xxx.sln -t:build -p:Configuration=Debug
The solution folder from Solution Explorer is a virtual folder for the Visual Studio IDE. If you want to use an MSBuild command line, you have to create such a real folder. Otherwise, there isn't any such way.
1) Open Solution Explorer → Switch View, create a real folder (on my side, it is called test), and then drag the related projects into the folder.
2) create a file called test.proj on the solution folder. And then add these on that file:
<Project>
<ItemGroup>
<File Include="test\**\*.csproj"/>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="#(File)" Targets="Build"/>
</Target>
</Project>
3) use command line msbuild test.proj to get what you want.

Publish web project from JetBrains Rider

I am giving Rider a try, and so far, quite like it.
One feature I use in Visual Studio quite often is right click on a web project and publish to our testing server.
I cannot find a similar option in Rider, so what I have done is, create a run configuration, with the following settings:
Exe path: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/Bin/amd64/msbuild.exe
Arguments: MySolution.sln /m /p:DeployOnBuild=True /p:PublishProfile=My-Project "/p:platform=Any CPU" /p:configuration=Release /p:VisualStudioVersion=15.0 /p:Password=****
Working Directory: C:\SolutionFolder
When I want to publish, I select it from the drop-down and click run.
This works 100%.
My question is, is this the best way to do it, sans setting up a CI pipeline? Am I missing an option or setting in the IDE?
As of June 2018, Rider doesn't have UI for publishing.
There is a feature request which you can vote for, once logged in YouTrack.
As a workaround, one can create a '.NET Executable' configuration like you did, and run it when you want to publish your project.
More detailed instructions follows:
Run > Edit Configuration
Add new configuration > .NET Executable
Name = your project name
Exe path = path to your MSBuild (for example C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/Bin/amd64/MSBuild.exe)
Program arguments = YourSolution.sln /t:"Your_Project" /p:PublishProfile=YourPublishProfile.pubxml /p:Configuration=Debug /p:DeployOnBuild=true /m
Working directory = C:/path/to/solution/dir/
Notes:
the project publish profile is usually located in the project folder, under Properties/PublishProfiles. If you don't have one you can start with the example reported below;
you need to replace the dots (.) in the project name with underscores (_). In the example above Your.Project was passed as Your_Project;
you can specify a different publishing directory, if not already specified in the publish profile, by adding the argument /p:PublishDir="C:/path/to/publish/dir/";
if you don't have Visual Studio installed on your machine, you can use the MSBuild bundled with the Build Tools for Visual Studio 2017.
Example of publish profile:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>..\YourPublishDirectory</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
<ExcludeFilesFromDeployment>bin\*.dll.config</ExcludeFilesFromDeployment>
</PropertyGroup>
</Project>
Latest versions of Rider support publishing via UI. If you don't have Visual Studio installed on your machine, make sure the web project has Build.Microsoft.VisualStudio.Web.targets nuget package installed.

How to change msbuild working directory in TFS 2013 workflow

I have a TFS 2013 build xaml workflow, that eventually calls the Microsoft.TeamFoundation.Build.Workflow.Activities.MSBuild activity once for each solution that I want to build. When msbuild.exe is called, it's working directory is the working directory of the current solution being built. I can see this through the 'MSBuildStartupDirectory' property when running msbuild with a 'diagnostic' verbosity.
Unfortunately, I need the working of msbuild.exe to be somewhere else when msbuild.exe starts. This is because I use the MSBuild SonarQube runner that imposes constraints on the directory from which msbuild is called.
I have looked at the 'msbuild' activity and there is no way to control the working directory. Is there another way to control the working directory of this activity?
Its been a while since I edited a build process template but I believe you could use an activity that just executes a command in CMD and provide the full MSBuild command. I'm sure there are tons of variables you will need to setup for this to work.
Instead of editing the build process template have you considered using a PowerShell script in the Post-build script to execute SonarQube?
I still haven't found any way to control the working directory of msbuild. But since I know that the working directory will be the directory of the project being built by msbuild, I created a new proj file at the root of my workspace (where my working directory has to be) and only build this new proj file from my workflow. This new proj file then builds all my other solutions. That way, my working directory is the same for all the solutions being built.
Here is an example of my top level proj file:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<ItemGroup>
<Solutions Include="**\*.sln"/>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="#(Solutions)" Targets="Build"/>
</Target>
</Project>
But beware that doing this may affect the output directory (OutDir) given to each solution. So you may want to do something like this:
<MSBuild Projects="#(Solutions)" Targets="Build" Properties="OutDir=$(OutDir)..\%(Solutions.Filename)"/>

In MSBUILD, how can you specify a condition that check whether command line or VS launched it?

I have a csproj that I would like to have trigger the opening of a particular file in Visual Studio, only if the target was executed from within Visual Studio, but not from the MSBUILD command line. How do I do this?
Quote from MSDN page:
When building inside Visual Studio, the property $(BuildingInsideVisualStudio) is set to true. This can be used in your project or .targets files to cause the build to behave differently.
Example how it could be used in your .*proj or .targets file:
<PropertyGroup>
<MyProperty Condition="'$(BuildingInsideVisualStudio)' == 'true'">This build is done by VS</MyProperty>
<MyProperty Condition="'$(BuildingInsideVisualStudio)' != 'true'">This build is done from command line of by TFS</MyProperty>
</PropertyGroup>
Add a property to the .csproj project file, example:
<PropertyGroup>
<FromMSBuild>false</FromMSBuild>
</PropertyGroup>
Then in the task you want to run, put a condition that evaluates that property. For example, i f you want to open notepad.exe whenever the build is executed from command line and NOT visual studio:
<Target Name="BeforeBuild">
<Exec Command="C:\Windows\Notepad.exe" Condition="$(FromMSBuild)" />
</Target>
Of course, this is dependent on setting the $(FromMSBuild) property correctly when you run the build via command line, like so:
MSBuild myProject.csproj /p:FromMSBuild=true
If I understand you correctly, you want to open a file when building in visual studio but not from command line with MSBuild?
If that is the case, specify a PreBuild or PostBuild in Visual Studio.
Right click on the project in the solution explorer and select Properties
Select the Events tab
Add either a Pre or Post Build event to open the desired file

Weird behavior with $(SolutionDir) with msbuild in a .csproj file being wrong

I have a .sln solution file that references a .csproj project file that has an after build task of something like:
<PropertyGroup>
<PostBuildEvent>
xcopy $(SolutionDir)\dir1\Somefle.xml $(ProjectDir) /Y /I
</PostBuildEvent>
</PropertyGroup>
The solution is built using msbuild with a task like the following:
<Target Name="CompileSolution">
<MSBuild Projects="#(SolutionToBuild)" Targets="Rebuild" Properties="Platform=Any CPU" />
</Target>
Now here's the strange part:
If I:
run the build script (say c:\MyWorkingCopy)
rename the working copy folder (say to c:\YourWorkingCopy)
run the build script again
On step 3, the xcopy will fail, because it will because it will be trying to copy the file from "c:\MyWorkingCopy" - which of course is not where the solution file now resides.
Why does msbuild use the old Solution directory? And is there some way to reset it?
(I am using .NET Framework 3.5)
It may be related to the sln.cache file that is created by msbuild when you build a sln file (it's a temporary proj file built from the sln one), if it is present or if the sln is not modified the sln.cache file may be used... I don't really know but it I think it could help.