I have a build definition set up in my TFS 2012 instance. In this Build Definition I want to pass in a custom argument and access said argument in my .csproj file. For example:
MSBuild Arguments: /p:MyFoo=1
In my .csproj file I want to do this:
<ItemGroup Condition=" '$(MyFoo)' == '1' ">
Is this possible, or am I going about this incorrectly?
This is more than possible, it's very easy to do. Edit your build definition, under the process tab expand the "advanced" section and you will see a property called "MSbuild Arguments" add the argument in the format in the question. e.g. /p:MyFoo=1
e.g.
You can also enter the arguments when you queue a build
Related
For example given a project quickie.vcxproj, to build it under c:\output\c without specifying the project name explicitly:
msbuild /t:Rebuild /p:Configuration=Debug /p:PlatformToolset=v100 quickie\quickie.vcxproj /p:OutDir=c:\output\c\$(MSBuildProjectName)\
The result is a directory c:\output\c\$(MSBuildProjectName)
Required result is: c:\output\c\quickie
Attemping with c:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild
No. Exceeding thirty characters
Below is a portion of a MSBuild file that I'm working on:
<ItemGroup>
<Tests Include="$(SolutionDir)\**\bin\$(TestPlatform)\$(Configuration)\*.Tests.dll" />
</ItemGroup>
<PropertyGroup>
<TestProperties>/testcontainer:%(Tests.FullPath)</TestProperties>
</PropertyGroup>
I want to have a property that holds a command line switch. However, when I try to use $(TestProperties) in an Exec Command string, %(Tests.FullPath) is never resolved to the absolute path of the Tests item. Instead, it's always processed literally, as "%(Tests.FullPath)".
Am I doing something wrong or is this a standard MSBuild behavior? If the latter, is there a way for me to workaround this?
Thanks
P.S. - I realize I probably don't need to access the FullPath property since my Include value is an absolute path. However, I'd still like to understand the issue, along with how to handle it.
You have a syntax error. Item lists are referenced via the # character and item meta data is referenced via %. Reference the MSBuild Special Character Reference for details. To access the well known item metadata, you need to apply a transform inside the Property itself.
<ItemGroup>
<Tests Include="MyFile.txt" />
</ItemGroup>
<PropertyGroup>
<TestProperties>/testcontainer:#(Tests->'%(FullPath)')</TestProperties>
</PropertyGroup>
You can find more help here
We have a Team City Unit test build which we call using $(teamcity_dotnet_nunitlauncher) through different MSBuild scripts. The build has an environment variable set up. Is there a way we can override the variable through the scripts?
Environment variables are "published" as MSBuild properties, so for example:
$(COMPUTERNAME)
...will give the value of that env var. You can override any property inside an MSBuild script...
<PropertyGroup>
<COMPUTERNAME>NewName</COMPUTERNAME>
</PropertyGroup>
...or pass it in on the command line to MSBuild...
> msbuild My.proj /t:Target /p:COMPUTERNAME=NewName
These two ways to alter the "property" presented by an environment variable are not equal though, when passing a property via the command line, the value will not be overridden if it is declared in an MSBuild file.
Our MSBuild scripts use the Exec task to call a few command line applications. Most of these applications have their own output verbosity settings which I would like to have be the same as the verbosity level of the MSBuild script calling them.
Is there a way for me to get the logging verbosity level of my MSBuild process?
I thought I could write a custom task to handle this, but poking around the MSBuild API, I couldn't find any properties or classes that would give me the verbosity level.
Shortly after asking my questions, I noticed that MSBuild 4 exposes System.Environment.CommandLine as a property function, which should include any verbosity arguments. With the following bit of parsing, you can create several boolean properties that will tell you the current logging level:
<PropertyGroup>
<CommandLine>$([System.Environment]::CommandLine.Trim().ToLower())</CommandLine>
<IsQuietVerbosity>False</IsQuietVerbosity>
<IsMinimalVerbosity>False</IsMinimalVerbosity>
<IsNormalVerbosity>True</IsNormalVerbosity>
<IsDetailedVerbosity>False</IsDetailedVerbosity>
<IsDiagnosticVerbosity>False</IsDiagnosticVerbosity>
</PropertyGroup>
<PropertyGroup Condition="'$(CommandLine.Contains("/v"))' == 'True'">
<IndexOfLastVerbosityArg>$(CommandLine.LastIndexOf("/v"))</IndexOfLastVerbosityArg>
<IndexOfVerbosityArg>$(CommandLine.IndexOf(":", $(IndexOfLastVerbosityArg)))</IndexOfVerbosityArg>
<IndexOfVerbosityArg>$([MSBuild]::Add($(IndexOfVerbosityArg), 1))</IndexOfVerbosityArg>
<IndexOfEndOfVerbosityArg>$(CommandLine.IndexOf(" ", $(IndexOfVerbosityArg)))</IndexOfEndOfVerbosityArg>
<IndexOfEndOfVerbosityArg Condition="'$(IndexOfEndOfVerbosityArg)' == '-1'">$(CommandLine.Length)</IndexOfEndOfVerbosityArg>
<LengthOfVerbosityArg>$([MSBuild]::Subtract($(IndexOfEndOfVerbosityArg), $(IndexOfVerbosityArg)))</LengthOfVerbosityArg>
<VerbosityLevel>$(CommandLine.Substring($(IndexOfVerbosityArg), $(LengthOfVerbosityArg)).Trim())</VerbosityLevel>
<IsQuietVerbosity>$(VerbosityLevel.StartsWith('q'))</IsQuietVerbosity>
<IsMinimalVerbosity>$(VerbosityLevel.StartsWith('m'))</IsMinimalVerbosity>
<IsNormalVerbosity>$(VerbosityLevel.StartsWith('n'))</IsNormalVerbosity>
<IsDiagnosticVerbosity>$(VerbosityLevel.StartsWith('di'))</IsDiagnosticVerbosity>
<IsDetailedVerbosity Condition="'$(IsDiagnosticVerbosity)' == 'False'">$(VerbosityLevel.StartsWith('d'))</IsDetailedVerbosity>
</PropertyGroup>
Remember, this will only work in MSBuild 4+.
Ugly? Yup. Kludgy? Maybe. Does it work. Yup!
You can't : http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/510f07b4-c5f7-43a8-b7cb-e3c398841725/
Instead, you can set a property of yours (passing it with the command line for example) which contains the verbosity level.
Is there a way to use MSBuild syntax to skip a specific target? I have a file consisting of a lot of properties (lines containing /property:X=Y) that I want to pass on to a recursively called instance of MSBuild, but this file also contains a /target:X line, that I do not want to have any effect. I don't have the option to modify the file.
I suppose you are able to edit .proj file. You can manage MSBuild targets executing by the Condition. Your target, which you want to exclude, could contain something like this:
<Target
Name="SomeTarget"
Condition="'$(SomeProperty)'=='true'"
DependsOnTargets="SomeAnotherTarget"/>
SomeProperty can be passed in the calling:
MSBuild.exe build.proj /p:SomeProperty=false
Regards