JGit HTTPS克隆无法正常工作

时间:2013-11-08 07:53:48

标签: git github egit jgit

我正在使用Geritt GitHub网址: https://mycompany.com/gerrit/p/my-services.git

我通过终端克隆它工作正常: git clone https://mycompany.com/gerrit/p/my-services.git

当我使用Java代码使用JGit进行克隆时,它会抛出类似“ UnknownHostException ”的错误,

我想知道我什么时候在终端(https://mycompany.com/gerrit/p/my-services.git)克隆它工作正常,但是当我用JGit尝试它时,它会抛出错误? 为什么它没有在JGit上识别该域

错误日志是,

org.eclipse.jgit.api.errors.TransportException: https://mycompany.com/gerrit/p/my-services.git: cannot open git-upload-pack
        at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
        at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178)
        at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
        at com.test.AppTest.importGit(AppTest.java:128)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
        at $Proxy0.invoke(Unknown Source)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
Caused by: org.eclipse.jgit.errors.TransportException: https://mycompany.com/gerrit/p/my-services.git: cannot open git-upload-pack
        at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:499)
        at org.eclipse.jgit.transport.TransportHttp.openFetch(TransportHttp.java:308)
        at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136)
        at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
        at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1105)
        at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128)
        ... 34 more
Caused by: java.net.UnknownHostException: mycompany.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:570)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:411)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:525)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:966)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
        at org.eclipse.jgit.util.HttpSupport.response(HttpSupport.java:167)
        at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:462)
        ... 39 more

我的代码是,

//我使用以下代码来信任所有https网址

//创建一个不验证证书链的信任管理器

TrustManager[] trustAllCerts = new TrustManager[] { 
    new X509TrustManager() {     
        public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
            return null;
        } 
        public void checkClientTrusted( 
            java.security.cert.X509Certificate[] certs, String authType) {
            } 
        public void checkServerTrusted( 
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    } 
}; 

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {

}


String url = "https://mycompany.com/gerrit/p/my-services.git";
String username = "username";
String password = "password";
String checkoutLocation = "/Users/testing";
String branch = "my-services-sample-test";

UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider(username, password);
Git r = Git.cloneRepository().setDirectory(new File(checkoutLocation + "/clonned"))
.setCredentialsProvider(userCredential)
.setURI(url)
.setProgressMonitor(new TextProgressMonitor())
.setBranch(branch)
.call();
System.out.println("Clonning @ " + checkoutLocation);
r.getRepository().close();  

我使用依赖

    <dependency>
        <groupId>org.eclipse.jgit</groupId>
        <artifactId>org.eclipse.jgit</artifactId>
        <version>3.0.0.201306101825-r</version>
        <scope>compile</scope>
    </dependency>

1 个答案:

答案 0 :(得分:2)

我得到了解决方案。 Eclipse社区成员给了我一些想法。

以下帖子:http://www.eclipse.org/forums/index.php/m/1176786/#msg_1176786

我需要使用ProxySelector类设置JGit的代理配置。

ProxySelector.setDefault(new ProxySelector(){
    final ProxySelector delegate = ProxySelector.getDefault();

    @Override
    public List<Proxy> select(URI uri) {
        // Filter the URIs to be proxied

        if (uri.toString().contains("github") && uri.toString().contains("https")) {
            return Arrays.asList(new Proxy(Type.HTTP,
              InetSocketAddress.createUnresolved("localhost", 3128)));
        }
        if (uri.toString().contains("github") & uri.toString().contains("http")) {
            return Arrays.asList(new Proxy(Type.HTTP,
              InetSocketAddress.createUnresolved("localhost", 3129)));
        }

        // revert to the default behaviour
        return delegate == null ? Arrays.asList(Proxy.NO_PROXY) : delegate.select(uri);
    }

    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        if (uri == null || sa == null || ioe == null) {
            throw new IllegalArgumentException("Arguments can't be null.");
        }
    }
});

在我的情况下,"localhost"/3128localhost"/3129分别是我想要使用的HTTPS和HTTP代理URL /端口。

参考:JGit proxy configuration in code

效果很好。