The Teamprise TFS Ant Tasks are created and supported by Teamprise. For more information, go to the index page or visit teamprise.com.
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" />
This example shows:
typedef task and a local copy of the Teamprise
Ant tasks JAR file (e.g. @NAME.JARFILE@)basedir
<project basedir="." default="default-target">
<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/@NAME.JARFILE@" />
<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>
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}/@NAME.JARFILE@" />
<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>
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}/@NAME.JARFILE@" />
<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>
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}/@NAME.JARFILE@" />
<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>
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}/@NAME.JARFILE@" />
<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>
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}/@NAME.JARFILE@" />
<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>
This example shows several advanced techniques, including using the generic tfs task and the successproperty and
failureproperty attributes.
A common request is for the ability to test whether a TFS workspace exists. For instance, you may want to create a TFS workspace in your
build script, but the tfsworkspace task fails if you try to create a workspace that already exists. The best solution to this
problem is to set the failonerror attribute to false for that invocation of the tfsworkspace task. However,
in some situations, this may not be desirable. This example shows how you can set a property if a TFS workspace exists. The way this example works
is to use the generic tfs task to execute the tf workspaces command. The return value of this command is used to
determine whether the workspace exists or not. See the manual for more details on the generic tfs task and the attributes that
are used in this example.
<project basedir="." default="default-target">
<typedef resource="com/teamprise/ant/antlib.xml" classpath="${basedir}/@NAME.JARFILE@" />
<property name="tfs.server" value="http://tfs.mycompany.com:8080" />
<target name="checkworkspace">
<tfs
command="workspaces"
successproperty="workspaceexists"
failureproperty="workspacenotexists"
failonerror="false">
<arg value="my_workspace_name" />
</tfs>
</target>
<target name="onworkspaceexists" if="workspaceexists">
<echo message="workspace exists" />
</target>
<target name="onworkspacenotexists" if="workspacenotexists">
<echo message="workspace does not exist" />
</target>
<target name="default-target" depends="checkworkspace,onworkspaceexists,onworkspacenotexists">
</target>
</project>
Copyright © Teamprise, 2007-2008. All rights reserved.