Microsoft TFS Tasks: Complete Examples


Contents

Note: These examples assume that the tf executable is in the path and Windows integrated authentication is being used. To see how to explicitly specify the location of the tf executable or login credentials, see the manual or troubleshooting documents. The following properties could be added to each example to illustrate setting these configuration values:

<property name="tfs.vc.executable" value="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\tf.exe" />
<property name="tfs.user" value="MYDOMAIN\autobuild" />
<property name="tfs.password" value="secret" />

Simple Recursive Get

This example shows:

<project basedir="." default="default-target">

	<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/teamprise-ant-1.0.jar" />

	<property name="tfs.server" value="http://tfs.mycompany.com:8080" />

	<target name="default-target">
		<tfsworkspace mode="delete" workspace="antworkspace" failonerror="false" />
		<tfsworkspace mode="create" workspace="antworkspace" />

		<delete dir="${basedir}/files" />
		<mkdir dir="${basedir}/files" />

		<tfsworkfold workspace="antworkspace" localpath="${basedir}/files" serverpath="$/demo" />

		<tfsget localpath="${basedir}/files/project" />
	</target>

</project>

Get With Label

This example is an expanded version of the first example. In this example, the build script first creates a new label and applies it to the target server folder. The get is then done using that label.

<project basedir="." default="default-target">

	<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/teamprise-ant-1.0.jar" />

	<property name="tfs.server" value="http://tfs.mycompany.com:8080" />

	<target name="default-target">

		<tstamp>
			<format pattern="MM_dd_yyyy_HH_mm_ss" property="label.tstamp" timezone="UTC" />
		</tstamp>

		<property name="build.label" value="${label.tstamp}_autobuild" />

		<echo message="adding autobuild label: ${build.label}" />

		<tfslabel name="${build.label}" recursive="true" item="$/demo/project" />

		<tfsworkspace mode="delete" workspace="antworkspace" failonerror="false" />
		<tfsworkspace mode="create" workspace="antworkspace" />

		<delete dir="${basedir}/files" />
		<mkdir dir="${basedir}/files" />

		<tfsworkfold workspace="antworkspace" localpath="${basedir}/files" serverpath="$/demo" />

		<tfsget localpath="${basedir}/files/project" version="L${build.label}" />

	</target>

</project>

Check-out, modify, and check-in

This example demonstrates using the tfscheckout and tfscheckin commands. The Ant script first performs a recursive get. It then checks out a .properties file, uses the Ant propertyfile task to modify the local file, and checks in the change. A script like this could be used, for instance, to automatically increment a version number stored in a properties file each time a build is done.

<project basedir="." default="default-target">

	<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/teamprise-ant-1.0.jar" />

	<property name="tfs.server" value="http://tfs.mycompany.com:8080" />

	<target name="default-target">

		<tfsworkspace mode="delete" workspace="antworkspace" failonerror="false" />
		<tfsworkspace mode="create" workspace="antworkspace" />

		<delete dir="${basedir}/files" />
		<mkdir dir="${basedir}/files" />

		<tfsworkfold workspace="antworkspace" localpath="${basedir}/files" serverpath="$/demo" />

		<tfsget localpath="${basedir}/files/project" />

		<tfscheckout item="${basedir}/files/project/test.properties" />

		<propertyfile file="${basedir}/files/project/test.properties">
			<entry key="version.minor" value="1" type="int" operation="+" />
		</propertyfile>

		<tfscheckin item="${basedir}/files/project/test.properties" comment="autobuild updated version number" />

		<property file="${basedir}/files/project/test.properties" />

		<echo message="version is now ${version.major}.${version.minor}" />

	</target>

</project>

Add a new file to source control

This example shows how to use the tfsadd task to add a new file to the server.

<project basedir="." default="default-target">

	<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/teamprise-ant-1.0.jar" />

	<property name="tfs.server" value="http://tfs.mycompany.com:8080" />

	<target name="default-target">

		<tstamp>
			<format pattern="MM_dd_yyyy_HH_mm_ss" property="test.tstamp" timezone="UTC" />
		</tstamp>

		<property name="output.file" value="${basedir}/files/project/${test.tstamp}_autobuild.properties" />

		<tfsworkspace mode="delete" workspace="antworkspace" failonerror="false" />
		<tfsworkspace mode="create" workspace="antworkspace" />

		<delete dir="${basedir}/files" />
		<mkdir dir="${basedir}/files" />

		<tfsworkfold workspace="antworkspace" localpath="${basedir}/files" serverpath="$/demo" />

		<tfsget localpath="${basedir}/files/project" />

		<echo message="adding file: ${output.file}" />

		<propertyfile file="${output.file}" comment="generated and added by the autobuild">
			<entry key="test" value="value" />
		</propertyfile>

		<tfsadd item="${output.file}" />

		<tfscheckin item="${output.file}" />

	</target>

</project>

Simple history to a property

This example shows how to use the tfshistory task to obtain history for an item. In this example, the history information is stored in a property which the Ant script can use after tfshistory has finished. For instance, a script could send an email with the contents of the property.

<project basedir="." default="default-target">

	<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/teamprise-ant-1.0.jar" />

	<property name="tfs.server" value="http://tfs.mycompany.com:8080" />

	<target name="default-target">

		<tfshistory item="$/demo/project" outputproperty="item.history" />

		<echo message="history is: ${line.separator}${item.history}" />

	</target>

</project>

Get based on changeset from history

This example shows how to combine the tfshistory and tfsparsehistory tasks. In this example, the tfshistory task is used get the latest history information for a given server item, and then the tfsparsehistory task is used to parse out the changeset number and store it in a property. A recursive get is then done, and the version for the get is a changeset version using the changeset previously retrieved. The changeset property could be further used, for instance, to create a build number.

<project basedir="." default="default-target">

	<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/teamprise-ant-1.0.jar" />

	<property name="tfs.server" value="http://tfs.mycompany.com:8080" />

	<target name="default-target">

		<tfshistory 
			item="$/demo/project" 
			outputproperty="historydata" 
			logerror="true"
			stopafter="1" />

		<tfsparsehistory 
			historyinputvalue="${historydata}" 
			historyoutputproperty="cset" 
			rowindex="1" 
			columnname="changeset" />

		<echo message="latest changeset for $/demo/project is: ${cset}" />

		<tfsworkspace mode="delete" workspace="antworkspace" failonerror="false" />
		<tfsworkspace mode="create" workspace="antworkspace" />

		<delete dir="${basedir}/files" />
		<mkdir dir="${basedir}/files" />

		<tfsworkfold workspace="antworkspace" localpath="${basedir}/files" serverpath="$/demo" />

		<tfsget localpath="${basedir}/files/project" version="C${cset}" />

	</target>

</project>