How to display quote (") symbol in ant script - scripting

My ant script is,
<target name="temp">
<property name="argument" value="xyz abc"/>
<echo message=" & quot;${argument}& quot;"/>
</target>
Output is
xyz abc
Expected Output is
"xyz abc"
How to get the expected output. I tried like this "quot;${argument}"quot;, this is also not working.
Note : I am using ant 1.8.2 and antcontrib in windows 7.

Is there a space between & and quot;?
This works for me:
<target name="temp">
<property name="argument" value="xyz abc"/>
<echo message=""${argument}""/>
</target>
Output:
temp:
[echo] "xyz abc"
BUILD SUCCESSFUL
Total time: 469 milliseconds

Related

How does Apache Ant resolve duplicate targets

Say I have two targets in ant with the same name in two different build files but, one is imported into the other.
build.xml
<project>
<target name="once">
<echo>once</echo>
</target>
<target name="twice">
<echo>twice-a in build.xml</echo>
</target>
<!-- duplicate target twice imported again from build2.xml -->
<import file="build2.xml"/>
</project>
build2.xml
<project>
<target name="twice">
<echo>twice-a in build2.xml</echo>
</target>
</project>
How does ant resolve duplicate targets ?
If it had been in a single file it would have a duplicate target thrown an error however since its imported its not throwing an error.
When I run ant twice I get
$ ant twice
Buildfile: /Users/nav/Codes/build.xml
twice:
[echo] twice-a in build.xml
BUILD SUCCESSFUL
Total time: 0 seconds
If ant does take the first declaration as the target then why doesn't moving the import statement up in build.xml
<?xml version="1.0"?>
<project>
<!-- import moved to the top -->
<import file="build2.xml"/>
<target name="once">
<echo>once</echo>
</target>
<target name="twice">
<echo>twice-a in build.xml</echo>
</target>
</project>
still outputs the same as
$ ant twice
Buildfile: /Users/nav/Codes/build.xml
twice:
[echo] twice-a in build.xml
BUILD SUCCESSFUL
Total time: 0 seconds
When you assign project names, then you can access the both targets
<project name="build1">
<target name="once">
<echo>once</echo>
</target>
<target name="twice">
<echo>twice-a in build.xml</echo>
</target>
<!-- duplicate target twice imported again from build2.xml -->
<import file="build2.xml"/>
</project>
Build2
<project name="build2">
<target name="twice">
<echo>twice-a in build2.xml</echo>
</target>
</project>
call ant -p
Buildfile: build.xml
Main targets:
Other targets:
build2.twice
once
twice
If not project name is assign the imported target are hidden if they have the same name.

Change output directory where test results file is generated

I am using MSTest to execute my unit tests against a web service on a remote server.
Is there way to change the path to where the results file(.trx) is generated. Currently my test results is generated in the directory which the Exec command is invoked from:
<Target Name="ExecuteTheTests" AfterTargets="StartService" Condition="'$(ServiceStarted)' == 0 And '$(Configuration)' == 'Release'">
<Message Text="Executing the Unit Tests" Importance="high" />
<PropertyGroup>
<TestSuccessOrNot>0</TestSuccessOrNot>
</PropertyGroup>
<Exec Command=""C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\MSTest.exe" /testcontainer:..\..\..\..\\_MyOutput\\UnitTests.dll /detail:testname ">
<Output TaskParameter="ExitCode" PropertyName="TestSuccessOrNot" />
</Exec>
<Error Condition="$(TestSuccessOrNot) == 1" Text="Unit tests fail!" />
Thanks,
Many Bothans died* to bring you this message:
mstest.exe /resultsfile:c:\BadPlaceForTestResults.trx
j/k, many Bothans didn't die, they just typed "MsTest.exe /?"

MSBuild /m:4 fails because it builds the same project twice

My team has a large solution (~500 csproj's). We use VS2012, and build using TFS Build, which uses MSBuild 4. Currently we build serially, but we want to build in parallel (using msbuild /maxcpucount:4). However, when I try it on my 4-proc machine, I get a weird failure:
11:2>CSC : fatal error CS0042: Unexpected error creating debug information file 'C:\Common\obj\Debug\Common.PDB' -- 'C:\Common\obj\Debug\Common.pdb: The process cannot access the file because it is being used by another process. [C:\Common\Common.csproj]
Looking at the log, 2 msbuild nodes were trying to build that same csproj, and thus colliding on writing some output:
10>Project "C:\Utils\Utils.csproj" (10) is building "C:\Common\Common.csproj" (11) on node 4 (default targets).
46:2>Project "C:\Objects\Objects.csproj" (46:2) is building "C:\Common\Common.csproj" (11:2) on node 1 (default targets).
Why would MSBuild try to build the same project twice?
Cause: Someone was calling <MSBuild Projects="Common.csproj" Properties="..." />. Then, MSBuild thinks that it should build Common.csproj again with those different properties, and it happened to occur at the same time with the regular compilation of Common.csproj.
Fix: Call <MSBuild ... /> without those unneeded properties.
Test:
Common.targets
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
<Message Importance="high" Text="Build in $(MSBuildThisFile)" />
</Target>
<Target Name="After" DependsOnTargets="Build">
<Message Importance="high" Text="After in $(MSBuildThisFile)" />
</Target>
</Project>
Other.targets
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
<Message Importance="high" Text="Build in $(MSBuildThisFile)" />
<MSBuild Projects="common.targets" Targets="Build" /> <!-- regular builds -->
<MSBuild Projects="common.targets" <!-- custom invocation with properties -->
Targets="After"
Properties="myprop=myvalue"
/>
</Target>
</Project>
Run:
> msbuild other.targets /clp:verbosity=minimal
Build in other.targets
Build in common.targets
Build in common.targets <<<< Common.targets Build is invoked again
After in common.targets
And indeed, removing Properties="myprop=myvalue" solves the issue.
I found someone had added two project references (from the same project) and that apparently caused msbuild to build twice also.. something to watch out for

How get exec task output with msbuild

I'm trying to get simple output by exec task with msbuild:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Test">
<Exec Command="echo test output">
<Output TaskParameter="Outputs" ItemName="Test1" />
</Exec>
<Exec Command="echo test output">
<Output TaskParameter="Outputs" PropertyName="Test2" />
</Exec>
<Message Text="----------------------------------------"/>
<Message Text="#(Test1)"/>
<Message Text="----------------------------------------"/>
<Message Text="$(Test2)"/>
<Message Text="----------------------------------------"/>
</Target>
</Project>
But get next output:
echo test output
test output
echo test output
test output
----------------------------------------
----------------------------------------
----------------------------------------
How can I get output by my script?
Good news everyone! You can now capture output from <Exec> as of .NET 4.5.
Like this:
<Exec ... ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
</Exec>
Simply:
Add ConsoleToMsBuild="true" to your <Exec> tag
Capture the output using the ConsoleOutput parameter in an <Output> tag
Finally!
Documentation here
If you want to capture output to an array-like structure and not to a plain string where the output lines are separated by a semicolon, use ItemName instead of PropertyName:
<Exec ... ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" ItemName="OutputOfExec" />
</Exec>
I've gotten to the point where I'm so frustrated with the limitations of MSBuild, and the stuff that's supposed to work but doesn't (at least not in every context), that pretty much anytime I need to do anything with MSBuild, I create a custom build task in C#.
If none of the other suggestions are working, then you could certainly do it that way.
You can pipe the output to a file so to speak, and read it back.
echo test output > somefile.txt

Finding TeamCity Agent's working path for use in MSBuild script

I want to copy the output files from my build to a staging server, but I can't figure out how to find the path used by TeamCity to store the build output in from in MSBuild. Any help?
Thanks!
The $(teamcity_build_workingDir) property did it.
The best way is to upload the files to teamcity. Choose step1 (General Settings) and enter artifacts path. It should be something like /SourceOfProject/bin/releaese/*.dll.
I zip files before I upload them, because you only want to download 1 file that contains the complete build.
My build always has 2 steps in a nant - file.
Step1 - call msbuild
Step2 - use 7zip to create zip
<?xml version="1.0"?>
<project name="MyProjectBuild"
default="build" basedir="."
xmlns="http://nant.sf.net/release/0.85/nant.xsd">
<description>Build Script</description>
<target name="build" >
<exec program="C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" >
<arg value="MyProject\MyProject.csproj" />
<arg value="/t:Build" />
<arg value="/p:Configuration=Release" />
</exec>
<exec program="7z" >
<arg value="a" />
<arg value="MyProject\bin\release\buildresult.zip" />
<arg value="MyProject\bin\release\*.dll" />
</exec>
</target>
</project>
Anyway my working path is:
C:\Programme\TeamCity\buildAgent\work