In this guide I will show you how to set up JGIT with SSH authentication using Apache MINA.
First of all we need to import the maven dependencies:
<dependency> <groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.8.0.202311291450-r</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.ssh.apache</artifactId>
<version>6.8.0.202311291450-r</version>
</dependency>
We create a ssh factory:
SshdSessionFactory sshdSessionFactory = new SshdSessionFactoryBuilder()
.setPreferredAuthentications("publickey,keyboard-interactive,password")
.setHomeDirectory(FS.DETECTED.userHome())
.setSshDirectory(sshDir)
.build(new JGitKeyCache());
SshSessionFactory.setInstance(sshdSessionFactory);
Than we can simply clone a Repository:
var repository = "git@github.com:eclipse-jgit/jgit.git";
var directory = "./directory";
Git.cloneRepository().setURI(repository)
.setDirectory(new File(directory))
.call();