I clone a remote repository to my local environment using Java JGit library (latest version that is 5.9.0.202009080501-r).
Git git = Git.cloneRepository().setURI(repositoryUrl).setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).setDirectory(targetDir).call();
After using the contents of the repository I want to remove it from my local environment by deleting the directory of this local repository.
FileUtils.deleteDirectory(gitDirectory);
The problem is that I cannot remove the .git directory from the Java code because access is denied on files in the '.git\objects\pack' directory
java.io.IOException: Unable to delete file: 'local repository directory'.git\objects ... Caused by: java.nio.file.AccessDeniedException: 'local repository directory'.git\objects\pack\pack-*************.idx**
It is the JGit library that locks these files. After stopping the Java program I can delete these files manually. But I want to remove them from the code because on the server there is no way to stop an application just to delete something.
I am investigating this problem for days by now. What I tried so far:
- Close everything that the Jgit library provides.
git.close();
git.getRepository().close();
git.getRepository().getObjectDatabase().close();
Git.shutdown();
- Some threads state that files are locked by the Garbage collector of the Jgit, I tried to turn it off by configuration parameters
StoredConfig configuration = git.getRepository().getConfig();
configuration.setBoolean( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTODETACH, false );
configuration.setInt( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTOPACKLIMIT, 0 );
configuration.setInt( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTO, 0 );
configuration.save();
- Some threads suggested that this problem can be solved by the below configuration of the WindowCacheConfig.
WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitMMAP(false);
WindowCache.reconfigure(config);
- Tried to play around with the with different settings of the JGit garbage collector. I tried multiple combinations and values (true/false) of the below settings. In most cases these settings made it worse because created an additional .bitmap file in the pack directory that was again impoossible to delete.
git.gc().setPreserveOldPacks(false).call();
git.gc().setPrunePreserved(true).call();
git.gc().setAggressive(true).call();
None of the above attempts helped, the result is always the same AccessDeniedException. Any help is appreciated.