TFS JAVA SDK14.0.3 - 如何从服务器获取远程文件并进行更改并再次签入
TFS JAVA SDK14.0.3 - How to fetch remote file from server and make changes and check in again
我在 team Foundation Server 中有一个名为 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;
}
这是 add/edit 个文件的片段
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 文件,我想要的是从服务器获取最新版本的 builds-main.xml 文件进行更改并进行签入。
另外,在本地路径中创建工作区后,我在该路径中看不到它,这是正确的行为吗?
谁能帮我解决这个问题?
当你要编辑一个由TFS版本控制的文件时,你需要在一个工作空间中获取该文件并在本地编辑它,然后将修改后的文件签入到TFS中。我不熟悉Java代码,我会添加c#代码供您参考。
- 为要编辑的文件夹创建工作区:
-
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();
}
}
}
使用Workspace.PendEdit Method编辑从TFS到工作区的文件。
使用 Workspace.CheckIn Method 签入未决更改。
查看下面的博客了解更多详情:
或者你可以参考这个案例使用TFS restapi更新文件:
我在 team Foundation Server 中有一个名为 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;
}
这是 add/edit 个文件的片段
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 文件,我想要的是从服务器获取最新版本的 builds-main.xml 文件进行更改并进行签入。
另外,在本地路径中创建工作区后,我在该路径中看不到它,这是正确的行为吗?
谁能帮我解决这个问题?
当你要编辑一个由TFS版本控制的文件时,你需要在一个工作空间中获取该文件并在本地编辑它,然后将修改后的文件签入到TFS中。我不熟悉Java代码,我会添加c#代码供您参考。
- 为要编辑的文件夹创建工作区:
-
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();
}
}
}
使用Workspace.PendEdit Method编辑从TFS到工作区的文件。
使用 Workspace.CheckIn Method 签入未决更改。
查看下面的博客了解更多详情:
或者你可以参考这个案例使用TFS restapi更新文件: