2012-10-08 18 views
0

我用我的SMaL公司的項目的Java SVNKit(爲標記):如果標記不存在,Java SVNKit標記?

此刻及其工作:與

public void copy(String branchName, String dstTag, boolean isMove, String msg) throws SVNException, IOException { 
    String finalURL = getSvnUrl() + "tags/" + dstTag; 
    URL url = new URL(finalURL); 
    String loginPassword = getUsername() + ":" + getPassword(); 

    String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(loginPassword))); 
    URLConnection conn = url.openConnection(); 
    conn.setRequestProperty("Authorization", "Basic " + encoded); 
    HttpURLConnection urlConnect = null; 
    try { 
     urlConnect = (HttpURLConnection)conn; 
     if (urlConnect.getResponseCode() != HttpURLConnection.HTTP_OK) { 
      ourClientManager.getCopyClient().doCopy(SVNURL.parseURIDecoded(getSvnUrl() + branchName), 
        SVNRevision.HEAD, SVNURL.parseURIDecoded(finalURL), isMove, msg); 
      LOGGER.info("svn-tagging " + dstTag); 
     } else 
      LOGGER.info(dstTag + " Tag exists."); 

    } finally { 
     if (urlConnect != null) { 
      urlConnect.disconnect(); 
     } 
    } 
} 

我要檢查標籤的存在與否,我想做/使用SVNRepositorySVNClientManager而不是HttpURLConnection.HTTP_OK,有沒有人有任何想法?

回答

0

我會用新的「操作」 API

public static void main(String[] args) throws SVNException { 
    final SvnList list = new SvnOperationFactory().createList(); 
    list.setSingleTarget(SvnTarget.fromURL(SVNURL.parseURIEncoded("http://svn.apache.org/repos/asf/spamassassin/tags"))); 
    list.setReceiver(new ISvnObjectReceiver<SVNDirEntry>() { 
     public void receive(SvnTarget target, SVNDirEntry object) throws SVNException { 
      System.out.println(object.getName()); 
     } 
    }); 
    list.run(); 
} 
+0

嗨!謝謝,但最新的'操作'API?你能寄給我更多的信息嗎? – Fawi

+0

請看代碼片段,它可以作爲禮物。 – mstrap

0

由於@mstrap的建議,我也傾向於使用使用SvnOperationFactory創建操作,然後可運行操作的API。 這是我在我的gradle腳本中用於標記項目的Groovy代碼,它可以很容易地適應Java代碼。

我使用了try/catch結構來避免覆蓋標籤,因爲SvnList給了我一個SVNException,FS_NOT_FOUND錯誤,而不是一個空列表或null。這樣我就省下了一個單獨的操作。

workingCopyUrl是當前工作副本項目文件夾的SVN服務器上的URL,例如爲myhost/svnprojects/PROJECTNAME。務必使用服務器版本作爲源,否則最終可能會複製服務器上不存在的版本。但是,如果你沒有檢查你是否事先提交了所有的東西,你將會在工作副本上標記一個與你的版本不同的版本!

subDir是在項目中的子目錄。 projectName/branches/Featurebranch或projectName/trunk。

import org.tmatesoft.svn.core.wc.* 
import org.tmatesoft.svn.core.wc2.* 
import org.tmatesoft.svn.core.* 
def tagSVN(projectName, workingCopyUrl, subDir, tagName) { 
    final SvnOperationFactory svnOperationFactory = new SvnOperationFactory(); 
    final defaultAuthManager = SVNWCUtil.createDefaultAuthenticationManager("username", "password") 
    svnOperationFactory.setAuthenticationManager(defaultAuthManager) 

    try { 
     final SvnRemoteCopy tagOperation = svnOperationFactory.createRemoteCopy();  

     // Setup source (Working copy + subDir which may be trunk or a tag or a branch or any other sub-directory) 
     def sourceFolder = workingCopyUrl + '/' + subDir 
     def SVNURL sourceURL = SVNURL.parseURIEncoded(sourceFolder) 
     def SvnCopySource source = SvnCopySource.create(SvnTarget.fromURL(sourceURL), SVNRevision.HEAD) 
     tagOperation.addCopySource(source) 

     // Setup destination (SVN Server) 
     def tagDestinationPath = workingCopyUrl + "/tags/" + tagName 
     def SVNURL destinationURL = SVNURL.parseURIEncoded(tagDestinationPath)     
     tagOperation.setSingleTarget(SvnTarget.fromURL(destinationURL)) 

     // Make the operation fail when no 
     tagOperation.setFailWhenDstExists(true) 
     tagOperation.setCommitMessage("Tagging the ${projectName}") 
     tagOperation.run() 
    } catch (SVNException e) { 
     if (e.getErrorMessage() == SVNErrorCode.ENTRY_EXISTS) { 
      logger.info("Entry exists") 
     } 

    } finally { 
     svnOperationFactory.dispose() 
    } 
} 
相關問題