Search code examples
javasvnkit

Once I've finished using a working copy with SVNKit, how can I remove it?


I have created a class to perform commits to a Subversion repository using SVNKit. When I've completed the commits I want to destroy the temporary directory I was using as a working copy.

The way I'm creating the working copy directory is as follows:

    Path svnWorkingCopyDirectory = Files
            .createTempDirectory("svn-working-copy-directory-"
                    + String.valueOf(System.currentTimeMillis()));
    LOGGER.debug("Created SVN working copy directory: " + svnWorkingCopyDirectory);
    svnWorkingCopyDirectory.toFile().deleteOnExit();

However, the directory does not get deleted on exit. Is there a way in SVNKit to mark the directory as no longer a working copy? Or another method to remove the directory?


Solution

  • File.deleteOnExit() works like File.delete() and Files.delete() in respect to the fact that if its argument denotes a directory, the directory must be empty to be removed which in your case probably it is not.

    So if you want to delete this directory on exit you must register a shutdown hook and in that method you have to iterate recursively through the directory depth first to remove the files and then the empty directories.

    Apache commons has a delete method you might use in a shutdown hook or whenever it is appropriate for you: https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#deleteDirectory(java.io.File)