Implement BeforePublish as in AfterBuild - visual-studio-2010

I have two application config file (app.debug.config and app.production.config), I found this solution to copy the config file to output folder according to the current build configuration name:
<Target Name="AfterBuild">
<Delete Files="$(TargetDir)$(TargetFileName).config" />
<Copy SourceFiles="$(ProjectDir)\App.$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>
so after selecting for example the (Production) build configuration, the MSbuild will automatically copy the app.production.config and rename it to projectname.config in the output folder.
unfortunately it is not the same case for publishing,
because when I published the project to a web server, the configuration file is not published.
how can I do the same task for publishing?

I found out the solution, I added the following to the project file:
<ItemGroup>
<CustomConfigFile Include="$(ProjectDir)\App.$(Configuration).config">
<Visible>False</Visible>
</CustomConfigFile>
</ItemGroup>
<Target Name="BeforePublish">
<CreateItem Include="#(CustomConfigFile)" AdditionalMetadata="TargetPath=$(TargetFileName).config;IsDataFile=false">
<Output TaskParameter="Include" ItemName="_DeploymentManifestFiles" />
</CreateItem>
</Target>

Related

msbuild target - AfterTargets="Build" for the solution?

I am looking for a way to run my defined Target only once per build process and not for every project that gets build.
I have defined the following in Directory.Build.props
<Target Name="MyTarget" AfterTargets="AfterBuild" >
<Message Text="Hello World!!!" Importance="High" />
</Target>
Should only run once no matter how many projects the msbuild process is building, currently it happens for each project.
It shouldn't matter if I hit (Re-)Build Soltution or (Re-)Build [ProjectName] or hit F5 in Visual Studio, as long any build happens I want to exectue MyTarget only once.
Just answer this situation:
If my guess is right, pure msbuild function is not enough and you have to use a external file to help it work.
create a file called test.txt on the solution folder and write 0 in the txt file.
Then, modify your Directory.Build.props like this:
<Project>
<PropertyGroup>
<Record></Record>
</PropertyGroup>
<ItemGroup>
<File Include="..\test.txt"></File>
</ItemGroup>
<Target Name="GetConditionValue" BeforeTargets="PrepareForBuild">
<ReadLinesFromFile File="#(File)">
<Output TaskParameter="Lines" PropertyName="Record"/>
</ReadLinesFromFile>
</Target>
<Target Name="MyTarget" AfterTargets="Build" Condition="'$(Record)'=='0'">
<WriteLinesToFile File="#(File)" Lines="2" Overwrite="true"></WriteLinesToFile>
<Message Text="Hello World!!!" Importance="High" />
</Target>
</Project>
When you start a new build process, you should clear the test.txt file to 0 to make a new start.

How to mark files of a folder as embedded resource with wildcards with the new project format?

My scenario is simple.
I have test project where i want all files within a folder to be marked as embedded resource by default. To prevent someone from doing mistakes here i want this to be automatic through wildcards
I looked at this question, which looked very promising.
MSBuild: Include a custom resource file as embedded resource
However that does not seem to work with the new csproj format. Does anyone know what i should be doing different for it to work with the new format?
My current code is this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.2.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.2.1" />
</ItemGroup>
<Target Name="BeforeBuild">
<CreateItem Include="TestContent\*.cs">
<Output ItemName="EmbeddedResource" TaskParameter="Include" />
</CreateItem>
</Target>
</Project>
You can try this script:
<Target Name="MyCustomStep" BeforeTargets="BeforeBuild">
<CreateItem Include="TestContent\*.cs">
<Output ItemName="EmbeddedResource" TaskParameter="Include" />
</CreateItem>
</Target>
There exists difference between the BeforeBuild Target in old and new csproj format. (Or maybe the difference between .net core and .net framewrok, not sure about this point)
Some discoveries when I set the msbuild verbosity to Detailed:
1.For projects that target .net framework using the old csproj format:
The BeforeBuild target will exactly execute the CreateItem Task. So it works for old-format project files.
2.For projects that target .net core using new sdk format:
The BeforeBuild target seems not to execute the task as what we expected.
After defining the Custom target which executes before the BeforeBuild target, it works in my machine:

MSBuild: How to run custom target after _CopyFilesToPublishFolder?

I'm using Visual Studio 2013. I'm trying to publish a ClickOnce application from the command-line by passing /target:publish. However, I would like to do a few extra steps after MSBuild is done copying all the files to the publish folder. This is what I have come up so far:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildCommunityTasksPath>$(SolutionDir)\.build</MSBuildCommunityTasksPath>
<MSBuildCommunityTasksLib>$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.dll</MSBuildCommunityTasksLib>
<ClickOnceBuildDirectory>$(ProjectDir)\bin\app.publish</ClickOnceBuildDirectory>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets" />
<UsingTask TaskName="FileUpdate" AssemblyFile="$(MSBuildCommunityTasksLib)" />
<Target Name="CustomPostPublishActions" AfterTargets="PublishBuild" >
<!-- Create directory -->
<MakeDir Directories="$(ProjectDir)..\Deploy\Client\Application Files" />
<!-- Copy published website to deployment location -->
<ItemGroup>
<_CopyItems Include="$(ClickOnceBuildDirectory)\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(_CopyItems)" DestinationFolder="$(ProjectDir)..\Deploy\Client" />
<!-- Write publish.htm file for ClickOnce -->
<Copy SourceFiles="$(ProjectDir)\..\..\Build\publish.htm" DestinationFiles="$(ProjectDir)..\Deploy\Client\publish.htm" />
<FileUpdate Files="$(ProjectDir)..\Deploy\Client\publish.htm" Regex="{VERSION}" IgnoreCase="true" Multiline="true" Singleline="false" ReplacementText="$(ApplicationVersion)" />
</Target>
</Project>
Unfortunately, this is the order that the targets get run:
CustomPostPublishActions:
Creating directory "C:\MyProject\..\Deploy\Client\Application Files".
Copying file from "C:\MyProject\\bin\app.publish\DeploymentScheduler.exe" to "C:\MyProject\..\Deploy\Client\DeploymentScheduler.exe".
Copying file from "C:\MyProject\\..\..\Build\publish.htm" to "C:\MyProject\..\Deploy\Client\publish.htm".
Updating File "C:\MyProject\..\Deploy\Client\publish.htm".
_CopyFilesToPublishFolder:
Creating directory "bin\app.publish\Application Files\MyApplication_1_0_0_0".
Copying file from "bin\MyProject.exe.manifest" to "bin\app.publish\Application Files\MyProject_1_0_0_0\MyProject.exe.manifest".
Copying file from "bin\app.publish\MyProject.exe" to "bin\app.publish\Application Files\MyProject_1_0_0_0\MyProject.exe.deploy".
Copying file from "app.config" to "bin\app.publish\Application Files\MyProject_1_0_0_0\MyProject.exe.config.deploy".
Copying file from "obj\Debug\MyProject.pdb" to "bin\app.publish\Application Files\MyProject_1_0_0_0\MyProject.pdb.deploy".
1>Done Building Project
Basically, how do I get my target "CustomPostPublishActions" to run AFTER "_CopyFilesToPublishFolder"? Is it even possible? Any help is really appreciated!
There are predefined targets that can be overwritten to run before or after publish, update your script like this:
<Target Name="AfterPublish">
<!-- Create directory -->
<MakeDir Directories="$(ProjectDir)..\Deploy\Client\Application Files" />
<!-- Copy published website to deployment location -->
<ItemGroup>
<_CopyItems Include="$(ClickOnceBuildDirectory)\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(_CopyItems)" DestinationFolder="$(ProjectDir)..\Deploy\Client" />
<!-- Write publish.htm file for ClickOnce -->
<Copy SourceFiles="$(ProjectDir)\..\..\Build\publish.htm" DestinationFiles="$(ProjectDir)..\Deploy\Client\publish.htm" />
<FileUpdate Files="$(ProjectDir)..\Deploy\Client\publish.htm" Regex="{VERSION}" IgnoreCase="true" Multiline="true" Singleline="false" ReplacementText="$(ApplicationVersion)" />
</Target>
This link will give you an idea of the existing overridable targets and how you can work with them.

Visual Studio Post build command line Deployment

In Visual Studio am creating a post-build event for Deploying using
md "$(SolutionDir)Deploy\bin"
which created the bin folder inside Deploy folder, inside my Solution.
How do I point this to the folder in some remote machine (where I have the web server)?
$(SolutionDir) to some other folder on a remote machine?
It may look simple to you. :) This is the first time am trying this stuff.
Thanks
The easiest way is to replace $(SolutionDir) with \\server\share
Just as an alternative, I like to keep my .sln and .csproj files "clean".
Then use a second (mini) .msbuild ( which is just a .xml file) to build the .sln, and then do these copy type events as a second action.
Here is a basic example:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper">
<PropertyGroup>
<WorkingCheckout>.</WorkingCheckout>
<WorkingOutputs>m:\working\outputs</WorkingOutputs>
</PropertyGroup>
<Target Name="AllTargetsWrapper">
<CallTarget Targets="Clean" />
<CallTarget Targets="Build" />
<CallTarget Targets="CopyItUp" />
</Target>
<Target Name="Clean">
<RemoveDir Directories="$(WorkingOutputs)" />
<MakeDir Directories="$(WorkingOutputs)" />
<Message Text="Cleaning done" />
</Target>
<Target Name="Build">
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
</MSBuild>
<Message Text="Build completed" />
</Target>
<!-- -->
<Target Name="CopyItUp" >
<ItemGroup>
<MyExcludeFiles Include="$(WorkingCheckout)\**\SuperSecretStuff.txt" />
<MyExcludeFiles Include="$(WorkingCheckout)\**\SuperSecretStuff.doc" />
</ItemGroup>
<ItemGroup>
<MyIncludeFiles Include="$(WorkingCheckout)\MyCsProject\bin\$(Configuration)\**\*.*" Exclude="#(MyExcludeFiles)"/>
</ItemGroup>
<Copy
SourceFiles="#(MyIncludeFiles)"
DestinationFiles="#(MyIncludeFiles->'$(WorkingOutputs)\%(RecursiveDir)%(Filename)%(Extension)')"
/>
</Target>
</Project>

TeamCity MSBuild 4.0 Help

I need some help with my MSBuild file i created a while ago.
All i want to do is build the solution, publish a project inside the solution and than copy the files to a directory
At the moment when i set Teamcity to .net 4 msbuild, msbuild 4.0 tools and for 86 i get an error stating
error MSB4067: The element <ItemDefinitionGroup> beneath element <Project> is unrecognized.
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets"/>
<PropertyGroup>
<OutputFolder>$(OutputDir)</OutputFolder>
<DeploymentFolder>$(DeploymentDir)</DeploymentFolder>
<CompilationDebug />
<CustomErrorsMode />
<ContentEditorsEmail />
<AdministratorsEmail />
</PropertyGroup>
<Target Name="Run">
<CallTarget Targets="Compile" />
<CallTarget Targets="Publish" />
<CallTarget Targets="Deploy" />
</Target>
<Target Name="Clean">
<ItemGroup>
<BinFiles Include="bin\*.*" />
</ItemGroup>
<Delete Files="#(BinFiles)" />
</Target>
<Target Name="Compile" DependsOnTargets="Clean">
<MSBuild Projects="WebCanvas.ZakisCatering.Website.sln"
Properties="Configuration=Release"/>
</Target>
<Target Name="Publish">
<RemoveDir Directories="$(OutputFolder)" ContinueOnError="true" />
<MSBuild Projects="WebCanvas.ZakisCatering.Website\WebCanvas.ZakisCatering.Website.csproj"
Targets="ResolveReferences;_CopyWebApplication"
Properties="Configuration=Release;WebProjectOutputDir=$(OutputFolder);OutDir=$(WebProjectOutputDir)\" />
</Target>
<Target Name="Deploy">
<RemoveDir Directories="$(DeploymentFolder)"
ContinueOnError="true" />
<ItemGroup>
<DeploymentFiles Include="$(OutputFolder)\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(DeploymentFiles)"
DestinationFolder="$(DeploymentFolder)\%(RecursiveDir)" />
</Target>
</Project>
I'm getting that error code too, although complaining about a different element:
error MSB4067: The element <ArtifactAssemblies> beneath element <ItemGroup> is unrecognized.
I did notice that Teamcity is invoking the 2.0 version of MSBuild, which could explain why msbuild is struggling with the xml.
'C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe'
'#"D:\BuildAgent\work\2f016459feee51ce\Build\BuildSolution.msbuild.teamcity.msbuild.tcargs"
"D:\BuildAgent\work\2f016459feee51ce\Build\BuildSolution.msbuild.teamcity.patch.tcprojx"'
working dir =
'D:\BuildAgent\work\2f016459feee51ce'
Microsoft (R) Build Engine Version
2.0.50727.4016 [Microsoft .NET Framework, Version 2.0.50727.4200]
I fixed the 2.0 msbuild problem by adding to the .\conf\buildagent.properties file on the team city build agent machine, the following:
env.MSBuild=%system.DotNetFramework4.0_x86_Path%
Restart the service after that and problem solved.
Unfortunately, they don't package the WebApplications targets.
I can't find an SDK that has these targets packaged without having VS installed...no way.
I don't understand why MS makes CI so difficult.

Resources