JGit 在 TransportConfigCallback 中抛出 ClassCastException

JGit throws ClassCastException in TransportConfigCallback

我正在尝试使用 JGit。我尝试遵循 http://www.codeaffine.com/2014/12/09/jgit-authentication/ 并且以下代码块抛出 ClassCastException

remoteRepository.setTransportConfigCallback(new TransportConfigCallback() {
    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }
});

异常:

java.lang.ClassCastException: org.eclipse.jgit.transport.TransportHttp cannot be cast to org.eclipse.jgit.transport.SshTransport

我错过了什么?我正在使用 JGit 版本 4.10.0.201712302008-r.

当你设置:cloneCommand.setURI("ssh://user@example.com/repo.git"); 指明url with ssh protocol, repo in github。 示例 - (ssh://git@github.com:githubtraining/hellogitworld.git)

参考这个https://github.com/allegro/axion-release-plugin/issues/101

该代码仅用于处理 SSH 连接。如果您通过其他协议连接,则需要调整代码以了解 transport 可能与 SshTransport.

不同

例如:

command.setTransportConfigCallback(new TransportConfigCallback() {
  @Override
  public void configure(Transport transport) {
    if(transport instanceof SshTransport) {
      SshTransport sshTransport = (SshTransport) transport;
      sshTransport.setSshSessionFactory(sshSessionFactory);
    } else if(transport instanceof HttpTransport) {
      // configure HTTP protocol specifics
    }
  }
} );