This article explains about how to clone the git with ssh keys in java. This might be really helpful if you working on any git related works like enhancing on git services / add (or) modifying some files for some of your custom needs in git.
Table of Contents
Dependency
<dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit.ssh.jsch</artifactId> <version>5.11.1.202105131744-r</version> </dependency>
Latest can be downloaded from here
Generate SSH Keys (Private & Public Keys):
ssh-keygen -t rsa -m PEM
This will ask for the key name and password. Please do remember the password as the same should be given in the java code later.
Add your ssh key
ssh-add git-ssh // to add the key ssh-add -l // to check the existing keys
Possible Errors & Solutions
Error connecting to agent: No such file or directory
unable to start ssh-agent service, error :1058:
If you encounter any of the above errors then open powershell in administrator mode and run these commands,
Set-Service ssh-agent -StartupType Manual Start-Service ssh-agent
Copy public ssh key to your github /bitbucket
This command copies your public ssh key to clipboard.
type git-ssh.pub | clip
github.com > settings > SSH and GPG keys
Java code to clone git using ssh keys
package com.ngdeveloper.java_git_ssh; import java.io.File; import java.io.IOException; import java.nio.file.Files; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.InvalidRemoteException; import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.OpenSshConfig; import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.SshTransport; import org.eclipse.jgit.transport.Transport; import org.eclipse.jgit.util.FS; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; public class GitDownload { private static final String sshPrivateFilePath = "D:\\github\\ssh-key\\git-ssh"; // replace with your ssh private // key path private static final String sshPassword = "ngdeveloper"; // replace with your ssh key password private static final String sshGitURI = "git@github.com:ngdeveloper-projects/android-toast.git"; // replace with // your git // project URI public static void main(String[] args) throws InvalidRemoteException, TransportException, GitAPIException, IOException { File workingDir = Files.createTempDirectory("workspace").toFile(); TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback(); Git git = Git.cloneRepository().setDirectory(workingDir).setTransportConfigCallback(transportConfigCallback) .setURI(sshGitURI).call(); System.out.println("Project cloned through SSH in: " + workingDir.getPath()); } private static class SshTransportConfigCallback implements TransportConfigCallback { private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host hc, Session session) { session.setConfig("StrictHostKeyChecking", "no"); } @Override protected JSch createDefaultJSch(FS fs) throws JSchException { JSch jSch = super.createDefaultJSch(fs); jSch.addIdentity(sshPrivateFilePath, sshPassword.getBytes()); return jSch; } }; @Override public void configure(Transport transport) { SshTransport sshTransport = (SshTransport) transport; sshTransport.setSshSessionFactory(sshSessionFactory); } } }
Output
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Project cloned through SSH in: C:\Users\Ngdeveloper\AppData\Local\Temp\workspace1469196261233043335
And in this path: C:\Users\Ngdeveloper\AppData\Local\Temp\workspace1469196261233043335, I can able to see the complete code is cloned/downloaded.