Listing contents of a directory using msbuild - msbuild

I'm trying to debug an msbuild script that is being run on our TFS build agent. I have no permissions to the build agent server.
Is there a way I can add some debugging to my msbuild script ("Message" or similar) to output a listing of all the files in a given directory?

Hope this helps to list all the files in the folder where the MSBuild files are there recursively:
<Target Name="Listing">
<ItemGroup>
<PackageFiles Include="$(MSBuildProjectDirectory)\**\*.*;"/>
</ItemGroup>
<Message Text="%(PackageFiles.FullPath)"/>
</Target>

Related

MSBuild ClickOnce app: FTP Upload files after build

I am using MS build to publish my ClickOnce app, in Visual Studio 2017 developer command line to a remote ftp site.
How do I initiate the FTP file upload that starts in Visual Studio after build? This is my command line, which builds the project:
msbuild /target:publish -property:Configuration=Release /p:PlatformTarget=x86 "%USERPROFILE%\VSProjects\IIC\IIC.UI.vbproj"
There are 2 problems with command line Click Once deployment: (1) Auto Incrementing option from the publish property page of a project is not honored from the command line, and (2) the subject of the question, starting the FTP upload to the remote site.
Solution (with Visual Studio 2017 Developer Command Prompt v15.7.3)
Download community ms build tasks from here: https://github.com/loresoft/msbuildtasks
Unload the project and open with notepad++ or your editor of choice and import the community ms build tasks. Follow the instructions on their github page.
Add file ProjectName.version.txt with just one line with the version information of your project. For example:
1.2.78.1341
The numbers correspond to {Major}.{Minor}.{Build}.{ApplicationRevision}
Add the following target to the bottom of the project, which uses the Version and FileUpdate community tasks:
<Target Name="beforePublishCmd">
<Message Text="revision before: 3.0.0.$(ApplicationRevision)"/>
<Version VersionFile="ProjectName.version.txt" BuildType="Automatic" Major="3" Minor="0" Build="0" RevisionType="Increment">
<Output TaskParameter="Major" PropertyName="Major" />
<Output TaskParameter="Minor" PropertyName="Minor" />
<Output TaskParameter="Build" PropertyName="Build" />
<Output TaskParameter="Revision" PropertyName="ApplicationRevision" />
</Version>
<Message Text="revision after: 3.0.0.$(ApplicationRevision)"/>
<FileUpdate Files="ProjectName.vbproj"
Regex="<ApplicationRevision>(\d+)"
ReplacementText="<ApplicationRevision>$(ApplicationRevision)" />
</Target>
Call the above target from the command line BEFORE calling the publish target, like so:
msbuild /target:beforePublishCmd -property:Configuration=Release /p:PlatformTarget=x86 "%USERPROFILE%\VSProjects\ProjectName.vbproj"
Add an "afterPublish" target, which uses the FtpUploadDirectoryContent community task. This target is automatically called after the publish target finishes.
<Target Name="afterPublish">
<PropertyGroup>
<CurrentDate>$([System.DateTime]::Now.ToString(yyyy MMM dd HH:mm:ss))</CurrentDate>
</PropertyGroup>
<FtpUploadDirectoryContent
ServerHost="projectname.org"
Username="*****"
Password="*****"
LocalDirectory=".\bin\Release\app.publish"
RemoteDirectory="/"
Recursive="true"
/>
<Exec Command="C:\Progra~1\TortoiseSVN\bin\svn commit ..\ --non-interactive --message "Release 3.0.0.$(Revision) on $(CurrentDate): $(commitMessage)""/>
</Target>
Finally, call the publish target from the command line:
msbuild /target:publish -property:Configuration=Release /p:PlatformTarget=x86 "%USERPROFILE%\VSProjects\ProjectName.vbproj"
MSBuild ClickOnce app: Upload files after build
You can add a copy task into your project file to upload files after build:
To accomplish this, unload your project. Then at the very end of the </project>, just before the end-tag, place below scripts:
<ItemGroup>
<UploadFiles Include="ThePathOfYourUploadFiles\*.*"/>
</ItemGroup>
<Target Name="AfterBuild">
<Copy
SourceFiles="#(UploadFiles)"
DestinationFolder="PathWhereYouWantTouploadYourUploadFiles"
/>
</Target>
With this target, Visual Studio/MSBuild will upload the files after build.
Hope this helps.

Linked files location on .Net core in Debug vs Publish

I have a shared.{Environment}.json file that is added as linked files in several .Net core 2.1 projects. When project is build or published file gets copied to output directory, in case of release its fine but on debug it doesn't work as when project run it looks up for that file in project directory not in output directory.
Whats the proper way to solve this issue for both debug and publish?
For linked files, it will not exist under the project directory.
For a workaround, you could try to copy the file with task in csproj like below:
<ItemGroup>
<Content Include="..\MVCPro\shared.{Environment}.json">
<Link>shared.{Environment}.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
<Copy SourceFiles="..\MVCPro\shared.{Environment}.json" DestinationFolder=".\" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
</Target>

web deploy copy a file from drop location to project

I have a console project in my solution.
Now I want that exe of that project should be added to my main project post deployment using web deploy.
How can I achieve that?
Regards,
Gautam
There are two high level solutions for this:
Option 1: Copy the file into App_Data
You can copy the exe into the App_Data folder as part of a post build event or as part of the script below. It's your choice.
Now that it's there, we have another problem. The WPP only includes files that are part of the project when it deploys. To get around this, you can create a WebProjectName.wpp.targets file to the root of the web application with the following contents:
<Project>
<PropertyGroup>
<BeforeAddContentPathToSourceManifest>
$(BeforeAddContentPathToSourceManifest);
IncludeExeInDeployment;
</BeforeAddContentPathToSourceManifest>
</PropertyGroup>
<Target Name="IncludeExeInDeployment">
<Copy SourceFiles="$(WebPublishPipelineProjectDirectory)\App_Data\Console\*"
TargetFolder="$(WPPAllFilesInSingleFolder)\App_Data\Console" />
</Target>
</Project>
(You could just as easily skip the interim step and copy the exe from it's original home into the $(WPPAllFilesInSingleFolder) folder)
Option 2: Include the exe as a separate provider
This one requires a bit more understanding of msdeploy, but gives you the option to deploy the exe wherever you want on the target server.
Basically it involves adding an additional dirPath provider in the deployment. Again, add a wpp.targets file in the root:
<Project>
<PropertyGroup>
<AfterAddContentPathToSourceManifest>
$(AfterAddContentPathToSourceManifest);
IncludeConsoleAppInDeployment;
</AfterAddContentPathToSourceManifest>
</PropertyGroup>
<Target Name="IncludeConsoleAppInDeployment">
<ItemGroup>
<MsDeploySourceManifest Include="dirPath">
<Path>full path to console directory</Path>
</MsDeploySourceManifest>
</ItemGroup>
</Target>
</Project>
You'll also need to replace the path in your pubxml to specify where the exe will go on the far end:
<ItemGroup>
<MsDeploySetParameters Include="ConsoleAppPath">
<Kind>ProviderPath</Kind>
<Scope>dirPath</Scope>
<Match>regex that matches console directory</Match>
<Value>Path to console application on remote server</Value>
</MsDeploySetParameters>
</ItemGroup>

Stopping Post Build events on project when building directly from MSBuild

I have a project which has some post build events that do some copying around for other projects. I unfortunately cannot change that, and have been asked to write a build script for use on a CI server.
Problem is that the post build steps run off the debug/release bin folders and I compile through the build script to a different folder. So one solution would be to let the project build as is, and then manually copy all files from the bin folders to the output folder I am using. However that feels like a bit of a hack, so I was wondering if there is a way for an MSBuild task to tell the solution it is building to ignore PostBuild events, I believe you could set a property PostBuildEvent='' but it didnt seem to stop them from happening...
Here is an example of the build script target:
<Target Name="Compile" DependsOnTargets="Clean;">
<MSBuild Projects="$(SourceDirectory)\SomeSolution.sln"
Properties="Configuration=Release; OutputPath=$(CompilationDirectory); PostBuildEvent=''" />
</Target>
Anyone had to do anything similar before?
To disable all PostBuildEvents, set the CustomAfterMicrosoftCommonTargets to C:\PostBuild.config (or whatever you name the file) and have PostBuild.config to be:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PostBuildEvent"/>
</Project>
Add /p:CustomAfterMicrosoftCommonTargets="C:\PostBuild.config" to your msbuild command line
Or update your MsBuild task properties:
<MsBuild Projects="$(ProjectTobuild)" Properties="Configuration=$(Configuration);Platform=$(Platform);CustomAfterMicrosoftCommonTargets='C:\PostBuild.config'" Targets="Build"/>
To disable PostBuildEvents at project level for MSBuild, simply put these codes inside .csproj:
<Target Name="BeforeBuild">
<PropertyGroup>
<PostBuildEvent Condition="'$(BuildingInsideVisualStudio)' == 'false' Or '$(BuildingInsideVisualStudio)' != 'true'"></PostBuildEvent>
</PropertyGroup>
</Target>

Change working directory of msbuild.exe

I am executing MSBuild from a batch file. The MSBuild script is in a different directory than the directory I want MSBuild to consider the working directory when running the script. When invoking MSBuild.exe, how do I change its working directory?
Edit: More details
Let's say I have an MSBuild script located on some other server. I want to run a command thusly:
msbuild.exe \\my_server\c$\My\Path\To\Scripts\TestScript.msbuild
I run that command with my command prompt at c:\temp. Let's say my TestScript.msbuild has a task to create a file. The file has no path just a filename. I would expect that the file gets created inside c:\temp. But it doesn't it gets created next to the msbuild file that is sitting on the server. This is the behavior I want to change.
Edit #2
Here is the script I'm using in my test:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Files Include="HelloWorld.txt" />
</ItemGroup>
<Target Name="TouchFiles">
<Touch Files="#(Files)" AlwaysCreate="True" />
</Target>
</Project>
I am going into a command shell CDing into c:\temp and then executing the script. With or without the /p:OutDir switch that #Nick Nieslanik mentions, the HelloWorld.txt file appears in the folder where the *.msbuild file is and not c:\temp.
I ran across this while looking for a solution to my problem. Here's my solution (build script):
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Default">
<Exec Command="build.bat" WorkingDirectory="..\[your dir]\" />
</Target>
</Project>
I believe that's more what you were originally looking for?
My problem was that my batch file called another that it expected to be in the same directory, but since my ms build script was being run elsewhere, the batch file failed to find the second batch file.
#jkohlhepp - I see now. You are doing the opposite of what I described in my comment to some degree.
MSBuild common targets use the MSBuildProjectDirectory to determine the output folder unless you override that. So in your case, you could run
msbuild.exe \\my_server\c$\My\Pat\To\Scripts\TestScript.msbuild /p:OutDir=c:\temp
to force the output to be dropped in that location.
EDIT:
Given the project file above, you'd need to edit it to do something like the following for this to work:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutDir Condition=" '$(OutDir)' == '' ">bin\debug\</OutDir>
</PropertyGroup>
<ItemGroup>
<!-- Without prefacing files with paths, they are assumed relative to the proj file -->
<FilesToCreate Include="$(OutDir)HelloWorld.txt" />
</ItemGroup>
<Target Name="TouchFiles">
<Touch Files="#(FilesToCreate)" AlwaysCreate="True" />
</Target>
</Project>
In current versions of MSBuild the well-known property MSBuildStartupDirectory can be used in the msbuild file to retrieve the absolute path of the folder where MSBuild is called.
https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties?view=vs-2019
This option perhaps did not exist in msbuild around the time when the question was asked. I didn't want to spend too much time investigating it.