Java connection and operation Perforce

For the basic use of source control, you can use Perforce client tools, but in some application scenarios, you may need to use code to interact with the Perforce server, such as:

  • Automatic code fetching in the automatic deployment process (this scenario can generally also be implemented using the P4 command line tool)
  • Integrate P4 applications, for example, you need to select P4 paths, Labels, etc. in your own applications

This article introduces how to connect and operate Perforce in the Java language.

For a basic introduction to Perforce, please refer to: Peforce(Helix) Quick Introduction

Solution of connecting Java to P4

In the Java language, there are many libraries for operating P4. For example, the Jenkins team has developed a library before: perforce-plugin. This Java library can connect to the Perforce server and perform related operations.
insert image description here

The address of the library on Git is: https://github.com/jenkinsci/perforce-plugin. However, this library has been deprecated for several years. The suggested alternative is p4-plugin (https://github.com/jenkinsci/p4-plugin), which is also a library developed by Jenkins. Its main purpose is to implement Perforce and Jenkins integration.

The officially recommended Java library for connecting and operating Perforce is P4Java.
P4Java is a Java-native API for accessing Perforce SCM services from Java applications, servlets, plugins, and other Java contexts.

The official requirements for the relevant versions are:

  • Perforce server version >= 2015.1
  • JDK >= 1.8
  • SSL, Unlimited Strength JCE (Java Cryptography Extension) package for 256-bit encryption level SSL connections to secure Perforce servers.

P4Java usage steps and examples

  1. Import the dependency package in Maven, and add the following configuration to pom.xml:
		<dependency>
		    <groupId>com.perforce</groupId>
		    <artifactId>p4java</artifactId>
		    <version>2022.1.2423241</version>
		</dependency>	
  1. Complete example code:
/**  
* @Title: PerforceTests.java
* @Package com.osxm.je.topic.p4
* @Description: TODO
* @author XM
* @date 2023年5月2日 上午9:39:00
* @Copyright: 2023
* @version V1.0  
*/
package com.osxm.je.topic.p4;

import java.util.List;

import org.junit.Test;

import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.FileSpecOpStatus;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.option.server.GetDepotFilesOptions;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.ServerFactory;

public class PerforceTests {
	String serverUri = "p4java://localhost:1666";

	private String userName = "oscar";

	String password = "123456";

	@Test
	public void listFile() throws Exception {
		IOptionsServer server = ServerFactory.getOptionsServer(serverUri, null);
		server.connect();
		server.setUserName(userName);
		server.login(password);

		List<IFileSpec> fileList = server.getDepotFiles(FileSpecBuilder.makeFileSpecList("//depot/..."),
				new GetDepotFilesOptions());
		for (IFileSpec fileSpec : fileList) {
			if (fileSpec == null) {
				System.err.println("filespec is null");
				continue;
			}
			if (fileSpec.getOpStatus() == FileSpecOpStatus.VALID) {
				System.out.println(fileSpec.getDepotPathString());
			} else {
				System.err.println(fileSpec.getStatusMessage());
			}
		}
		server.disconnect();
	}
}

  • Here we use JUnit to run the test
  • The above code first uses the address to connect to Perforce, then uses the username/password to log in
  • getDepotFiles()Method The user obtains the files contained in the specified path of the remote library
  • Remember to release the connection after useserver.disconnect();
  1. The result of the operation is as follows:

insert image description here

reference



Guess you like

Origin blog.csdn.net/oscar999/article/details/130461471