TFS JAVA SDK14.0.3 - 如何从服务器获取远程文件并进行更改并再次签入

时间:2018-04-22 13:47:48

标签: java tfs tfs-sdk

我在团队基础服务器中有一个名为master-builds的项目。 master-build文件夹有一个名为build-main.xml的xml文件 我想从服务器获取这些文件并对其进行一些更改(更改版本号)并再次检入,我如何使用TFS JAVA SDK 14.0.3实现它。

我尝试了文件夹中提供的样本,但无法实现。

这是创建工作区的代码段

public static Workspace createAndMapWorkspace(final TFSTeamProjectCollection tpc) {
    final String workspaceName = "SampleVCWorkspace" + System.currentTimeMillis(); //$NON-NLS-1$
    Workspace workspace = null;

    // Get the workspace
    workspace = tpc.getVersionControlClient().tryGetWorkspace(ConsoleSettings.MAPPING_LOCAL_PATH);

    // Create and map the workspace if it does not exist
    if (workspace == null) {
        workspace = tpc.getVersionControlClient().createWorkspace(
            null,
            workspaceName,
            "Sample workspace comment", //$NON-NLS-1$
            WorkspaceLocation.SERVER,
            null,
            WorkspacePermissionProfile.getPrivateProfile());

        // Map the workspace
        final WorkingFolder workingFolder = new WorkingFolder(
            ConsoleSettings.MAPPING_SERVER_PATH,
            LocalPath.canonicalize(ConsoleSettings.MAPPING_LOCAL_PATH));
        workspace.createWorkingFolder(workingFolder);
    }

    System.out.println("Workspace '" + workspaceName + "' now exists and is mapped"); //$NON-NLS-1$ //$NON-NLS-2$

    return workspace;
}

这是添加/编辑文件的代码段

public static void getLatest(final Workspace workspace) {
    final ItemSpec spec = new ItemSpec(ConsoleSettings.MAPPING_LOCAL_PATH, RecursionType.FULL);
    final GetRequest request = new GetRequest(spec, LatestVersionSpec.INSTANCE);
    workspace.get(request, GetOptions.NONE);
}

public static File addFile(final Workspace workspace) {

    // Create the file locally
    final File newFile = new File(ConsoleSettings.MAPPING_LOCAL_PATH, "SampleAppFile"); //$NON-NLS-1$
    writeFileContents(newFile, "", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

    // Pend an add
    // The encoding is passed as null and the add will detect the encoding
    // of the file
    workspace.pendAdd(new String[] {
        newFile.getAbsolutePath()
    }, false, ENCODING, LockLevel.UNCHANGED, GetOptions.NONE, PendChangesOptions.NONE);

    // Checkin the pending change
    final int cs = checkinPendingChanges(workspace, "Adding a sample file"); //$NON-NLS-1$

    System.out.println("Added file " + newFile.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$

    return newFile;
}

public static void editFile(final Workspace workspace, final File file) {
    // Pend edit
    final ItemSpec fileSpec = new ItemSpec(file.getAbsolutePath(), RecursionType.NONE);
    workspace.pendEdit(
        new ItemSpec[] {
            fileSpec
    },
        LockLevel.UNCHANGED,
        ENCODING,

        GetOptions.NONE,
        PendChangesOptions.NONE);

    // Edit the file
    writeFileContents(file, "Edited this file at " + new Date().toString(), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

    // Checkin the pending change
    final int cs = checkinPendingChanges(workspace, "Editing the sample file"); //$NON-NLS-1$

    System.out.println("Edited file " + file.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$
}

这些是我的问题

我能够成功将新文件添加到远程服务器,但无法编辑现有的远程文件。

如果我调用editFile后跟addFile方法它会更新,但我不想每次都添加enw文件,我想要的是从服务器获取最新版本的buils-main.xml文件并进行更改和办理入住手续。

此外,在本地路径中创建工作区后,我无法在该路径中看到它,这是正确的行为吗?

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

如果要编辑由TFS控制的版本的文件,则需要在工作区中获取文件并在本地编辑它,然后将修改后的文件签入TFS。我不熟悉Java代码,我会添加c#代码供您参考。

  1. 为要编辑的文件夹创建工作区:
  2. -

    using Microsoft.TeamFoundation.Client;
    using System;
    using Microsoft.TeamFoundation.VersionControl.Client;
    
    namespace TestCaseProject
    {
        class Program
        {
            static void Main(string[] args)
            {
                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection"));
                var versioncontrols = tfs.GetService<VersionControlServer>();
    
                var workspace = versioncontrols.CreateWorkspace("test", "Cece Dong");
    
                String ServerFolder = @"$/ScrumProject/TestCaseProject";
                String LocalFolder = @"E:\test";
    
                WorkingFolder workfolder = new WorkingFolder(ServerFolder, LocalFolder);
                workspace.CreateMapping(workfolder);
    
                workspace.Get();
    
    
            }
        }
    }
    
    1. 使用Workspace.PendEdit Method编辑从TFS到工作区的文件。

    2. 使用Workspace.CheckIn Method签入待处理的更改。

    3. 查看下面的博客了解更多详情:

      https://blogs.msdn.microsoft.com/buckh/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer/

      或者你可以参考这个案例来使用TFS rest api来更新文件:

      Updating a file using REST Api Visual Studio Team Services