Using Team Foundation Server 2010 Version Control for a Java Project

Posted by: Subodh Sohoni , on 11/8/2011, in Category Visual Studio, VSTS & TFS
Views: 93767
Abstract: In this article, I am going to walk you through the entire process of how to use the Team Explorer Everywhere plug-in in Team Foundation Server 2010 (TFS) for version control of a Java project .

In this article I am going to walk you through the entire process of how to use TFS 2010 for version control of a typical Java project. I will use Team Explorer Everywhere 2010 that is a plug-in to Eclipse and facilitates access to TFS 2010. It can be downloaded from here. The file "TFSEclipsePlugin-UpdateSiteArchive-10.1.0.zip" contains the Team Foundation Server plug-in for Eclipse. The file "TEE-CLC-10.1.0.zip" contains the Cross-platform command-line client for Team Foundation Server.

Once the plug-in is downloaded, you can install it using the Help menu of Eclipse. Now we will create a Java Project and add it to TFS 2010 Source and Version Control

Step 1: Start Eclipse and when you see the dialogue box to select a workspace, select the default that is shown and click the OK button.

tfs-version-java-1

 

Step 2: When the Welcome page is shown, you may either click on Go to workbench icon or simply close that page.

Step 3: Eclipse opens in a Java perspective

tfs-version-java-2
 
Step 4: From the toolbar, select the New icon drop down and select Java Project

tfs-version-java-3

Or select the File – New – Java Project

Step 5: A wizard to create a new Java project gets started. We are going to create a simple Java Project which validates a password when the user attempts to set or change the password. On the first page, provide the name of the project as Password Validator and then click the Next button

tfs-version-java-5

Step 6: Select the default settings on next page too and click on the Finish button.

tfs-version-java-6

Step 7: Package Explorer shows the project and it’s ‘src’ folder. Right click on that src folder and select New > Package

tfs-version-java-7

Step 8: Provide a name to package as passwordValidator and click the finish button

 tfs-version-java-8

Step 9: Now select the package name in the Package Explorer and right click on it. Select New > Class to start the wizard to create a new class file.

 tfs-version-java-9

Step 10: Give the name PwdValidator to the class and select the check box to create a “public static void main” method.

tfs-version-java-10

Click on the Finish button to create the new class.

Step 11: In the Package Explorer right click on the package name “Password Validator” and from the context sensitive menu select Team > Share Project

tfs-version-java-11

Step 12: In the Select Repository Type dialogue select Team Foundation Server option and click Next button

Step 13: Click Next on the license agreement page and then check the checkbox against “Use full featured trial version of Microsoft Team Explorer Everywhere 2010” and click Next.

Step 14: Enter machine name (WIN-GS9GMUJITS8 in my case) as the name of TFS and keep the logon credentials as ‘Authenticate as currently logged on user’. Click Next

TFVC4JAVA_14

Step 15: Wizard will connect to TFS 2010 and populate the list of servers, team project collections and team project for default collection. Check the box against “Tailspin toys” team project and click Next.

TFVC4JAVA_15

Step 16: Let the workspace name be same as the machine name and click Next

TFVC4JAVA_16

Step 17: Select the Team Project – Tailspin Toys as the server path so that it will appear as $/Tailspin Toys/Password Validator at the bottom.

TFVC4JAVA_17

Step 18: Click Next and Finish to close the wizard.

Step 19: Now click the Perspective Selector and select Other

TFVC4JAVA_18

Step 20: In the Open Perspective dialogue box, select Team Foundation Server Exploring perspective and click OK

TFVC4JAVA_19

Step 21: You will be able to see the Team Explorer in place of Package Explorer and you will also see the Pending Changes window where all the created files will be shown. After it populates the entire data from TFS, click Check-in button.

TFVC4JAVA_20

Step 22: Confirm the check-in action so that all the files will be checked in. Now let us add Check-in Policy. Do Checkout, GET and Check-in Operations.

Step 23: Add another class named PwdValidatorLogic to the project. Do not add main method in this class.

Add a method named Validate as follows in PwdValidatorLogic class.

image

Step 24: In the main method of PwdValidator class, create the instance of PwdValidationLogic class and call the method validate. Code of the main method should be as follows:

public static void main(String[] args) {
  String result = "";
  PwdValidatorLogic Logic = new PwdValidatorLogic();
  Boolean validity = Logic.Validate(args[0]);
  if (validity) { result = "Acceptable"; }
  else {
    result = "Not Acceptable";
  }
  System.out.println("Password is " + result);
}


Step 25: Check in all the newly added code, add comments as validation is added.  From Team Explorer, right click on the project name and select Check in Policies

TFVC4JAVA_21

In Check-in policies dialog box, click on the Add button

TFVC4JAVA_22

and select the option for Work Item Policy, click on the Ok button.

TFVC4JAVA_23

Step 26: Right click the Password Validator project in the source control explorer and select to Checkout for edit.

Keep the settings of existing lock. Again right click on the project and select to Get Latest Version.
You will get a message that all files are up to date.

Change the code of the main method as follows:

public static void main(String[] args) {
if (args.length > 0) {
String result = "";
PwdValidatorLogic Logic = new PwdValidatorLogic();
Boolean validity = Logic.Validate(args[0]);
if (validity)
{
     result = "Acceptable";
}
else
{
     result = "Not Acceptable";
}
System.out.println("Password is " + result);
}
else {
   System.out.println("No password provided to validate");
}
}

Step 27: Try Check in again. You can save the file locally but you get a message saying you cannot checkin because a policy is not satisfied. Click on the Cancel button

TFVC4JAVA_25

Step 28: Go to Policy Warning tab in the Pending Changes Window and you will be able to see the Policy Failure

tfs-version-java-26

Remove the check in policy and check in the code. Right click on Password Validator and select Check out for Edit. View various locks available.

tfs-version-java-27

We can now specify particular lock while checking out.

When the Welcome page is shown, you may either click on Go to workbench icon or simply close that page. Eclipse opens in a Java perspective   From the toolbar, select the New icon drop down and select Java Project Or select the File – New – Java Project A wizard to create a new Java project gets started. We are going to create a simple Java Project which validates a password when the user attempts to set or change the password. On the first page, provide the name of the project as Password Validator and then click the Next button Select the default settings on next page too and click on the Finish button. Package Explorer shows the project and it’s ‘src’ folder. Right click on that src folder and select New > Package Provide a name to package as and click the finish button  Now select the package name in the Package Explorer and right click on it. Select New > Class to start the wizard to create a new class file.  Give the name PwdValidator to the class and select the check box to create a “public static void main” method. Click on the Finish button to create the new class. In the Package Explorer right click on the package name “Password Validator” and from the context sensitive menu select Team > Share Project In the Select Repository Type dialogue select Team Foundation Server option and click Next button Click Next on the license agreement page and then check the checkbox against “Use full featured trial version of Microsoft Team Explorer Everywhere 2010” and click Next. Enter machine name (WIN-GS9GMUJITS8 in my case) as the name of TFS and keep the logon credentials as ‘Authenticate as currently logged on user’. Click Next Wizard will connect to TFS 2010 and populate the list of servers, team project collections and team project for default collection. Check the box against “Tailspin toys” team project and click Next. Let the workspace name be same as the machine name and click Next Select the Team Project – Tailspin Toys as the server path so that it will appear as $/Tailspin Toys/Password Validator at the bottom. Click Next and Finish to close the wizard. Now click the Perspective Selector and select Other In the Open Perspective dialogue box, select Team Foundation Server Exploring perspective and click OK You will be able to see the Team Explorer in place of Package Explorer and you will also see the Pending Changes window where all the created files will be shown. After it populates the entire data from TFS, click Check-in button. Confirm the check-in action so that all the files will be checked in. Now let us add Check-in Policy. Do Checkout, GET and Check-in Operations. Add another class named PwdValidatorLogic to the project. Do not add main method in this class. Add a method named Validate as follows in PwdValidatorLogic class. In the main method of PwdValidator class, create the instance of PwdValidationLogic class and call the method validate. Code of the main method should be as follows: Check in all the newly added code, add comments as validation is added.  From Team Explorer, right click on the project name and select Check in Policies In Check-in policies dialog box, click on the Add button and select the option for Work Item Policy, click on the Ok button. Right click the Password Validator project in the source control explorer and select to . Keep the settings of existing lock. Again right click on the project and select to Get Latest Version. You will get a message that all files are up to date. Change the code of the main method as follows: Try Check in again. You can save the file locally but you get a message saying you cannot checkin because a policy is not satisfied. Click on the Cancel button Go to Policy Warning tab in the Pending Changes Window and you will be able to see the Policy Failure Remove the check in policy and check in the code. Right click on Password Validator and select Check out for Edit. View various locks available. We can now specify particular lock while checking out.

 

Labelling, Branching, Merging and View History

Let us now see how we can do Labelling, Branching, Merging and View History
 
Step 29: Right click on Password Validator in Source Control Explorer and select the option of Properties

tfs-version-java-28

Information about pending changes, if any branches available and which workspace it belongs to is available here. Click on Cancel button.

Step 30: Right click on Password Validator and select Branch

tfs-version-java-29

Click Ok button.

Step 31: Check-in pending changes to confirm the branching

tfs-version-java-30

Source control explorer now will show two branches of PasswordValidator project.

tfs-version-java-31

Eclipse also works with it’s own workspaces and it does not allows projects of same name to be opened in it’s workspace. We will create a new workspace.

Step 32: Click on File menu and select Switch Workspace. This will show you an existing workspace that is in a folder C:\Users\abuobe\workspace.

tfs-version-java-32

Click on the Browse button, go one step up in the tree view to the user name Abu Obeida Bakhach (Dev) and create a new folder under it named BranchWorkspace

tfs-version-java-33

Click the OK button twice to complete the workspace creation. Eclipse will restart and open in a new workspace.

Step 33: Now to add the Java Project that is under the branch that you have created, Select File > Import menu. In the dialogue that opens, select Team > Team Foundation Server and then Select the Tailspin Toys folder to go to the Next screens. Select the Default Workspace of TFS (machine name) and Click Next. Drill down to the new branch that contains Java Project and Click Next.

tfs-version-java-34

Step 34: Select the project that you want to import (Password Validator-branch) and Click Finish.  Now the Java Project is imported under the new Eclipse workspace and opened for editing.

Step 35: Make change in the code of PasswordValidatorLogic.java so that now the acceptable password length is 5 to 10 characters instead of 6 to 10.

tfs-version-java-36

Step 36: Checkin this code by Right Clicking on the file name > Team > Checkin Pending Changes

tfs-version-java-37
tfs-version-java-38

Step 37: Reopen Team Foundation Server Exploring perspective and open source control of Tailspin Toys project. Right click on the branch and select Merge

tfs-version-java-39

Let the Target Branch be as default which is the parent branch and click on the Next button. Accept Latest Version as the criterion and Finish the Merge operation. Checkin pending Changes.

Step 38: Go back to File > Switch Workspace and select the original Eclipse workspace. Open Java perspective and confirm that the the change made in the branch is merged in the parent.

Step 39: Open the Source Control Explorer and Select Password Validation Project. Right Click on it and Select Apply Label (Ctrl + Alt + Y).

Give a name to the label as Demo Label and also provide some comments. Click on the Create button.

tfs-version-java-41

Step 40: Right click again on the same project branch and select Find Label. Provide Demo Label name to find the label. Click on the Find button.

tfs-version-java-42

From the Results, select Demo Label and click on the Edit button.

tfs-version-java-43

From the list of files that are shown, select the PasswordValidatorLogic.java file and click on the Remove button.

tfs-version-java-44 

Step 41: Click now on the Add button. Browse to the same file and change the version to Changeset.

tfs-version-java-45

Click on the Find button and select the previous changeset instead of the latest one. And click the OK button

tfs-version-java-46

Click OK button again so that now the PasswordValidatorLogic.java old version becomes part of the labelled version. Click OK button to close the Label Editor.

tfs-version-java-47

Step 42: Right click on the same branch and select View History. Select the latest changeset and double click on it. The changeset details will be shown.

tfs-version-java-48

and that’s it!!

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Subodh Sohoni, Team System MVP, is an MCTS – Microsoft Team Foundation Server – Configuration and Development and also is a Microsoft Certified Trainer(MCT) since 2004. Subodh has his own company and conducts a lot of corporate trainings. He is an M.Tech. in Aircraft Production from IIT Madras. He has over 20 years of experience working in sectors like Production, Marketing, Software development and now Software Training. Follow him on twitter @subodhsohoni


Page copy protected against web site content infringement by Copyscape


User Feedback

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel