[quote]perforce是种版本管理软件,提供啦完整的java api,可以方便我们做一些客户化开发,官方文档http://www.perforce.com/product/components/apis[/quote]
1 . pom 依赖
2. 下载文件示例
1 . pom 依赖
<dependency>
<groupId>com.perforce</groupId>
<artifactId>p4java</artifactId>
<version>2011.1.297684</version>
</dependency>
2. 下载文件示例
public class PerforceUtil {
private static Logger logger = Logger.getLogger(PerforceUtil.class);
public static void SaveFile(InputStream inputStream, File file) {
try {
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
out.write(b, 0, len);
}
inputStream.close();
out.close();
} catch (IOException e) {
logger.error("", e);
}
}
public static void downloadFile(String filePerforcePath, String destFolder) {
try {
IServer server = ServerFactory.getServer(ResourceUtil.getConfig().getProperty(Constants.P4PROTOCAL), null);
server.connect();
try {
server.setUserName(ResourceUtil.getConfig().getProperty(Constants.P4USERNAME));
server.login(ResourceUtil.getConfig().getProperty(Constants.P4PASSWORD));
IClient client = server.getClient(ResourceUtil.getConfig().getProperty(Constants.P4WORKSPACE));
if (client != null) {
server.setCurrentClient(client);
IServerInfo info = server.getServerInfo();
logger.info("server address: " + info.getServerAddress() + "\n" + "server version" + info.getServerVersion() + "\n"
+ "client address: " + info.getClientAddress() + "\n" + "client working directory: " + info.getClientCurrentDirectory()
+ "\n" + "client name: " + info.getClientName() + "\n" + "user name: " + info.getUserName());
List<IFileSpec> list =
client.sync(FileSpecBuilder.getValidFileSpecs(FileSpecBuilder.makeFileSpecList(new String[] {filePerforcePath})), true, false, false, false);
logger.info("download file size: " + list.size());
for (IFileSpec iFileSpec : list) {
String pathVersionInfo = iFileSpec.toString();
logger.info(iFileSpec);
File file = new File(iFileSpec.toString());
SaveFile(iFileSpec.getContents(true), file);
}
}
} catch (Exception exc) {
logger.error("", exc);
}
if (server != null) {
server.disconnect();
}
} catch (ConnectionException e) {
logger.error("", e);
} catch (NoSuchObjectException e) {
logger.error("", e);
} catch (ConfigException e) {
logger.error("", e);
} catch (ResourceException e) {
logger.error("", e);
} catch (AccessException e) {
logger.error("", e);
} catch (RequestException e) {
logger.error("", e);
} catch (URISyntaxException e) {
logger.error("", e);
}
}