I have created some ms build tasks for my VS project.
Rather than having to update the VS Project file with each of the tasks, is it possible to create an external file to hold the build tasks and reference it via the main project file?
Also, I have seen with nant, that you can create .bat file to run nant tasks. Is it possible to do similar with msbuild?
Yes. You can use the Import task:
<Import Project="PathToMyIncludeFile\Include.proj" />
And yes you can create a batch file to run msbuild. The syntax is
msbuild <project> /t:target[;target] /p:propertyname=propertyvalue
Where the targets are defined in the msbuild file and the properties are any properties defined in the file. If you don't specify a target, the default that is defined in the msbuild file element will be run. Here are a couple of examples:
So to run your build with the Clean, and Compile targets:
msbuild myproject.proj /t:Clean;Compile
Or to run your build with a target of Compile and a release configuration:
msbuild myproject.proj /t:Compile /p:Configuration=Release
Or to run your build with the default target and a set a version property:
msbuild myproject.proj /p:Version=2.0.0.1
Command line parameters take precedence over the values defined in the file. So in the example above if you had the version defined in the file as:
<PropertyGroup>
<Version>1.0.0.0</Version>
<PropertyGroup>
The build would run with a configured version of 2.0.0.1
As usual, check out MSDN for more info.
Related
when building an APK using MSBuild I would like to change the output apk name to include the version number
so
android:versionName="3.1.5"
would end up like:
MyAndroidApp-3.1.5.apk
i tried to do a post-build step where i try to copy the apk to a different name , but there aren't any macros in the post build step that has the version (that i can see)
When calling
MSBuild .\trunk\TaxiTabletUniversal.Droid.MyAndroidProject /t:SignAndroidPackage /p:Configuration=Release
the output apk ends up with the name:
company_name.mypackage_name-Signed.apk
In the Android Package Signing setting i can only specify the keystore and paswords, but no output name.
I would like the output name to pickup the versionName in the
AndroidManifest.xml
file
Changing default APK output to include version in the name using
MSBuild
Sorry but the answer could be negative, as I know MSBuild itself doesn't have the ability to read data like Version-number from AndroidManifest.xml. In other words, it's not supported by msbuild.
MSBuild can access any property defined in project file or imported targets file, but it can't access AndroidManifest.xml file. And there's no official msbuild task can to this for us, so if you do need this behavior, we have to code ourselves to read the version info from that xml file. Topics about this: one, two, three...(Too many topics online talk about this, so I don't talk too much here, if you meet some issue about coding that, let me know:-
))
Here're two possible ways after that coding:
1.Create a .exe file with code to execute the renaming job, and call the .exe in a post-build event
2.Write a custom task named CustomTask, and add this script into the project file to call this task after build or SignAndroidPackage target.
<UsingTask TaskName="CustomTask.CustomTask"
AssemblyFile="path\CustomTask.dll"/>
<!--Maybe it should be AfterTargets="SignAndroidPackage"-->
<Target Name="CustomTarget" AfterTargets="Build">
<CustomTask/>
</Target>
May it makes some help.
This may be a noobish question but here I go anyway.
So in VS I have a project whose build configuration I changed from Debug|AnyCpu to Release|AnyCpu.
Now this all works good and fine and when I build the project via VS this configuration is also used in the build process.
however I am also using the msbuild.exe in order to build the project via command sometimes and the problem is no matter what I do I can not change the
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
element of the project via VS. So setting the build configuration does only seem to work for VS and I bascially have to manually add this in the project file so that the msbuild.exe uses the correct build configuration.
Any Idea how I can change it in VS so the change also alters the project file ?
You don't need to change it explicitly. This string is "default configuration" for msbuild and VS - if no $(Configuration) value specified - set it to Debug.
If you want to build release configuration using msbuild you may want to specify configuration property or platform architecture in command line explicitly:
msbuild.exe myProject.csproj /p:Configuration="Release" /p:Platform="Any CPU"
I have a custom MSBuild task which among other things adds embedded resources to other projects in the solution. After adding the resources I'd like to then build those projects, but found I can't get this working within Visual Studio.
To test, I stripped out the custom task entirely and redefined a simple AfterBuild target in the web project of a Silverlight solution. The target uses the MSBuild task to build the Silverlight application project in the solution, and looks like this:
<Target Name="AfterBuild">
<PropertyGroup>
<LinkedProject>..\SilverlightApplication1\SilverlightApplication1.csproj</LinkedProject>
</PropertyGroup>
<MSBuild Condition="'$(LinkedProject)' != '' "
Projects="$(LinkedProject)"
Targets="Build"
Properties="CustomFlag=true" >
</MSBuild>
</Target>
The odd thing is that this works perfectly when using MSBuild from the command line, yet does not work in Visual Studio when building the web project. I thought this might be some sort of Silverlight problem, and had the task build a .NET class library project instead, but the result was the same - it worked from the command line but not within VS. In VS there's no actual error - it's just that the Csc task does not compile the assembly and generates no output.
What do I need to do to get this working within Visual Studio?
Pass the 'UseHostCompilerIfAvailable=false' property to the MSBuild task.
It looks like Visual Studio breaks badly if csc is invoked from a MSBuild task as it reuses the initial project build settings for its in-process host compiler. In my case, I was building the same project twice - default build was using target framework v3.5, with a AfterBuild MSBuild task specifying v4.0. I ended up with the same issue - csc appeared to run but produced no output. I think what was happening was that with the UseHostCompilerIfAvailable property set to true, csc was calling the hosted compiler which reused my initial project settings, so even though the command line showed csc "building" my v4.0 assembly, the host compiler was simply overwriting the v3.5 one I had just built!
Change Visual Studio verbosity to detailed and check build log. I think that CoreBuild is not executed if your files have not changed, so you could try to use AfterCompile instead of AfterBuild.
I have a Visual Studio 2010 SharePoint project. If I choose 'Package' from the project menu, a .wsp file is generated. How can I invoke the same build from command line (i.e. what /target is required for MSBuild)?
I got it to work, finally. The tricky part is the fact that the SharePoint targets do not exist when MSBuild loads the .sln file, you have to load the individual .csproj files.
set msbuild="C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
set config=Debug
set outdir="C:\out\"
%msbuild% /p:Configuration=%config% /m ../My.SharePoint.Project/My.SharePoint.Projectcsproj /t:Package /p:BasePackagePath=%outdir%
This is also a useful document here: http://msdn.microsoft.com/en-us/ff622991.aspx
"To generate packages when building in
TFS 2010, set the parameter
/p:IsPackaging=True on MSBuild"
Also to package project with msbuild you can use target Package:
Define new target "BuildAndPackage"
<Target Name="BuildAndPackage">
<CallTarget Targets="Build"/>
<CallTarget Targets="Package"/>
</Target>
Use new target in build process:
<Project ToolsVersion="4.0" DefaultTargets="BuildAndPackage">
But this approach not recommended because it may cause errors in TFS Build process..
Set the MSBuild's verbosity to 'maximum' and you should see what is called from the build console.
In VS2010 of course :)
I'm using the config file replacement feature of Visual Studio 2010's "Publish" functionality, as described in this article. I want to automate this using MSBuild/Hudson. Does anybody know how to do this?
I like how it works but if I cannot automate it I'll have to switch to XmlMassUpdate or similar.
Explanation
To transform your config file you'll have to execute the TransformWebConfig target.
This target takes two files Web.config and Web.$(Configuration).config and generates a Web.config. The generated file is the transformed version of the original one for the current configuration.
This file is generated in folder : obj\$(Configuration)\TransformWebConfig
Usage
You don't really explain what you want to achieve, so here a basic usage, a job that generates a transformed config file in a given folder.
Add the following piece in the end of your project file *.csproj after the import of Microsoft.WebApplication.targets
<PropertyGroup>
<!-- Directory where your web.config will be copied -->
<TransformedWebConfigDestination>$(MSBuildProjectDirectory)</TransformedWebConfigDestination>
</PropertyGroup>
<!--
This target transforms the web.config based on current configuration and
put the transformed files in $(TransformedWebConfigDestination) folder
-->
<Target Name="ConfigSubstitution">
<CallTarget Targets="TransformWebConfig"/>
<ItemGroup>
<TransformedWebConfig Include="obj\$(Configuration)\TransformWebConfig\Web.config"/>
</ItemGroup>
<!-- Copy the transformed web.config to the configured destination -->
<Copy SourceFiles="#(TransformedWebConfig)"
DestinationFolder="$(TransformedWebConfigDestination)"/>
</Target>
In Hudson you could add a Build step in your build, or create a dedicated job configured as follow:
MsBuild Build File : Your csproj file.
Command Line Arguments : /t:ConfigSubstitution /p:Platform=AnyCpu;Configuration=Test;TransformedWebConfigDestination=DestinationFolder
Edit your web project.csproj
under
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Add -
<UseMsDeployExe>True</UseMsDeployExe>
Look at the Build output (make sure VS Tools - Options - Project & Solutions -Build & Run - MSBuild Output Verbosity - Detailed)
You should be able to see the msdeploy commands VS uses to produce the package. It's my understanding that VS actually uses Web Platform Pipeline API's and .target files to actually produce the deploy packages when building using MSBuild, and this command changes to use MsDeploy instead.
This stuff is so in need of documentation, its very frustrating.
I am using this in Hudson to target Release:
/Property:Configuration=Release
The exact settings are:
Build
MSBuild Version: msbuild-4 (configured to point to v4 msbuild)
MsBuild Build File: project_name.sln
Command Line Arguments: /Property:Configuration=Release
You can test this in your project directory by running something similar (as your .NET framework version may differ) to this:
%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319\msbuild project.sln /Property:Configuration=Release