Execute PostBuildEvent after AfterBuild on Visual Studio - build

How do I make the postbuildevent execute AFTER the AfterBuild is completed?

The solution was to add :
<Target Name="AfterBuild">
<Exec Command="$(PostBuildEvent)" />
</Target>
I was actually using Costura VS Package and I wished it automatically did this.Anyway, I've modified the Costura VS Package source code and changed it so that it automatically adds this to the Web.Config.
Thanks.

Related

Running unit tests from a .proj file with MSBuild

I want to run unit tests using MSBuild. Here is how I invoke msbuild today:
msbuild MySolution.sln
Instead, I want to use an MSBuild project file called "MyBuild.proj" like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5" DefaultTargets="Build">
<Target Name="Build">
<ItemGroup>
<SolutionToBuild Include="MySolution.sln" />
<TestContainer Include="..\Output\bin\Debug\*unittests.dll"/>
</ItemGroup>
</Target>
</Project>
And then call this command line:
msbuild MyBuild.proj
For some reason, when I do that the command succeeds immediately and the build doesn't even happen. I fear I must be missing something very obvious as I am new to MSBuild.
I suppose I really have 2 questions:
Why doesn't this even build my solution
Is the "TestContainer" element correct for executing my tests
Thanks!
You havent supplied any task to actually do anything,
inside your build target you need a call to an msbuild task, your example becomes:
<Target Name="Build">
<ItemGroup>
<SolutionToBuild Include="MySolution.sln" />
<TestContainer Include="..\Output\bin\Debug\*unittests.dll"/>
</ItemGroup>
<MSBuild Projects="#(SolutionToBuild)"/>
</Target>
this specifies what projects you actually want msbuild to build.
See:http://msdn.microsoft.com/en-us/library/vstudio/z7f65y0d.aspx for more details and the parameters it takes.
Thats part one.
As for part 2? what testing framework are you using? If using mstest id try wrapping the commandline mstest.exe in an msbuild exec statement to get it to run and execute the tests. See an example here:http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/cb87a184-6589-454b-bf1c-2e82771fc3aa

Call Publish after Build in MSBuild

I am trying to call the Publish target every time I build my WPF app. I have tweaked the .csproj file to include this:
<Target Name="AfterBuild">
<Message Text="Running AfterBuild..." />
<MSBuild Projects="$(MSBuildProjectFullPath)" Properties="Configuration=$(Configuration); PublishDependsOn=" Targets="Publish" />
</Target>
When I run this from the command line, I see the message that it is 'Running AfterBuild...' but nothing happens. If I remove the '; PublishDependsOn=' from the Properties of the MSBuild task, I get a circular reference error.
What magic am I missing here?
OK, I figured out how to do what I want to do. Instead of trying to explicitly call Publish in AfterBuild, I just added it to the DefaultTargets of the project. Now it calls Build then Publish.
<Project ToolsVersion="4.0" DefaultTargets="Build;Publish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Compiling SSIS Projects with Team City

Has anyone done this? If so, what tools/techniques/approaches did you use?
Is it possible to do with installing the SQL Business Studio Version of Visual Studio?
Thanks in advance!
Got it folks...
1) Install MSBuild Extensions
2) Created a Build.Xml file as so...
<Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
<Target Name="Default">
<PropertyGroup>
<OutputRoot>../../../build-artifacts</OutputRoot>
</PropertyGroup>
<ItemGroup>
<SSISProjectFile Include="SSISProject.dtproj"/>
<SSISProject Include="#(SSISProjectFile)">
<OutputDirectory>$(OutputRoot)</OutputDirectory>
</SSISProject>
</ItemGroup>
<ItemGroup>
<Namespaces Include="Mynamespace">
<Prefix>DTS</Prefix>
<Uri>www.microsoft.com/SqlServer/Dts</Uri>
</Namespaces>
</ItemGroup>
<MSBuild.ExtensionPack.Xml.XmlFile
TaskAction="UpdateElement"
File="EnclarityDataImport.dtsx"
XPath="//DTS:Property[#DTS:Name='ConfigurationString']"
InnerText="$(MSBuildProjectDirectory)\EnclarityDataImport.dtsConfig"
Namespaces="#(Namespaces)"/>
<MSBuild.ExtensionPack.SqlServer.BuildDeploymentManifest InputProject="#(SSISProject)"/>
</Target>
The only trick was the last part of the build here. By default visual studio adds the absolute path to your config and connection string files for your dtsx package. Team City will use these in conjunction with MSBuild extensions to build the package so a local path will break the build because the paths to the build directories in Team City are automatically generated. So using the code above and the $(MSBuildProjectDirectory) you can twiddle the value of the path on your dtsx file so that it points to the path where your compilation is exectuing.
Like booyaa says SSIS projects don't need to be compiled, but what i have done is make the .dtconfigs configurable by the build/deployment process.
I do this so that i can run the packages on deployment in different environments. So the build will copy a template of the dtconfig file.
this contains tokens- $(Servername) $(ConnectionString)
And then i do the replacement on deployment and then execute by wrapping the dtexec in an command.
Not sure about 2012.

How to run exe file on CruiseControl

I've been looking through CruiseControl documentation and I found tag and
for running scripts. But when I am trying to run exe-file from that tags it does not work as well as it described in documantation.
I also tried to put call of the exe in batch file and execute it from CruiseControl but also did not work as I expected. So how can I run exe-file from CC? I also need to be able to include output of this file work in my email notification is it possible at all?
E.g. I have file UnitTests.exe which prints something like this:
Unit tests are passed.
47 Tests was successful
How can I do this? Or how can I at least get an returning code from that executable file?
Run the exec in ant.
In cruisecontrol:
<schedule>
<ant anthome="/usr/apache-ant-1.8.2" buildfile="/usr/ant-build-files/my-ant-build-file.build" target="do-task" uselogger="true">
</ant>
</schedule>
In /usr/ant-build-files/my-ant-build-file.build
...
<target name="do-task">
<exec executable="/<path to dir containing exe>/UnitTests.exe" failonerror="true">
<arg line="<args to UnitTests.exe>"/>
</exec>
There is option to execute .bat or .exe files using the following tag.
<exec executable="c:/something.exe" />
You can place the above line in any target of the xml files that your build script is going to call.
<target name="target-to-call-an-exe">
<exec executable="c:/cygwin/bin/bash.exe" />
</target>
Hope this helps, Thanks.

Copy all files and folders using msbuild

Just wondering if someone could help me with some msbuild scripts that I am trying to write. What I would like to do is copy all the files and sub folders from a folder to another folder using msbuild.
{ProjectName}
|----->Source
|----->Tools
|----->Viewer
|-----{about 5 sub dirs}
What I need to be able to do is copy all the files and sub folders from the tools folder into the debug folder for the application. This is the code that I have so far.
<ItemGroup>
<Viewer Include="..\$(ApplicationDirectory)\Tools\viewer\**\*.*" />
</ItemGroup>
<Target Name="BeforeBuild">
<Copy SourceFiles="#(Viewer)" DestinationFolder="#(Viewer->'$(OutputPath)\\Tools')" />
</Target>
The build script runs but doesn't copy any of the files or folders.
Thanks
I was searching help on this too. It took me a while, but here is what I did that worked really well.
<Target Name="AfterBuild">
<ItemGroup>
<ANTLR Include="..\Data\antlrcs\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(ANTLR)" DestinationFolder="$(TargetDir)\%(RecursiveDir)" SkipUnchangedFiles="true" />
</Target>
This recursively copied the contents of the folder named antlrcs to the $(TargetDir).
I think the problem might be in how you're creating your ItemGroup and calling the Copy task. See if this makes sense:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<YourDestinationDirectory>..\SomeDestinationDirectory</YourDestinationDirectory>
<YourSourceDirectory>..\SomeSourceDirectory</YourSourceDirectory>
</PropertyGroup>
<Target Name="BeforeBuild">
<CreateItem Include="$(YourSourceDirectory)\**\*.*">
<Output TaskParameter="Include" ItemName="YourFilesToCopy" />
</CreateItem>
<Copy SourceFiles="#(YourFilesToCopy)"
DestinationFiles="#(YourFilesToCopy->'$(YourDestinationDirectory)\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
</Project>
I'm kinda new to MSBuild but I find the EXEC Task handy for situation like these. I came across the same challenge in my project and this worked for me and was much simpler. Someone please let me know if it's not a good practice.
<Target Name="CopyToDeployFolder" DependsOnTargets="CompileWebSite">
<Exec Command="xcopy.exe $(OutputDirectory) $(DeploymentDirectory) /e" WorkingDirectory="C:\Windows\" />
</Target>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<YourDestinationDirectory>..\SomeDestinationDirectory</YourDestinationDirectory>
<YourSourceDirectory>..\SomeSourceDirectory</YourSourceDirectory>
</PropertyGroup>
<Target Name="BeforeBuild">
<CreateItem Include="$(YourSourceDirectory)\**\*.*">
<Output TaskParameter="Include" ItemName="YourFilesToCopy" />
</CreateItem>
<Copy SourceFiles="#(YourFilesToCopy)"
DestinationFiles="$(YourFilesToCopy)\%(RecursiveDir)" />
</Target>
</Project>
\**\*.* help to get files from all the folder.
RecursiveDir help to put all the file in the respective folder...
This is the example that worked:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<MySourceFiles Include="c:\MySourceTree\**\*.*"/>
</ItemGroup>
<Target Name="CopyFiles">
<Copy
SourceFiles="#(MySourceFiles)"
DestinationFiles="#(MySourceFiles->'c:\MyDestinationTree\%(RecursiveDir)%(Filename)%(Extension)')"
/>
</Target>
</Project>
source: https://msdn.microsoft.com/en-us/library/3e54c37h.aspx
This is copy task i used in my own project, it was working perfectly for me that copies folder with sub folders to destination successfully:
<ItemGroup >
<MyProjectSource Include="$(OutputRoot)/MySource/**/*.*" />
</ItemGroup>
<Target Name="AfterCopy" AfterTargets="WebPublish">
<Copy SourceFiles="#(MyProjectSource)"
OverwriteReadOnlyFiles="true" DestinationFolder="$(PublishFolder)api/% (RecursiveDir)"/>
In my case i copied a project's publish folder to another destination folder, i think it is similiar with your case.
Did you try to specify concrete destination directory instead of
DestinationFolder="#(Viewer->'$(OutputPath)\\Tools')" ?
I'm not very proficient with advanced MSBuild syntax, but
#(Viewer->'$(OutputPath)\\Tools')
looks weird to me. Script looks good, so the problem might be in values of $(ApplicationDirectory) and $(OutputPath)
Here is a blog post that might be useful:
How To: Recursively Copy Files Using the <Copy> Task
Personally I have made use of CopyFolder which is part of the SDC Tasks Library.
http://sdctasks.codeplex.com/
The best way to recursively copy files from one directory to another using MSBuild is using Copy task with SourceFiles and DestinationFiles as parameters. For example - To copy all files from build directory to back up directory will be
<PropertyGroup>
<BuildDirectory Condition="'$(BuildDirectory)' == ''">Build</BuildDirectory>
<BackupDirectory Condition="'$(BackupDiretory)' == ''">Backup</BackupDirectory>
</PropertyGroup>
<ItemGroup>
<AllFiles Include="$(MSBuildProjectDirectory)/$(BuildDirectory)/**/*.*" />
</ItemGroup>
<Target Name="Backup">
<Exec Command="if not exist $(BackupDirectory) md $(BackupDirectory)" />
<Copy SourceFiles="#(AllFiles)" DestinationFiles="#(AllFiles->
'$(MSBuildProjectDirectory)/$(BackupDirectory)/%(RecursiveDir)/%(Filename)%
(Extension)')" />
</Target>
Now in above Copy command all source directories are traversed and files are copied to destination directory.
If you are working with typical C++ toolchain, another way to go is to add your files into standard CopyFileToFolders list
<ItemGroup>
<CopyFileToFolders Include="materials\**\*">
<DestinationFolders>$(MainOutputDirectory)\Resources\materials\%(RecursiveDir)</DestinationFolders>
</CopyFileToFolders>
</ItemGroup>
Besides being simple, this is a nice way to go because CopyFilesToFolders task will generate appropriate inputs, outputs and even TLog files therefore making sure that copy operations will run only when one of the input files has changed or one of the output files is missing. With TLog, Visual Studio will also properly recognize project as "up to date" or not (it uses a separate U2DCheck mechanism for that).