A common place to store files used by Clojure (or JVM) applications

I have an application written in Clojure. This application helps to write other applications (for example, as a plugin for leiningen

). To do this, it needs a place to write some files that are used more or less internally and really do not need to be glimpsed by the user (do not need to be hidden from her).

I used clojure.java.io/resource

to do something similar, but it looks like it only allows you to access the resources associated with the application, but not create new resources or modify existing ones.

I am considering using something like ~/.myapp/

this, but I suspect that Clojure

either Java

or leiningen

have some conventions about this, with the API and everything else, to save me some trouble. However, since I'm new to this, I don't know where to dig.

As I said, this is a plugin for leiningen

, but I cannot use a folder %project/out/

or something like that because I want it to be able to work even without a project.

+3


source to share


2 answers


I think there is a subdirectory underneath ~/.lein

that you can use, maybe something like ~/.lein/plugin-name/your_stuff_here

.



+2


source


Unfortunately, there is no definitive agreement on where to put intermediate files for programs, neither in the java / clojure world, nor in the home world. However, I think there are some basic principles.

As far as I know, most programs extract all the files they need into mailing files (the ones that shouldn't be changed: resources like icons and sounds or binaries like jar files), configuration files (those that the user can modify, either directly or through the program itself) and temporary files (I think it's obvious what this is).

Distribution files are usually installed using a package manager (mainly on Linux systems) or, if necessary, they are manually installed in some predefined directory, for example /opt

. On Windows systems, they are usually installed by the installer. These files cannot be modified directly, but can usually be updated by the Package Manager during updates.

Configuration files are usually stored in the user's home directory (if the program is a user's application). This is unfortunate, but there is no agreement on exactly where to place these files. This is a pain on unix systems ( ~

cluttering), but on Windows IMO it is even worse because files can appear almost anywhere on file systems, as it is not widely accepted to set up restrictive file system permissions under Windows.

However, there is now a trend (in the Linux world at least) to store configuration files in subdirectories of an environment variable $XDG_CONFIG_HOME

; this variable usually decides the path ~/.config

. I'm pretty sure it's correct to use this directory for setup. There is a family of these variables ( $XDG_{CONFIG,CACHE,DATA}_HOME

), each of which is used for specific file types. I think this is the freedesktop standard. I don't know if Windows has something like this, but it is very possible; I would try to find information on this and use it if I had such a need.

Many programs allow you to explicitly set the exact location of the configuration directory, for example. IntelliJ IDEA IDE. It could also be an option.



Temporary files are usually stored in the directory where the system provides temporary files. On Unix systems, usually /tmp

, on Windows, this might be, for example C:\Users\%USER%\AppData\Local\Temp

. In Java, you can get the temporary location of a directory using system properties. In Clojure it will look like

(System/getProperty "java.io.tmpdir")

      

If I recall correctly, there is even a function somewhere in the Clojure standard library that allows you to get this value.

Only temporary files should always be kept here, as this directory can be cleaned up (in recent Linux distributions, it is even stored in RAM).

TL; DR

If you want to store persistent configuration files (work, cache, etc.), use a directory ~/.config

on Unix systems and some sane directory on Windows (this requires more lookups); if you want to store temporary files that you don't need after doing any work in the program, use the standard system temporary directory, which you can get through the system property "java.io.tmpdir"

.

+2


source







All Articles