淘先锋技术网

首页 1 2 3 4 5 6 7

首先要使用svn的方法,就需要在工程中增加svn的jar包:  jar包到http://subversion.apache.org/download/#recommended-release  下载   ;  source包可以到   http://subversion.apache.org/download/        下载

jar导入工程后,就可以使用了


新建两个java文件:

1.SvnUserPassPromptImpl

/**
 * @CLASS:SvnUserPassPromptImpl
 * @DESCRIPTION:用	PromptUserPassword的回调函数来接收用户名和密码
 * @AUTHOR:yehui
 * @VERSION:v1.0
 * @DATE:2013-5-2 上午11:14:32
 **/              
 
 
public class SvnUserPassPromptImpl implements PromptUserPassword{
	/**
	 * svn 用户ID
	 */
	private String Svn_UserName ;
	/**
	 * svn 用户名
	 */
	private String Svn_Password ;
	
	public SvnUserPassPromptImpl(String Svn_UserName,String Svn_Password )
	{
		this.Svn_UserName = Svn_UserName;
		this.Svn_Password = Svn_Password;
	}
	
	public String getSvn_UserName() {
		return Svn_UserName;
	}

	public void setSvn_UserName(String svn_UserName) {
		Svn_UserName = svn_UserName;
	}

	public String getSvn_Password() {
		return Svn_Password;
	}

	public void setSvn_Password(String svn_Password) {
		Svn_Password = svn_Password;
	}

	@Override
	public String askQuestion(String arg0, String arg1, boolean arg2) {
		return null;
	}

	@Override
	public boolean askYesNo(String arg0, String arg1, boolean arg2) {
		return false;
	}

	@Override
	public String getPassword() {
		return null;
	}

	@Override
	public String getUsername() {
		return null;
	}

	/* (non-Javadoc)   
	 * @see org.tigris.subversion.javahl.PromptUserPassword#prompt(java.lang.String, java.lang.String)   
	 */
	@Override
	public boolean prompt(String arg0, String arg1) {
		return false;
	}

}

2.SvnApp具体操作的工具类

public class SvnApp implements ISvn {

	/**
	 * @description:用于加载动态的链接库 ,svn的dll文件
	 * @author:yehui void
	 */

	private static native void initNativeLibrary();

	protected final Log log = LogFactory.getLog(this.getClass());
	private SVNClient SvnClient;

	private SvnUserPassPromptImpl auth;
	/**
	 * @description:登陆成功的标识符
	 */
	private boolean authFlag = true;

	/**
	 * @description: svn 先做清理,再做更新
	 */
	public SvnApp() {

		try {
			SvnClient = new SVNClient();
			SvnClient.cleanup(SVN_DES_PATH);

			svn_update(SVN_DES_PATH);
		} catch (Exception e) {
			log.error(e.toString(), e);
		}

	}

	/**
	 * @description: 使用用户名和密码登陆svn
	 */
	public SvnApp(String userid, String password) {
		try {
			SvnClient = new SVNClient();
			SvnClient.username(userid);
			SvnClient.password(password);
			auth = new SvnUserPassPromptImpl(userid, password);
			SvnClient.setPrompt(auth);
			svn_update(SVN_DES_PATH);
		} catch (Exception e) {
			setAuthFlag(false);
			log.error(e.toString(), e);

		}

	}

	/**@description:	svn delete文件
	 * @author:yehui
	 * @param paths     需要delete的文件路径
	 * @param message   如果delete成功,将提交一条信息
	 * void
	 */
	
	public void svn_delete(String[] paths, String message) {

		try {
			Map revmap = new HashMap();
			SvnClient.remove(paths, message, false, false, revmap);
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}

	}

	/**
	 * @description: svn add文件
	 * @return:void
	 */
	public void svn_add(String File_Path) {
		try {

			SvnClient.add(File_Path, Depth.infinity, false, false, false);
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
	}

	/**
	 * @description: svn commit文件
	 * @return:long
	 */
	public long svn_commit(String[] path, String message) {
		long num = 0;
		try {

			String[] array = new String[1];
			Map revmap = new HashMap();
			num = SvnClient.commit(path, message, Depth.infinity, false, false,
					null, revmap);
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
		return num;
	}

	/**
	 * @description: svn update文件
	 * @return:void
	 */
	public void svn_update(String path) throws ClientException {

		SvnClient.update(path, Revision.HEAD, Depth.infinity, false, false,
				false);

	}

	/**
	 * @description: svn checkout文件
	 * @return:void
	 */
	public void svn_checkout(String Svn_Checkout_Path, String Svn_Des_Path) {
		try {

			SvnClient.checkout(Svn_Checkout_Path, Svn_Des_Path, Revision.HEAD,
					Revision.HEAD, Depth.infinity, false, false);

		} catch (ClientException e) {
			log.error(e.toString(), e);
		}

	}

	/**
	 * @description: svn lock文件
	 * @return:void
	 */
	public void svn_lockSingleFile(String path, String comment, boolean force) {
		try {
			SvnClient.lock(new String[] { path }, comment, force);

		} catch (ClientException e) {
			log.error(e.toString(), e);
		}

	}

	/**
	 * @description: svn unlock文件
	 * @return:void
	 */
	public void svn_unlock(String path, boolean force) {
		try {
			SvnClient.unlock(new String[] { path }, force);
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
	}

	/**
	 * @description: svn 取得lock文件的用户
	 * @return:String
	 */
	public String svn_getLockOwner(String path) {

		String obj = null;
		Status st;
		try {
			st = SvnClient.singleStatus(path, true);
			obj = st.getLockOwner();

		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
		return obj;
	}

	/**
	 * @description: svn 文件的版本列表
	 * @return:List<Revision>
	 */
	public List<Revision> svn_log_revision(String path) {
		List<Revision> list = new ArrayList<Revision>();
		try {
			LogMessage[] logs = SvnClient.logMessages(path, Revision.START,
					null);
			for (LogMessage l : logs) {
				Revision.Number num = l.getRevision();
				list.add(num);
			}
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
		return list;
	}

	/**
	 * @description: svn 文件的版本列表
	 * @return:List<Revision>
	 */
	public List<String[]> svn_log_list(String path) {
		List<String[]> list = new ArrayList<String[]>();

		try {
			LogMessage[] logs = SvnClient.logMessages(path, Revision.START,
					null);
			for (LogMessage l : logs) {
				String[] array = { l.getRevisionNumber() + "", l.getAuthor(),
						DateUtils.format(l.getDate(), "yyyy/MM/dd  HH:mm:ss") };
				list.add(array);
			}
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
		return list;
	}

	/**
	 * @description: svn merge文件
	 * @return:void
	 */
	public void svn_merge(String path, Revision from, Revision to) {
		RevisionRange range = new RevisionRange(from, to);
		try {
			SvnClient.merge(path, Revision.HEAD, new RevisionRange[] { range },
					path, false, Depth.infinity, false, false, false);
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
	}

	/**
	 * @description: svn checkin文件
	 * @return:void
	 */
	public void svn_revert(String path) {
		try {
			SvnClient.revert(path, Depth.infinity, null);
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}
	}

	/**
	 * @description: svn 判断是否是需要add文件
	 * @return:boolean
	 */
	public boolean svn_isAdded(String path) {
		Status st;
		boolean flag = false;
		try {
			st = SvnClient.singleStatus(path, true);
			String str = st.getLastCommitAuthor();
			if (!StringUtils.isBlank(str)) {
				flag = true;
			}
		} catch (ClientException e) {
			log.error(e.toString(), e);
		}

		return flag;
	}

	public SVNClient getSvnClient() {
		return SvnClient;
	}

	public void setSvnClient(SVNClient svnClient) {
		SvnClient = svnClient;
	}

	public boolean isAuthFlag() {
		return authFlag;
	}

	public void setAuthFlag(boolean authFlag) {
		this.authFlag = authFlag;
	}
}

linux环境下可能需要导入dll文件,这里就不做介绍了,希望对大家有所帮助


ISvn只是从properties文件中取出svn的地址,和准备将文件checkout的路径

public interface ISvn {
public static final String SVN_DES_PATH = Utils.getsvnString("SVN_DES_PATH");
public static final String SVN_CHECKOUT_PATH = Utils.getsvnString("SVN_CHECKOUT_PATH");
}